SGObject.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "base/SGObject.h"
00012 #include "lib/io.h"
00013 #include "base/Parallel.h"
00014 #include "base/init.h"
00015 #include "base/Version.h"
00016
00017 #include <stdlib.h>
00018 #include <stdio.h>
00019
00020
00021 #ifdef HAVE_BOOST_SERIALIZATION
00022 #include <boost/serialization/export.hpp>
00023
00024
00025 #endif //HAVE_BOOST_SERIALIZATION
00026
00027 namespace shogun
00028 {
00029 class CMath;
00030 class CParallel;
00031 class CIO;
00032 class CVersion;
00033
00034 extern CMath* sg_math;
00035 extern CParallel* sg_parallel;
00036 extern CIO* sg_io;
00037 extern CVersion* sg_version;
00038
00039 }
00040
00041 using namespace shogun;
00042
00043 void CSGObject::set_global_objects()
00044 {
00045 if (!sg_io || !sg_parallel || !sg_version)
00046 {
00047 fprintf(stderr, "call init_shogun() before using the library, dying.\n");
00048 exit(1);
00049 }
00050
00051 SG_REF(sg_io);
00052 SG_REF(sg_parallel);
00053 SG_REF(sg_version);
00054
00055 io=sg_io;
00056 parallel=sg_parallel;
00057 version=sg_version;
00058 }
00059
00060 #ifdef HAVE_BOOST_SERIALIZATION
00061 std::string CSGObject::to_string() const
00062 {
00063 std::ostringstream s;
00064 boost::archive::text_oarchive oa(s);
00065 oa << this;
00066 return s.str();
00067 }
00068
00069 void CSGObject::from_string(std::string str)
00070 {
00071 std::istringstream is(str);
00072 boost::archive::text_iarchive ia(is);
00073
00074
00075 CSGObject* tmp = const_cast<CSGObject*>(this);
00076
00077 ia >> tmp;
00078 *this = *tmp;
00079 }
00080
00081 void CSGObject::to_file(std::string filename) const
00082 {
00083 std::ofstream os(filename.c_str(), std::ios::binary);
00084 boost::archive::binary_oarchive oa(os);
00085 oa << this;
00086 }
00087
00088 void CSGObject::from_file(std::string filename)
00089 {
00090 std::ifstream is(filename.c_str(), std::ios::binary);
00091 boost::archive::binary_iarchive ia(is);
00092 CSGObject* tmp= const_cast<CSGObject*>(this);
00093 ia >> tmp;
00094 }
00095 #endif //HAVE_BOOST_SERIALIZATION