NSVD Reader  0.0.1
nullable.hpp
Go to the documentation of this file.
1 
5 #ifndef __NODAMUSHI_SVD_NORMALIZE_NULLABLE_HPP__
6 #define __NODAMUSHI_SVD_NORMALIZE_NULLABLE_HPP__
7 
8 # include <utility>
9 # include <type_traits>
10 # include "nodamushi/svd/value.hpp"
11 
12 namespace nodamushi{
13 namespace svd{
14 namespace normalized{
15 
20 template<typename T>
21 struct nullable{
22 
23  static_assert(std::is_fundamental<T>::value
24  || std::is_enum<T>::value
25  ,"T must be fundamental or enum");
26 
27  nullable():t(),enable(false){};
28  nullable(T v):t(v),enable(true){};
29  nullable(T v,bool e):t(v),enable(e){};
30 
31  template<bool attribute,bool required,char... name>
32  nullable(const ::nodamushi::svd::value<T,attribute,required,name...> & v):
33  t(v.get(static_cast<T>(0))),enable(v){}
34 
35 
36  nullable(const nullable&) = default;
37  nullable(nullable&&) = default;
38  nullable& operator=(const nullable&) = default;
39  nullable& operator=(nullable&&) = default;
40 
41  operator bool()const noexcept{return enable;}
42  bool empty()const noexcept{return !enable;}
43 
46  {
47  t = v;
48  enable =true;
49  return *this;
50  }
51 
54  {
55  t = T{};
56  enable = false;
57  return *this;
58  }
59 
60  bool operator==(T v)const noexcept
61  {
62  return enable && t ==v;
63  }
64 
65  bool operator!=(T v)const noexcept
66  {
67  return !enable || t != v;
68  }
69 
70  T get(T defaultValue)const noexcept{return enable?t:defaultValue;}
71 
72  template<T FUNC()>
73  T get()const{return enable? t: FUNC();}
74 
75  template<typename SRC,T FUNC(const SRC&)>
76  T get(const SRC* src)const{return enable? t: FUNC(*src);}
77 
78  private:
79  T t;
80  bool enable;
81 };
82 
83 
84 }}}// end namespace nodamushi
85 
86 #endif // __NODAMUSHI_SVD_NORMALIZE_NULLABLE_HPP__
nullable(const ::nodamushi::svd::value< T, attribute, required, name... > &v)
Definition: nullable.hpp:32
bool empty() const noexcept
Definition: nullable.hpp:42
bool operator!=(T v) const noexcept
Definition: nullable.hpp:65
a data container like optional type.
Definition: nullable.hpp:21
T get(T defaultValue) const noexcept
Definition: nullable.hpp:70
bool operator==(T v) const noexcept
Definition: nullable.hpp:60
nullable & clear()
clear value
Definition: nullable.hpp:53
T get(const SRC *src) const
Definition: nullable.hpp:76
value class, empty_value_access_error class
nullable & operator=(const nullable &)=default