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 <shogun/lib/config.h>
00015 #include <shogun/io/SGIO.h>
00016 #include <shogun/lib/DataType.h>
00017 #include <shogun/lib/ShogunException.h>
00018 #include <shogun/lib/memory.h>
00019 #include <shogun/base/Parallel.h>
00020 #include <shogun/base/Version.h>
00021 
00022 #ifdef HAVE_PTHREAD
00023 #include <pthread.h>
00024 #endif //HAVE_PTHREAD
00025 
00029 namespace shogun
00030 {
00031 class IO;
00032 class Parallel;
00033 class Version;
00034 class Parameter;
00035 class CSerializableFile;
00036 
00037 // define reference counter macros
00038 //
00039 #ifdef USE_REFERENCE_COUNTING
00040 #define SG_REF(x) { if (x) (x)->ref(); }
00041 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=NULL; } }
00042 #define SG_UNREF_NO_NULL(x) { if (x) { (x)->unref(); } }
00043 #else
00044 #define SG_REF(x)
00045 #define SG_UNREF(x)
00046 #define SG_UNREF_NO_NULL(x)
00047 #endif
00048 
00049 /*******************************************************************************
00050  * Macros for registering parameters/model selection parameters
00051  ******************************************************************************/
00052 #define SG_ADD(param, name, description, ms_available) {\
00053         m_parameters->add(param, name, description);\
00054         if (ms_available)\
00055             m_model_selection_parameters->add(param, name, description);\
00056 }
00057 /*******************************************************************************
00058  * End of macros for registering parameters/model selection parameters
00059  ******************************************************************************/
00060 
00062 enum EModelSelectionAvailability {
00063     MS_NOT_AVAILABLE=0, MS_AVAILABLE
00064 };
00065 
00076 class CSGObject
00077 {
00078 public:
00080     CSGObject();
00081 
00083     CSGObject(const CSGObject& orig);
00084 
00086     virtual ~CSGObject();
00087 
00088 #ifdef USE_REFERENCE_COUNTING
00089 
00093     int32_t ref();
00094 
00099     int32_t ref_count();
00100 
00106     int32_t unref();
00107 #endif //USE_REFERENCE_COUNTING
00108 
00114     virtual const char* get_name() const = 0;
00115 
00124     virtual bool is_generic(EPrimitiveType* generic) const;
00125 
00128     template<class T> void set_generic();
00129 
00134     void unset_generic();
00135 
00140     virtual void print_serializable(const char* prefix="");
00141 
00150     virtual bool save_serializable(CSerializableFile* file,
00151                                    const char* prefix="");
00152 
00162     virtual bool load_serializable(CSerializableFile* file,
00163                                    const char* prefix="");
00164 
00169     void set_global_io(SGIO* io);
00170 
00175     SGIO* get_global_io();
00176 
00181     void set_global_parallel(Parallel* parallel);
00182 
00187     Parallel* get_global_parallel();
00188 
00193     void set_global_version(Version* version);
00194 
00199     Version* get_global_version();
00200 
00203     SGVector<char*> get_modelsel_names();
00204 
00211     char* get_modsel_param_descr(const char* param_name);
00212 
00219     index_t get_modsel_param_index(const char* param_name);
00220 
00221 #ifdef TRACE_MEMORY_ALLOCS
00222     static void list_memory_allocs()
00223     {
00224         ::list_memory_allocs();
00225     }
00226 #endif
00227 
00228 protected:
00229 
00238     virtual void load_serializable_pre() throw (ShogunException);
00239 
00248     virtual void load_serializable_post() throw (ShogunException);
00249 
00258     virtual void save_serializable_pre() throw (ShogunException);
00259 
00268     virtual void save_serializable_post() throw (ShogunException);
00269 
00270 private:
00271     void set_global_objects();
00272     void unset_global_objects();
00273     void init();
00274 
00278     bool save_parameter_version(CSerializableFile* file, const char* prefix="");
00279 
00283     int32_t load_parameter_version(CSerializableFile* file,
00284             const char* prefix="");
00285 
00286 public:
00288     SGIO* io;
00289 
00291     Parallel* parallel;
00292 
00294     Version* version;
00295 
00297     Parameter* m_parameters;
00298 
00300     Parameter* m_model_selection_parameters;
00301 
00302 private:
00303     EPrimitiveType m_generic;
00304     bool m_load_pre_called;
00305     bool m_load_post_called;
00306     bool m_save_pre_called;
00307     bool m_save_post_called;
00308 
00309     int32_t m_refcount;
00310 
00311 #ifdef HAVE_PTHREAD
00312     PTHREAD_LOCK_T m_ref_lock;
00313 #endif //HAVE_PTHREAD
00314 };
00315 }
00316 #endif // __SGOBJECT_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation