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/serialization/access.hpp>
00016 #include <string>
00017 #endif //HAVE_BOOST_SERIALIZATION
00018
00019 #include "lib/io.h"
00020 #include "base/Parallel.h"
00021 #include "base/Version.h"
00022
00023 #ifndef WIN32
00024 #include <pthread.h>
00025 #else
00026 #define pthread_mutex_init(x)
00027 #define pthread_mutex_destroy(x)
00028 #define pthread_mutex_lock(x)
00029 #define pthread_mutex_unlock(x)
00030 #endif
00031
00032 class CSGObject;
00033 class CIO;
00034
00035
00036 #define SG_REF(x) { if (x) (x)->ref(); }
00037 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=0; } }
00038
00049 class CSGObject
00050 {
00051 public:
00052 inline CSGObject() : refcount(0)
00053 {
00054 set_global_objects();
00055 pthread_mutex_init(&ref_mutex, NULL);
00056 }
00057
00058 inline CSGObject(const CSGObject& orig) : refcount(0), io(orig.io),
00059 parallel(orig.parallel), version(orig.version)
00060 {
00061 set_global_objects();
00062 }
00063
00064 virtual ~CSGObject()
00065 {
00066 pthread_mutex_destroy(&ref_mutex);
00067 SG_UNREF(version);
00068 SG_UNREF(parallel);
00069 SG_UNREF(io);
00070 }
00071
00076 inline int32_t ref()
00077 {
00078 pthread_mutex_lock(&ref_mutex);
00079 ++refcount;
00080 SG_DEBUG("ref() refcount %ld obj %s (%p) increased\n", refcount, this->get_name(), this);
00081 pthread_mutex_unlock(&ref_mutex);
00082 return refcount;
00083 }
00084
00089 inline int32_t ref_count() const
00090 {
00091 SG_DEBUG("ref_count(): refcount %d, obj %s (%p)\n", refcount, this->get_name(), this);
00092 return refcount;
00093 }
00094
00100 inline int32_t unref()
00101 {
00102 pthread_mutex_lock(&ref_mutex);
00103 if (refcount==0 || --refcount==0)
00104 {
00105 SG_DEBUG("unref() refcount %ld, obj %s (%p) destroying\n", refcount, this->get_name(), this);
00106 pthread_mutex_unlock(&ref_mutex);
00107 delete this;
00108 return 0;
00109 }
00110 else
00111 {
00112 SG_DEBUG("unref() refcount %ld obj %s (%p) decreased\n", refcount, this->get_name(), this);
00113 pthread_mutex_unlock(&ref_mutex);
00114 return refcount;
00115 }
00116 }
00117
00122 virtual const char* get_name() const=0;
00123
00124 #ifdef HAVE_BOOST_SERIALIZATION
00125
00129 virtual std::string to_string() const;
00130
00135 virtual void from_string(std::string str);
00136
00141 virtual void to_file(std::string filename) const;
00142
00147 virtual void from_file(std::string filename);
00148
00149 protected:
00150 friend class boost::serialization::access;
00151
00159 template<class Archive>
00160 void serialize(Archive & ar, const unsigned int version_num)
00161 {
00162
00163 SG_ERROR("SERIALIZING SGObject: nothing to do");
00164 }
00165
00166 #endif //HAVE_BOOST_SERIALIZATION
00167
00168
00169 private:
00170 void set_global_objects();
00171
00172 private:
00173 int32_t refcount;
00174 #ifndef WIN32
00175 pthread_mutex_t ref_mutex;
00176 #endif
00177
00178 public:
00179 CIO* io;
00180 CParallel* parallel;
00181 CVersion* version;
00182 };
00183 #endif // __SGOBJECT_H__