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 #include <sstream>
00029 #include <fstream>
00030
00031 #endif //HAVE_BOOST_SERIALIZATION
00032
00033
00034 #include <iostream>
00035 #include <string>
00036 #include <vector>
00037 #include <set>
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
00162 void set_io(CIO* io);
00163
00168 CIO* get_io();
00169
00174 void set_parallel(CParallel* parallel);
00175
00180 CParallel* get_parallel();
00181
00186 void set_version(CVersion* version);
00187
00192 CVersion* get_version();
00193
00194 #ifdef HAVE_BOOST_SERIALIZATION
00195
00199 virtual std::string to_string() const;
00200
00205 virtual void from_string(std::string str);
00206
00211 virtual void to_file(std::string filename) const;
00212
00217 virtual void from_file(std::string filename);
00218
00219 protected:
00220 friend class ::boost::serialization::access;
00221
00229 template<class Archive>
00230 void serialize(Archive & ar, const unsigned int archive_version)
00231 {
00232
00233 SG_DEBUG("SERIALIZING SGObject (done)\n");
00234 }
00235
00236 #endif //HAVE_BOOST_SERIALIZATION
00237
00238
00239 private:
00240 void set_global_objects();
00241
00242 private:
00243 int32_t refcount;
00244 #ifndef WIN32
00245 pthread_mutex_t ref_mutex;
00246 #endif
00247
00248 public:
00249 CIO* io;
00250 CParallel* parallel;
00251 CVersion* version;
00252 };
00253 }
00254 #endif // __SGOBJECT_H__