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 "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 //BOOST_IS_ABSTRACT(CSGObject);
00024 //BOOST_SERIALIZATION_ASSUME_ABSTRACT(shogun::CSGObject);
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 void CSGObject::set_io(CIO* new_io)
00061 {
00062     SG_UNREF(sg_io);
00063     sg_io=new_io;
00064     SG_REF(sg_io);
00065 }
00066 
00067 CIO* CSGObject::get_io()
00068 {
00069     SG_REF(sg_io);
00070     return sg_io;
00071 }
00072 
00073 void CSGObject::set_parallel(CParallel* new_parallel)
00074 {
00075     SG_UNREF(sg_parallel);
00076     sg_parallel=new_parallel;
00077     SG_REF(sg_parallel);
00078 }
00079 
00080 CParallel* CSGObject::get_parallel()
00081 {
00082     SG_REF(sg_parallel);
00083     return sg_parallel;
00084 }
00085 
00086 void CSGObject::set_version(CVersion* new_version)
00087 {
00088     SG_UNREF(sg_version);
00089     sg_version=new_version;
00090     SG_REF(sg_version);
00091 }
00092 
00093 CVersion* CSGObject::get_version()
00094 {
00095     SG_REF(sg_version);
00096     return sg_version;
00097 }
00098 
00099 
00100 #ifdef HAVE_BOOST_SERIALIZATION
00101 std::string CSGObject::to_string() const
00102 {
00103     std::ostringstream s;
00104     boost::archive::text_oarchive oa(s);
00105     oa << this;
00106     return s.str();
00107 }
00108 
00109 void CSGObject::from_string(std::string str)
00110 {
00111     std::istringstream is(str);
00112     boost::archive::text_iarchive ia(is);
00113 
00114     //cast away constness
00115     CSGObject* tmp = const_cast<CSGObject*>(this);
00116 
00117     ia >> tmp;
00118     *this = *tmp;
00119 }
00120 
00121 void CSGObject::to_file(std::string filename) const
00122 {
00123     std::ofstream os(filename.c_str(), std::ios::binary);
00124     boost::archive::binary_oarchive oa(os);
00125     oa << this;
00126 }
00127 
00128 void CSGObject::from_file(std::string filename)
00129 {
00130     std::ifstream is(filename.c_str(), std::ios::binary);
00131     boost::archive::binary_iarchive ia(is);
00132     CSGObject* tmp= const_cast<CSGObject*>(this);
00133     ia >> tmp;
00134 }
00135 #endif //HAVE_BOOST_SERIALIZATION
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation