SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SGObject.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2008-2010 Soeren Sonnenburg
8  * Written (W) 2011-2013 Heiko Strathmann
9  * Written (W) 2013-2014 Thoralf Klein
10  * Copyright (C) 2008-2010 Fraunhofer Institute FIRST and Max Planck Society
11  */
12 
13 #ifndef __SGOBJECT_H__
14 #define __SGOBJECT_H__
15 
16 #include <shogun/lib/config.h>
17 
18 #include <shogun/lib/common.h>
19 #include <shogun/lib/DataType.h>
21 #include <shogun/base/Version.h>
22 
26 namespace shogun
27 {
28 class RefCount;
29 class SGIO;
30 class Parallel;
31 class Parameter;
32 class ParameterMap;
33 class SGParamInfo;
34 class CSerializableFile;
35 
36 template <class T, class K> class CMap;
37 
38 struct TParameter;
39 template <class T> class DynArray;
40 template <class T> class SGStringList;
41 
42 /*******************************************************************************
43  * define reference counter macros
44  ******************************************************************************/
45 
46 #ifdef USE_REFERENCE_COUNTING
47 #define SG_REF(x) { if (x) (x)->ref(); }
48 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=NULL; } }
49 #define SG_UNREF_NO_NULL(x) { if (x) { (x)->unref(); } }
50 #else
51 #define SG_REF(x)
52 #define SG_UNREF(x)
53 #define SG_UNREF_NO_NULL(x)
54 #endif
55 
56 /*******************************************************************************
57  * Macros for registering parameters/model selection parameters
58  ******************************************************************************/
59 
60 #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
61 #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
62 
63 #define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
64 #define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__)
65 #define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
66 
67 #define SG_ADD4(param, name, description, ms_available) {\
68  m_parameters->add(param, name, description);\
69  if (ms_available)\
70  m_model_selection_parameters->add(param, name, description);\
71 }
72 
73 #define SG_ADD5(param, name, description, ms_available, gradient_available) {\
74  m_parameters->add(param, name, description);\
75  if (ms_available)\
76  m_model_selection_parameters->add(param, name, description);\
77  if (gradient_available)\
78  m_gradient_parameters->add(param, name, description);\
79 }
80 
81 #define SG_ADD(...) VARARG(SG_ADD, __VA_ARGS__)
82 
83 /*******************************************************************************
84  * End of macros for registering parameters/model selection parameters
85  ******************************************************************************/
86 
91 };
92 
95 {
98 };
99 
113 {
114 public:
116  CSGObject();
117 
119  CSGObject(const CSGObject& orig);
120 
122  virtual ~CSGObject();
123 
124 #ifdef USE_REFERENCE_COUNTING
125 
129  int32_t ref();
130 
135  int32_t ref_count();
136 
142  int32_t unref();
143 #endif //USE_REFERENCE_COUNTING
144 
145 #ifdef TRACE_MEMORY_ALLOCS
146  static void list_memory_allocs();
147 #endif
148 
152  virtual CSGObject *shallow_copy() const;
153 
157  virtual CSGObject *deep_copy() const;
158 
164  virtual const char* get_name() const = 0;
165 
174  virtual bool is_generic(EPrimitiveType* generic) const;
175 
178  template<class T> void set_generic();
179 
184  void unset_generic();
185 
190  virtual void print_serializable(const char* prefix="");
191 
199  virtual bool save_serializable(CSerializableFile* file,
200  const char* prefix="");
201 
211  virtual bool load_serializable(CSerializableFile* file,
212  const char* prefix="");
213 
218  void set_global_io(SGIO* io);
219 
224  SGIO* get_global_io();
225 
231 
237 
243 
249 
253 
255  void print_modsel_params();
256 
263  char* get_modsel_param_descr(const char* param_name);
264 
271  index_t get_modsel_param_index(const char* param_name);
272 
280 
281 protected:
289  virtual void load_serializable_pre() throw (ShogunException);
290 
298  virtual void load_serializable_post() throw (ShogunException);
299 
307  virtual void save_serializable_pre() throw (ShogunException);
308 
316  virtual void save_serializable_post() throw (ShogunException);
317 
318 public:
320  virtual void update_parameter_hash();
321 
325  virtual bool parameter_hash_changed();
326 
339  virtual bool equals(CSGObject* other, float64_t accuracy=0.0, bool tolerant=false);
340 
349  virtual CSGObject* clone();
350 
351 private:
352  void set_global_objects();
353  void unset_global_objects();
354  void init();
355 
364  void get_parameter_incremental_hash(uint32_t& hash, uint32_t& carry,
365  uint32_t& total_length);
366 
367 public:
370 
373 
376 
379 
382 
385 
387  uint32_t m_hash;
388 
389 private:
390 
391  EPrimitiveType m_generic;
392  bool m_load_pre_called;
393  bool m_load_post_called;
394  bool m_save_pre_called;
395  bool m_save_post_called;
396 
397  RefCount* m_refcount;
398 };
399 }
400 #endif // __SGOBJECT_H__
virtual const char * get_name() const =0
SGStringList< char > get_modelsel_names()
Definition: SGObject.cpp:498
template class SGStringList
Definition: SGObject.h:40
Parallel * get_global_parallel()
Definition: SGObject.cpp:277
virtual void update_parameter_hash()
Definition: SGObject.cpp:248
int32_t index_t
Definition: common.h:62
virtual CSGObject * clone()
Definition: SGObject.cpp:714
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual CSGObject * shallow_copy() const
Definition: SGObject.cpp:192
void unset_generic()
Definition: SGObject.cpp:303
Version * get_global_version()
Definition: SGObject.cpp:290
parameter struct
virtual void save_serializable_pre()
Definition: SGObject.cpp:431
virtual bool is_generic(EPrimitiveType *generic) const
Definition: SGObject.cpp:296
EModelSelectionAvailability
Definition: SGObject.h:88
Parameter * m_parameters
Definition: SGObject.h:378
virtual bool load_serializable(CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:369
Parallel * parallel
Definition: SGObject.h:372
char * get_modsel_param_descr(const char *param_name)
Definition: SGObject.cpp:522
Parameter class.
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
virtual ~CSGObject()
Definition: SGObject.cpp:139
void build_gradient_parameter_dictionary(CMap< TParameter *, CSGObject * > *dict)
Definition: SGObject.cpp:597
Template Dynamic array class that creates an array that can be used like a list or an array...
Definition: DynArray.h:32
double float64_t
Definition: common.h:50
Version * version
Definition: SGObject.h:375
EGradientAvailability
Definition: SGObject.h:94
virtual void save_serializable_post()
Definition: SGObject.cpp:436
void print_modsel_params()
Definition: SGObject.cpp:474
Class Version provides version information.
Definition: Version.h:28
Parameter * m_model_selection_parameters
Definition: SGObject.h:381
virtual bool equals(CSGObject *other, float64_t accuracy=0.0, bool tolerant=false)
Definition: SGObject.cpp:618
virtual CSGObject * deep_copy() const
Definition: SGObject.cpp:198
void set_global_parallel(Parallel *parallel)
Definition: SGObject.cpp:241
virtual void load_serializable_pre()
Definition: SGObject.cpp:421
virtual void load_serializable_post()
Definition: SGObject.cpp:426
Class Parallel provides helper functions for multithreading.
Definition: Parallel.h:27
virtual bool save_serializable(CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:314
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
SGIO * get_global_io()
Definition: SGObject.cpp:235
index_t get_modsel_param_index(const char *param_name)
Definition: SGObject.cpp:535
void set_global_io(SGIO *io)
Definition: SGObject.cpp:228
uint32_t m_hash
Definition: SGObject.h:387
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:243
Parameter * m_gradient_parameters
Definition: SGObject.h:384
virtual void print_serializable(const char *prefix="")
Definition: SGObject.cpp:308
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:262
void set_global_version(Version *version)
Definition: SGObject.cpp:283
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:36

SHOGUN Machine Learning Toolbox - Documentation