SGObject.h

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 #ifndef __SGOBJECT_H__
00012 #define __SGOBJECT_H__
00013 
00014 #ifdef HAVE_BOOST_SERIALIZATION
00015 #include <boost/archive/text_oarchive.hpp>
00016 #include <boost/archive/text_iarchive.hpp>
00017 
00018 #include <boost/archive/binary_oarchive.hpp>
00019 #include <boost/archive/binary_iarchive.hpp>
00020 
00021 #include <boost/serialization/vector.hpp>
00022 
00023 //TODO xml will not work right away, every class needs name-value-pairs (NVP)
00024 //will have to be defined using respective boost macros
00025 //#include <boost/archive/xml_oarchive.hpp>
00026 //#include <boost/archive/xml_iarchive.hpp>
00027 
00028 //some STL modules needed for serialization
00029 
00030 #include <string>
00031 #include <sstream>
00032 #include <fstream>
00033 
00034 #endif //HAVE_BOOST_SERIALIZATION
00035 
00036 #include <iostream>
00037 #include <vector>
00038 
00039 #include "lib/io.h"
00040 #include "base/Parallel.h"
00041 #include "base/Version.h"
00042 
00043 #ifndef WIN32
00044 #include <pthread.h>
00045 #else
00046 #define pthread_mutex_init(x)
00047 #define pthread_mutex_destroy(x)
00048 #define pthread_mutex_lock(x)
00049 #define pthread_mutex_unlock(x)
00050 #endif
00051 
00055 namespace shogun
00056 {
00057 class CIO;
00058 class CParallel;
00059 class CVersion;
00060 
00061 // define reference counter macros
00062 //
00063 #ifdef USE_REFERENCE_COUNTING
00064 #define SG_REF(x) { if (x) (x)->ref(); }
00065 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=0; } }
00066 #else
00067 #define SG_REF(x)
00068 #define SG_UNREF(x)
00069 #endif
00070 
00081 class CSGObject
00082 {
00083 public:
00084     inline CSGObject() : refcount(0)
00085     {
00086         set_global_objects();
00087         pthread_mutex_init(&ref_mutex, NULL);
00088     }
00089 
00090     inline CSGObject(const CSGObject& orig) : refcount(0), io(orig.io),
00091         parallel(orig.parallel), version(orig.version)
00092     {
00093         set_global_objects();
00094     }
00095 
00096     virtual ~CSGObject()
00097     {
00098         pthread_mutex_destroy(&ref_mutex);
00099         SG_UNREF(version);
00100         SG_UNREF(parallel);
00101         SG_UNREF(io);
00102     }
00103 
00104 #ifdef USE_REFERENCE_COUNTING
00105 
00109     inline int32_t ref()
00110     {
00111         pthread_mutex_lock(&ref_mutex);
00112         ++refcount;
00113         SG_GCDEBUG("ref() refcount %ld obj %s (%p) increased\n", refcount, this->get_name(), this);
00114         pthread_mutex_unlock(&ref_mutex);
00115         return refcount;
00116     }
00117 
00122     inline int32_t ref_count() const
00123     {
00124         SG_GCDEBUG("ref_count(): refcount %d, obj %s (%p)\n", refcount, this->get_name(), this);
00125         return refcount;
00126     }
00127 
00133     inline int32_t unref()
00134     {
00135         pthread_mutex_lock(&ref_mutex);
00136         if (refcount==0 || --refcount==0)
00137         {
00138             SG_GCDEBUG("unref() refcount %ld, obj %s (%p) destroying\n", refcount, this->get_name(), this);
00139             pthread_mutex_unlock(&ref_mutex);
00140             delete this;
00141             return 0;
00142         }
00143         else
00144         {
00145             SG_GCDEBUG("unref() refcount %ld obj %s (%p) decreased\n", refcount, this->get_name(), this);
00146             pthread_mutex_unlock(&ref_mutex);
00147             return refcount;
00148         }
00149     }
00150 #endif
00151 
00156     virtual const char* get_name() const=0;
00157 
00158 #ifdef HAVE_BOOST_SERIALIZATION
00159 
00163     virtual std::string to_string() const;
00164 
00169     virtual void from_string(std::string str);
00170 
00175     virtual void to_file(std::string filename) const;
00176 
00181     virtual void from_file(std::string filename);
00182 
00183   protected:
00184     friend class ::boost::serialization::access;
00185 
00193     template<class Archive>
00194         void serialize(Archive & ar, const unsigned int archive_version)
00195         {
00196             //ar & test;
00197             SG_DEBUG("SERIALIZING SGObject");
00198         }
00199 
00200 #endif //HAVE_BOOST_SERIALIZATION
00201 
00202 
00203 private:
00204     void set_global_objects();
00205 
00206 private:
00207     int32_t refcount;
00208 #ifndef WIN32
00209     pthread_mutex_t ref_mutex;
00210 #endif
00211 
00212 public:
00213     CIO* io;
00214     CParallel* parallel;
00215     CVersion* version;
00216 };
00217 }
00218 #endif // __SGOBJECT_H__

SHOGUN Machine Learning Toolbox - Documentation