SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
maybe.h
浏览该文件的文档.
1 #ifndef __SG_MAYBE_H__
2 #define __SG_MAYBE_H__
3 
5 
6 namespace shogun
7 {
8 
9  namespace detail
10  {
11  static const char* NO_REASON_FOR_ABSENCE = "AVAILABLE";
12  }
13 
21  class Nothing
22  {
23  public:
28  Nothing(const char* reason = "no specific reason") :
29  m_absence_reason(reason)
30  {
31  }
36  Nothing(const Nothing& other) :
38  {
39  }
43  const char* m_absence_reason;
44  };
45 
85  template <typename T>
86  class Maybe
87  {
88  public:
94  Maybe(const Nothing& nothing) :
95  m_value(),
96  m_absence_reason(nothing.m_absence_reason)
97  {
98  }
103  Maybe(const Maybe<T>& other) :
104  m_value(other.m_value),
105  m_absence_reason(other.m_absence_reason)
106  {
107  }
108 
113  inline operator bool() const
114  {
115  return is_present();
116  }
117 
122  inline T& value()
123  {
124  return *get();
125  }
126 
131  inline T& value_or(T& v)
132  {
133  if (is_present())
134  return *get();
135  else
136  return v;
137  }
138 
143  inline bool is_absent() const
144  {
145  return m_absence_reason != detail::NO_REASON_FOR_ABSENCE;
146  }
147 
152  inline bool is_present() const
153  {
154  return m_absence_reason == detail::NO_REASON_FOR_ABSENCE;
155  }
156 
157  private:
158  inline T* get()
159  {
160  if (is_present())
161  return &m_value;
162  else
163  throw ShogunException("Tried to access inexistent object");
164  }
165  Maybe();
166  Maybe(const char* reason, bool) :
167  m_value(),
168  m_absence_reason(reason)
169  {
170  }
171  Maybe(const T& someValue) :
172  m_value(someValue),
173  m_absence_reason(detail::NO_REASON_FOR_ABSENCE)
174  {
175  }
176 
177  public:
178  template <typename Q>
179  friend Maybe<Q> Just(const Q& value);
180 
181  private:
182  T m_value;
183  const char* m_absence_reason;
184  };
185 
193  template <typename T>
194  static Maybe<T> Just(const T& value)
195  {
196  return Maybe<T>(value);
197  }
198 
199 }
200 #endif
static Maybe< T > Just(const T &value)
Definition: maybe.h:194
static const char * NO_REASON_FOR_ABSENCE
Definition: maybe.h:11
bool is_present() const
Definition: maybe.h:152
T & value_or(T &v)
Definition: maybe.h:131
T & value()
Definition: maybe.h:122
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
Maybe(const Nothing &nothing)
Definition: maybe.h:94
Maybe(const Maybe< T > &other)
Definition: maybe.h:103
const char * m_absence_reason
Definition: maybe.h:43
friend Maybe< Q > Just(const Q &value)
Nothing(const Nothing &other)
Definition: maybe.h:36
bool is_absent() const
Definition: maybe.h:143
Holder that represents an object that can be either present or absent. Quite simllar to std::optional...
Definition: maybe.h:86
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Nothing(const char *reason="no specific reason")
Definition: maybe.h:28

SHOGUN 机器学习工具包 - 项目文档