SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
201  virtual bool save_serializable(CSerializableFile* file,
202  const char* prefix="", int32_t param_version=Version::get_version_parameter());
203 
215  virtual bool load_serializable(CSerializableFile* file,
216  const char* prefix="", int32_t param_version=Version::get_version_parameter());
217 
232  int32_t file_version, CSerializableFile* file,
233  const char* prefix="");
234 
247  DynArray<TParameter*>* load_all_file_parameters(int32_t file_version,
248  int32_t current_version,
249  CSerializableFile* file, const char* prefix="");
250 
265  void map_parameters(DynArray<TParameter*>* param_base,
266  int32_t& base_version,
267  DynArray<const SGParamInfo*>* target_param_infos);
268 
273  void set_global_io(SGIO* io);
274 
279  SGIO* get_global_io();
280 
286 
292 
298 
304 
308 
310  void print_modsel_params();
311 
318  char* get_modsel_param_descr(const char* param_name);
319 
326  index_t get_modsel_param_index(const char* param_name);
327 
335 
336 protected:
356  virtual TParameter* migrate(DynArray<TParameter*>* param_base,
357  const SGParamInfo* target);
358 
381  virtual void one_to_one_migration_prepare(DynArray<TParameter*>* param_base,
382  const SGParamInfo* target, TParameter*& replacement,
383  TParameter*& to_migrate, char* old_name=NULL);
384 
392  virtual void load_serializable_pre() throw (ShogunException);
393 
401  virtual void load_serializable_post() throw (ShogunException);
402 
410  virtual void save_serializable_pre() throw (ShogunException);
411 
419  virtual void save_serializable_post() throw (ShogunException);
420 
421 public:
423  virtual void update_parameter_hash();
424 
428  virtual bool parameter_hash_changed();
429 
442  virtual bool equals(CSGObject* other, float64_t accuracy=0.0, bool tolerant=false);
443 
452  virtual CSGObject* clone();
453 
454 private:
455  void set_global_objects();
456  void unset_global_objects();
457  void init();
458 
464  bool is_param_new(const SGParamInfo param_info) const;
465 
474  bool save_parameter_version(CSerializableFile* file, const char* prefix="",
475  int32_t param_version=Version::get_version_parameter());
476 
480  int32_t load_parameter_version(CSerializableFile* file,
481  const char* prefix="");
482 
491  void get_parameter_incremental_hash(uint32_t& hash, uint32_t& carry,
492  uint32_t& total_length);
493 
494 public:
497 
500 
503 
506 
509 
512 
515 
517  uint32_t m_hash;
518 
519 private:
520 
521  EPrimitiveType m_generic;
522  bool m_load_pre_called;
523  bool m_load_post_called;
524  bool m_save_pre_called;
525  bool m_save_post_called;
526 
527  RefCount* m_refcount;
528 };
529 }
530 #endif // __SGOBJECT_H__

SHOGUN Machine Learning Toolbox - Documentation