|
SHOGUN v0.9.3
|
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 #include <sstream> 00029 #include <fstream> 00030 00031 #endif //HAVE_BOOST_SERIALIZATION 00032 00033 //some STL modules 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 // 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 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 //ar & test; 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__