SGObject.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2008-2009 Soeren Sonnenburg
00008  * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max Planck Society
00009  */
00010 
00011 #include "base/SGObject.h"
00012 #include "lib/io.h"
00013 #include "lib/Mathematics.h"
00014 #include "base/Parallel.h"
00015 #include "base/init.h"
00016 #include "base/Version.h"
00017 
00018 #include <stdlib.h>
00019 #include <stdio.h>
00020 
00021 #ifdef HAVE_BOOST_SERIALIZATION
00022 #include <boost/serialization/access.hpp>
00023 #include <boost/archive/text_oarchive.hpp>
00024 #include <boost/archive/text_iarchive.hpp>
00025 #include <boost/archive/binary_oarchive.hpp>
00026 #include <boost/archive/binary_iarchive.hpp>
00027 #include <boost/serialization/export.hpp>
00028 //#include <boost/serialization/vector.hpp>
00029 //
00036 #include <sstream>
00037 #include <iostream>
00038 #include <string>
00039 #include <fstream>
00040 //#include <vector>
00041 BOOST_IS_ABSTRACT(CSGObject);
00042 #endif //HAVE_BOOST_SERIALIZATION
00043 
00044 
00045 extern CParallel* sg_parallel;
00046 extern CIO* sg_io;
00047 extern CVersion* sg_version;
00048 extern CMath* sg_math;
00049 
00050 void CSGObject::set_global_objects()
00051 {
00052     if (!sg_io || !sg_parallel || !sg_version)
00053     {
00054         fprintf(stderr, "call init_shogun() before using the library, dying.\n");
00055         exit(1);
00056     }
00057 
00058     SG_REF(sg_io);
00059     SG_REF(sg_parallel);
00060     SG_REF(sg_version);
00061 
00062     io=sg_io;
00063     parallel=sg_parallel;
00064     version=sg_version;
00065 }
00066 
00067 #ifdef HAVE_BOOST_SERIALIZATION
00068 std::string CSGObject::to_string() const
00069 {
00070     std::ostringstream s;
00071     boost::archive::text_oarchive oa(s);
00072     oa << this;
00073     return s.str();
00074 }
00075 
00076 void CSGObject::from_string(std::string str)
00077 {
00078     std::istringstream is(str);
00079     boost::archive::text_iarchive ia(is);
00080 
00081     //cast away constness
00082     CSGObject* tmp = const_cast<CSGObject*>(this);
00083 
00084     ia >> tmp;
00085     *this = *tmp;
00086 }
00087 
00088 void CSGObject::to_file(std::string filename) const
00089 {
00090     std::ofstream os(filename.c_str(), std::ios::binary);
00091     boost::archive::binary_oarchive oa(os);
00092     oa << this;
00093 }
00094 
00095 void CSGObject::from_file(std::string filename)
00096 {
00097     std::ifstream is(filename.c_str(), std::ios::binary);
00098     boost::archive::binary_iarchive ia(is);
00099     CSGObject* tmp= const_cast<CSGObject*>(this);
00100     ia >> tmp; 
00101 }
00102 #endif //HAVE_BOOST_SERIALIZATION

SHOGUN Machine Learning Toolbox - Documentation