SGObject.h

Go to the documentation of this file.
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 #else
00046 #define SG_REF(x)
00047 #define SG_UNREF(x)
00048 #endif
00049 
00060 class CSGObject
00061 {
00062 public:
00064     CSGObject();
00065 
00067     CSGObject(const CSGObject& orig);
00068 
00070     virtual ~CSGObject();
00071 
00072 #ifdef USE_REFERENCE_COUNTING
00073 
00077     inline int32_t ref()
00078     {
00079         pthread_mutex_lock(&m_ref_mutex);
00080         ++m_refcount;
00081         SG_GCDEBUG("ref() refcount %ld obj %s (%p) increased\n", m_refcount, this->get_name(), this);
00082         pthread_mutex_unlock(&m_ref_mutex);
00083         return m_refcount;
00084     }
00085 
00090     inline int32_t ref_count()
00091     {
00092         pthread_mutex_lock(&m_ref_mutex);
00093         int32_t count=m_refcount;
00094         SG_GCDEBUG("ref_count(): refcount %d, obj %s (%p)\n", count, this->get_name(), this);
00095         pthread_mutex_unlock(&m_ref_mutex);
00096         return count;
00097     }
00098 
00104     inline int32_t unref()
00105     {
00106         pthread_mutex_lock(&m_ref_mutex);
00107         if (m_refcount==0 || --m_refcount==0)
00108         {
00109             SG_GCDEBUG("unref() refcount %ld, obj %s (%p) destroying\n", m_refcount, this->get_name(), this);
00110             pthread_mutex_unlock(&m_ref_mutex);
00111             delete this;
00112             return 0;
00113         }
00114         else
00115         {
00116             SG_GCDEBUG("unref() refcount %ld obj %s (%p) decreased\n", m_refcount, this->get_name(), this);
00117             pthread_mutex_unlock(&m_ref_mutex);
00118             return m_refcount;
00119         }
00120     }
00121 #endif
00122 
00128     virtual const char* get_name() const = 0;
00129 
00138     virtual bool is_generic(EPrimitiveType* generic) const;
00139 
00142     template<class T> void set_generic();
00143 
00148     void unset_generic();
00149 
00154     virtual void print_serializable(const char* prefix="");
00155 
00164     virtual bool save_serializable(CSerializableFile* file,
00165                                    const char* prefix="");
00166 
00176     virtual bool load_serializable(CSerializableFile* file,
00177                                    const char* prefix="");
00178 
00183     void set_io(IO* io);
00184 
00189     IO* get_io();
00190 
00195     void set_parallel(Parallel* parallel);
00196 
00201     Parallel* get_parallel();
00202 
00207     void set_version(Version* version);
00208 
00213     Version* get_version();
00214 
00215 protected:
00216 
00225     virtual void load_serializable_pre() throw (ShogunException);
00226 
00235     virtual void load_serializable_post() throw (ShogunException);
00236 
00245     virtual void save_serializable_pre() throw (ShogunException);
00246 
00255     virtual void save_serializable_post() throw (ShogunException);
00256 
00257 private:
00258     void set_global_objects();
00259     void unset_global_objects();
00260     void init();
00261 
00262 public:
00263     IO* io;
00264     Parallel* parallel;
00265     Version* version;
00266 
00267 protected:
00268     Parameter* m_parameters;
00269 
00270 private:
00271     EPrimitiveType m_generic;
00272     bool m_load_pre_called;
00273     bool m_load_post_called;
00274     bool m_save_pre_called;
00275     bool m_save_post_called;
00276 
00277     int32_t m_refcount;
00278 
00279 #ifndef WIN32
00280     pthread_mutex_t m_ref_mutex;
00281 #endif
00282 };
00283 }
00284 #endif // __SGOBJECT_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation