SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
some.h
Go to the documentation of this file.
1 #ifndef __SG_SOME_H__
2 #define __SG_SOME_H__
3 
4 #include <shogun/lib/config.h>
5 
6 #ifdef HAVE_CXX11
7 #include <memory>
8 
9 #include <shogun/base/SGObject.h>
10 
11 namespace shogun
12 {
13 
20  template <typename T>
21  class Some
22  {
23  public:
24  Some(const Some<T>& other);
25  explicit Some(T* other);
26  Some& operator=(T* other);
27  ~Some();
28 
29  static Some<T> from_raw(T* raw);
30 
37  operator T*();
42  T* operator->();
43  private:
44  Some();
45  void unref();
46  void ref();
47  private:
48  T* raw;
49  };
50 
51  template <typename T>
52  Some<T>::Some()
53  : raw(nullptr)
54  {
55  }
56  template <typename T>
57  Some<T>::Some(const Some<T>& other)
58  : raw(other.raw)
59  {
60  ref();
61  }
62  template <typename T>
63  Some<T>::Some(T* other)
64  : raw(other)
65  {
66  ref();
67  }
68  template <typename T>
69  Some<T>& Some<T>::operator=(T* other)
70  {
71  if (raw != other) {
72  unref();
73  raw = other;
74  ref();
75  }
76  return *this;
77  }
78  template <typename T>
79  Some<T>::~Some()
80  {
81  unref();
82  }
83  template <typename T>
84  Some<T>::operator T*()
85  {
86  return raw;
87  }
88  template <typename T>
89  T* Some<T>::operator->()
90  {
91  return raw;
92  }
93  template <typename T>
94  void Some<T>::ref()
95  {
96  SG_REF(raw);
97  }
98  template <typename T>
99  void Some<T>::unref()
100  {
101  SG_UNREF(raw);
102  }
103  template <typename T>
104  Some<T> Some<T>::from_raw(T* raw)
105  {
106  Some<T> result(raw);
107  return result;
108  }
109 
120  template <typename T, class... Args>
121  Some<T> some(Args&&... args)
122  {
123  T* ptr = new T(args...);
124  return Some<T>::from_raw(ptr);
125  }
126 
127  template <class T>
128  inline T wrap(const T& value)
129  {
130  return value;
131  }
132 
133  template <class T>
134  inline Some<T> wrap(T* ptr)
135  {
136  return Some<T>::from_raw(ptr);
137  }
138 
139  template <class T>
140  inline Some<T> wrap(const Some<T>& other)
141  {
142  return other;
143  }
144 
145 };
146 
147 #endif /* HAVE_CXX11 */
148 #endif /* __SG_SOME_H__ */
#define SG_REF(x)
Definition: SGObject.h:54
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18

SHOGUN Machine Learning Toolbox - Documentation