SGObject.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
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
00024
00025
00026
00027
00028
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
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
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__