SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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 #include <shogun/lib/common.h>
18 #include <shogun/lib/DataType.h>
20 #include <shogun/base/Version.h>
21 #include <shogun/base/unique.h>
22 #include <shogun/io/SGIO.h>
23 #include <shogun/lib/tag.h>
24 #include <shogun/lib/any.h>
25 
29 namespace shogun
30 {
31 class RefCount;
32 class SGIO;
33 class Parallel;
34 class Parameter;
35 class CSerializableFile;
36 
37 template <class T, class K> class CMap;
38 
39 struct TParameter;
40 template <class T> class DynArray;
41 template <class T> class SGStringList;
42 
43 /*******************************************************************************
44  * define reference counter macros
45  ******************************************************************************/
46 
47 #ifdef USE_REFERENCE_COUNTING
48 #define SG_REF(x) { if (x) (x)->ref(); }
49 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=NULL; } }
50 #define SG_UNREF_NO_NULL(x) { if (x) { (x)->unref(); } }
51 #else
52 #define SG_REF(x)
53 #define SG_UNREF(x)
54 #define SG_UNREF_NO_NULL(x)
55 #endif
56 
57 /*******************************************************************************
58  * Macros for registering parameters/model selection parameters
59  ******************************************************************************/
60 
61 #ifdef _MSC_VER
62 
63 #define VA_NARGS(...) INTERNAL_EXPAND_ARGS_PRIVATE(INTERNAL_ARGS_AUGMENTER(__VA_ARGS__))
64 #define INTERNAL_ARGS_AUGMENTER(...) unused, __VA_ARGS__
65 #define INTERNAL_EXPAND(x) x
66 #define INTERNAL_EXPAND_ARGS_PRIVATE(...) INTERNAL_EXPAND(INTERNAL_GET_ARG_COUNT_PRIVATE(__VA_ARGS__, 5, 4, 3, 2, 1, 0))
67 #define INTERNAL_GET_ARG_COUNT_PRIVATE(_0_, _1_, _2_, _3_, _4_, _5_, count, ...) count
68 
69 #else
70 
71 #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
72 #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
73 
74 #endif
75 
76 #define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
77 #define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__)
78 #define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
79 
80 #define SG_ADD4(param, name, description, ms_available) {\
81  m_parameters->add(param, name, description);\
82  if (ms_available)\
83  m_model_selection_parameters->add(param, name, description);\
84 }
85 
86 #define SG_ADD5(param, name, description, ms_available, gradient_available) {\
87  m_parameters->add(param, name, description);\
88  if (ms_available)\
89  m_model_selection_parameters->add(param, name, description);\
90  if (gradient_available)\
91  m_gradient_parameters->add(param, name, description);\
92 }
93 
94 #define SG_ADD(...) VARARG(SG_ADD, __VA_ARGS__)
95 
96 /*******************************************************************************
97  * End of macros for registering parameters/model selection parameters
98  ******************************************************************************/
99 
104 };
105 
108 {
111 };
112 
126 {
127 public:
129  CSGObject();
130 
132  CSGObject(const CSGObject& orig);
133 
135  virtual ~CSGObject();
136 
137 #ifdef USE_REFERENCE_COUNTING
138 
142  int32_t ref();
143 
148  int32_t ref_count();
149 
155  int32_t unref();
156 #endif //USE_REFERENCE_COUNTING
157 
158 #ifdef TRACE_MEMORY_ALLOCS
159  static void list_memory_allocs();
160 #endif
161 
165  virtual CSGObject *shallow_copy() const;
166 
170  virtual CSGObject *deep_copy() const;
171 
177  virtual const char* get_name() const = 0;
178 
187  virtual bool is_generic(EPrimitiveType* generic) const;
188 
191  template<class T> void set_generic();
192 
197  void unset_generic();
198 
203  virtual void print_serializable(const char* prefix="");
204 
212  virtual bool save_serializable(CSerializableFile* file,
213  const char* prefix="");
214 
224  virtual bool load_serializable(CSerializableFile* file,
225  const char* prefix="");
226 
231  void set_global_io(SGIO* io);
232 
237  SGIO* get_global_io();
238 
244 
250 
256 
262 
266 
268  void print_modsel_params();
269 
276  char* get_modsel_param_descr(const char* param_name);
277 
284  index_t get_modsel_param_index(const char* param_name);
285 
293 
299  bool has(const std::string& name) const
300  {
301  return type_erased_has(BaseTag(name));
302  }
303 
309  template <typename T>
310  bool has(const Tag<T>& tag) const
311  {
312  return has<T>(tag.name());
313  }
314 
320  template <typename T, typename U=void>
321  bool has(const std::string& name) const
322  {
323  BaseTag tag(name);
324  if(!type_erased_has(tag))
325  return false;
326  const Any value = type_erased_get(tag);
327  return value.same_type<T>();
328  }
329 
336  template <typename T>
337  void set(const Tag<T>& _tag, const T& value)
338  {
339  if(type_erased_has(_tag))
340  {
341  if(has<T>(_tag.name()))
342  type_erased_set(_tag, erase_type(value));
343  else
344  {
345  SG_ERROR("Type for parameter with name \"%s\" is not correct.\n",
346  _tag.name().c_str());
347  }
348  }
349  else
350  {
351  SG_ERROR("\"%s\" does not have a parameter with name \"%s\".\n",
352  get_name(), _tag.name().c_str());
353  }
354  }
355 
362  template <typename T, typename U=void>
363  void set(const std::string& name, const T& value)
364  {
365  Tag<T> tag(name);
366  set(tag, value);
367  }
368 
375  template <typename T>
376  T get(const Tag<T>& _tag) const
377  {
378  const Any value = type_erased_get(_tag);
379  try
380  {
381  return recall_type<T>(value);
382  }
383  catch (const std::logic_error&)
384  {
385  SG_ERROR("Type for parameter with name \"%s\" is not correct in \"%s\".\n",
386  _tag.name().c_str(), get_name());
387  }
388  // we won't be there
389  return recall_type<T>(value);
390  }
391 
398  template <typename T, typename U=void>
399  T get(const std::string& name) const
400  {
401  Tag<T> tag(name);
402  return get(tag);
403  }
404 
405 protected:
413  virtual void load_serializable_pre() throw (ShogunException);
414 
422  virtual void load_serializable_post() throw (ShogunException);
423 
431  virtual void save_serializable_pre() throw (ShogunException);
432 
440  virtual void save_serializable_post() throw (ShogunException);
441 
449  template <typename T>
450  void register_param(Tag<T>& _tag, const T& value)
451  {
452  type_erased_set(_tag, erase_type(value));
453  }
454 
462  template <typename T>
463  void register_param(const std::string& name, const T& value)
464  {
465  BaseTag tag(name);
466  type_erased_set(tag, erase_type(value));
467  }
468 
469 public:
471  virtual void update_parameter_hash();
472 
476  virtual bool parameter_hash_changed();
477 
490  virtual bool equals(CSGObject* other, float64_t accuracy=0.0, bool tolerant=false);
491 
500  virtual CSGObject* clone();
501 
502 protected:
503  /* Iteratively clones all parameters of the provided instance into this instance.
504  * This will fail if the objects have different sets of registered parameters,
505  * or if they have a different type as defined by get_name().
506  *
507  * @param other object whose parameters are to be cloned into this instance
508  * @return true if cloning was successful
509  */
510  bool clone_parameters(CSGObject* other);
511 
512 private:
513  void set_global_objects();
514  void unset_global_objects();
515  void init();
516 
524  bool type_erased_has(const BaseTag& _tag) const;
525 
532  void type_erased_set(const BaseTag& _tag, const Any& any);
533 
540  Any type_erased_get(const BaseTag& _tag) const;
541 
550  void get_parameter_incremental_hash(uint32_t& hash, uint32_t& carry,
551  uint32_t& total_length);
552 
553  class Self;
554  Unique<Self> self;
555 
556 public:
559 
562 
565 
568 
571 
574 
576  uint32_t m_hash;
577 
578 private:
579 
580  EPrimitiveType m_generic;
581  bool m_load_pre_called;
582  bool m_load_post_called;
583  bool m_save_pre_called;
584  bool m_save_post_called;
585 
586  RefCount* m_refcount;
587 };
588 }
589 #endif // __SGOBJECT_H__
virtual const char * get_name() const =0
SGStringList< char > get_modelsel_names()
Definition: SGObject.cpp:531
template class SGStringList
Definition: SGObject.h:41
Parallel * get_global_parallel()
Definition: SGObject.cpp:310
virtual void update_parameter_hash()
Definition: SGObject.cpp:281
int32_t index_t
Definition: common.h:72
virtual CSGObject * clone()
Definition: SGObject.cpp:729
Base class for all tags. This class stores name and not the type information for a shogun object...
Definition: basetag.h:48
bool clone_parameters(CSGObject *other)
Definition: SGObject.cpp:754
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual CSGObject * shallow_copy() const
Definition: SGObject.cpp:225
void register_param(const std::string &name, const T &value)
Definition: SGObject.h:463
void register_param(Tag< T > &_tag, const T &value)
Definition: SGObject.h:450
void unset_generic()
Definition: SGObject.cpp:336
Version * get_global_version()
Definition: SGObject.cpp:323
parameter struct
virtual void save_serializable_pre()
Definition: SGObject.cpp:464
#define SG_ERROR(...)
Definition: SGIO.h:128
virtual bool is_generic(EPrimitiveType *generic) const
Definition: SGObject.cpp:329
EModelSelectionAvailability
Definition: SGObject.h:101
Allows to store objects of arbitrary types by using a BaseAnyPolicy and provides a type agnostic API...
Definition: any.h:262
Parameter * m_parameters
Definition: SGObject.h:567
virtual bool load_serializable(CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:402
Parallel * parallel
Definition: SGObject.h:561
bool has(const std::string &name) const
Definition: SGObject.h:299
void set(const std::string &name, const T &value)
Definition: SGObject.h:363
std::string name() const
Definition: basetag.h:79
char * get_modsel_param_descr(const char *param_name)
Definition: SGObject.cpp:555
bool same_type() const
Definition: any.h:344
void set(const Tag< T > &_tag, const T &value)
Definition: SGObject.h:337
Parameter class.
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:125
virtual ~CSGObject()
Definition: SGObject.cpp:172
void build_gradient_parameter_dictionary(CMap< TParameter *, CSGObject * > *dict)
Definition: SGObject.cpp:630
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:60
Version * version
Definition: SGObject.h:564
EGradientAvailability
Definition: SGObject.h:107
virtual void save_serializable_post()
Definition: SGObject.cpp:469
void print_modsel_params()
Definition: SGObject.cpp:507
Acts as an identifier for a shogun object. It contains type information and name of the object...
Definition: tag.h:47
Class Version provides version information.
Definition: Version.h:28
Parameter * m_model_selection_parameters
Definition: SGObject.h:570
virtual bool equals(CSGObject *other, float64_t accuracy=0.0, bool tolerant=false)
Definition: SGObject.cpp:651
virtual CSGObject * deep_copy() const
Definition: SGObject.cpp:231
void set_global_parallel(Parallel *parallel)
Definition: SGObject.cpp:274
virtual void load_serializable_pre()
Definition: SGObject.cpp:454
virtual void load_serializable_post()
Definition: SGObject.cpp:459
Class Parallel provides helper functions for multithreading.
Definition: Parallel.h:27
virtual bool save_serializable(CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:347
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
bool has(const Tag< T > &tag) const
Definition: SGObject.h:310
SGIO * get_global_io()
Definition: SGObject.cpp:268
index_t get_modsel_param_index(const char *param_name)
Definition: SGObject.cpp:568
void set_global_io(SGIO *io)
Definition: SGObject.cpp:261
uint32_t m_hash
Definition: SGObject.h:576
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:242
Parameter * m_gradient_parameters
Definition: SGObject.h:573
virtual void print_serializable(const char *prefix="")
Definition: SGObject.cpp:341
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:295
void set_global_version(Version *version)
Definition: SGObject.cpp:316
Any erase_type(const T &v)
Definition: any.h:409
bool has(const std::string &name) const
Definition: SGObject.h:321
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:37

SHOGUN Machine Learning Toolbox - Documentation