SHOGUN v0.9.0
SGObject.h
浏览该文件的文档。
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-2010 Soeren Sonnenburg
00008  * Copyright (C) 2008-2010 Fraunhofer Institute FIRST and Max Planck Society
00009  */
00010 
00011 #ifndef __SGOBJECT_H__
00012 #define __SGOBJECT_H__
00013 
00014 #include "lib/io.h"
00015 #include "lib/DataType.h"
00016 #include "lib/ShogunException.h"
00017 #include "base/Parallel.h"
00018 #include "base/Version.h"
00019 
00020 #ifndef WIN32
00021 #include <pthread.h>
00022 #else
00023 #define pthread_mutex_init(x)
00024 #define pthread_mutex_destroy(x)
00025 #define pthread_mutex_lock(x)
00026 #define pthread_mutex_unlock(x)
00027 #endif
00028 
00032 namespace shogun
00033 {
00034 class IO;
00035 class Parallel;
00036 class Version;
00037 class Parameter;
00038 class CSerializableFile;
00039 
00040 // define reference counter macros
00041 //
00042 #ifdef USE_REFERENCE_COUNTING
00043 #define SG_REF(x) { if (x) (x)->ref(); }
00044 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=NULL; } }
00045 #define SG_UNREF_NO_NULL(x) { if (x) { (x)->unref(); } }
00046 #else
00047 #define SG_REF(x)
00048 #define SG_UNREF(x)
00049 #endif
00050 
00061 class CSGObject
00062 {
00063 public:
00065     CSGObject();
00066 
00068     CSGObject(const CSGObject& orig);
00069 
00071     virtual ~CSGObject();
00072 
00073 #ifdef USE_REFERENCE_COUNTING
00074 
00078     inline int32_t ref()
00079     {
00080         pthread_mutex_lock(&m_ref_mutex);
00081         ++m_refcount;
00082         SG_GCDEBUG("ref() refcount %ld obj %s (%p) increased\n", m_refcount, this->get_name(), this);
00083         pthread_mutex_unlock(&m_ref_mutex);
00084         return m_refcount;
00085     }
00086 
00091     inline int32_t ref_count()
00092     {
00093         pthread_mutex_lock(&m_ref_mutex);
00094         int32_t count=m_refcount;
00095         SG_GCDEBUG("ref_count(): refcount %d, obj %s (%p)\n", count, this->get_name(), this);
00096         pthread_mutex_unlock(&m_ref_mutex);
00097         return count;
00098     }
00099 
00105     inline int32_t unref()
00106     {
00107         pthread_mutex_lock(&m_ref_mutex);
00108         if (m_refcount==0 || --m_refcount==0)
00109         {
00110             SG_GCDEBUG("unref() refcount %ld, obj %s (%p) destroying\n", m_refcount, this->get_name(), this);
00111             pthread_mutex_unlock(&m_ref_mutex);
00112             delete this;
00113             return 0;
00114         }
00115         else
00116         {
00117             SG_GCDEBUG("unref() refcount %ld obj %s (%p) decreased\n", m_refcount, this->get_name(), this);
00118             pthread_mutex_unlock(&m_ref_mutex);
00119             return m_refcount;
00120         }
00121     }
00122 #endif
00123 
00129     virtual const char* get_name() const = 0;
00130 
00139     virtual bool is_generic(EPrimitiveType* generic) const;
00140 
00143     template<class T> void set_generic();
00144 
00149     void unset_generic();
00150 
00155     virtual void print_serializable(const char* prefix="");
00156 
00165     virtual bool save_serializable(CSerializableFile* file,
00166                                    const char* prefix="");
00167 
00177     virtual bool load_serializable(CSerializableFile* file,
00178                                    const char* prefix="");
00179 
00184     void set_io(IO* io);
00185 
00190     IO* get_io();
00191 
00196     void set_parallel(Parallel* parallel);
00197 
00202     Parallel* get_parallel();
00203 
00208     void set_version(Version* version);
00209 
00214     Version* get_version();
00215 
00216 protected:
00217 
00226     virtual void load_serializable_pre() throw (ShogunException);
00227 
00236     virtual void load_serializable_post() throw (ShogunException);
00237 
00246     virtual void save_serializable_pre() throw (ShogunException);
00247 
00256     virtual void save_serializable_post() throw (ShogunException);
00257 
00258 private:
00259     void set_global_objects();
00260     void unset_global_objects();
00261     void init();
00262 
00263 public:
00264     IO* io;
00265     Parallel* parallel;
00266     Version* version;
00267 
00268 protected:
00269     Parameter* m_parameters;
00270 
00271 private:
00272     EPrimitiveType m_generic;
00273     bool m_load_pre_called;
00274     bool m_load_post_called;
00275     bool m_save_pre_called;
00276     bool m_save_post_called;
00277 
00278     int32_t m_refcount;
00279 
00280 #ifndef WIN32
00281     pthread_mutex_t m_ref_mutex;
00282 #endif
00283 };
00284 }
00285 #endif // __SGOBJECT_H__

SHOGUN Machine Learning Toolbox - Documentation