NSVD Reader  0.0.1
value.hpp
Go to the documentation of this file.
1 
5 /*
6  * These codes are licensed under CC0.
7  * http://creativecommons.org/publicdomain/zero/1.0/
8  */
9 #ifndef __NODAMUSHI_SVD_VALUE_HPP__
10 #define __NODAMUSHI_SVD_VALUE_HPP__
11 
12 # include <utility>
13 # include <type_traits>
14 # include <functional>
15 # include <ostream>
16 # include <sstream>
17 # include <stdexcept>
18 
21 # include "nodamushi/svd/vattr.hpp"
23 
24 namespace nodamushi{
25 namespace svd{
26 
30 struct empty_value_access_error:public std::logic_error
31 {
32  empty_value_access_error(std::string& message):logic_error(message){}
33 };
34 
35 
53 template<typename T,bool attribute,bool required,char... name>struct value
54 {
55  using this_t = value<T,attribute,required,name...>;
57  using type = typename value_type_t::type;
58 
59  static constexpr bool REQUIRED=required;
60  static constexpr bool ATTRIBUTE=attribute;
61 
62  // constructors ------------------------------------------------
64  value():_value(),_has_value(false){};
66  value(const type& t):_value(t),_has_value(true){}
68  value(value&&) = default;
69 
70  // constant properties -----------------------------------------
72  constexpr bool is_required()const noexcept{return required;}
74  constexpr bool is_attribute()const noexcept{return attribute;}
76  NODAMUSHI_CONSTEXPR_STRING get_name()const noexcept;
77 
78  // contents ----------------------------------------------------
79  // setter
84  template<typename SVD> auto operator =(SVD& src)
85  ->typename std::enable_if<std::is_base_of<svd_reader,SVD>::value
86  ,bool>::type
87  {
88  if(::nodamushi::svd::value_try_set(_value,src)){
89  _has_value = true;
90  return true;
91  }
92  return false;
93  }
98  template<typename SVD> auto operator =(SVD&& src)
99  ->typename std::enable_if<std::is_base_of<svd_reader,SVD>::value
100  ,bool>::type
101  {
102  if(::nodamushi::svd::value_try_set(_value,src)){
103  _has_value = true;
104  return true;
105  }
106  return false;
107  }
113  template<typename V> auto operator =(const V& value)
114  ->typename std::enable_if<!std::is_base_of<svd_reader,V>::value
115  ,this_t&>::type
116  {
117  _value = value; _has_value =true;
118  return *this;
119  }
125  template<typename V> this_t& operator =(V&& value)
126  {
127  _value = std::move(value); _has_value =true;
128  return *this;
129  }
134  void clear()noexcept;
138  value& operator=(value&&)=default;
142  value& operator=(const value&)=default;
143 
144 
145 
146  // getter (empty_value_access_error will be occurer)
152  const type& get()const;
158  type& get();
164  type& operator*(){return get();}
170  const type& operator*()const {return get();}
176  type* operator->(){return &get();}
182  const type* operator->()const{return &get();}
189  bool operator==(const type& t)const noexcept{return _has_value? _value ==t:false;}
196  bool operator!=(const type& t)const noexcept{return _has_value? _value !=t:true;}
203  bool operator==(type&& t)const noexcept{return _has_value? _value ==t:false;}
210  bool operator!=(type&& t)const noexcept{return _has_value? _value !=t:true;}
219  bool operator==(const value& t)const noexcept
220  {return _has_value&&t._has_value? _value ==t._value:_has_value==t._has_value;}
229  bool operator!=(const value&& t)const noexcept
230  {return _has_value&&t._has_value? _value !=t._value:_has_value!=t._has_value;}
231 
237  type& get_for_init()noexcept{_has_value=true;return _value;}
243  type& get(type& defaultValue)noexcept{return _has_value?_value:defaultValue;}
249  type& get(type&& defaultValue)noexcept{return _has_value?_value:defaultValue;}
255  const type& get(const type& defaultValue)const noexcept{return _has_value?_value:defaultValue;}
256 
257 
258 
263  bool empty()const noexcept{return !_has_value;}
268  operator bool()const noexcept{return _has_value;}
269 
270 
276  bool check_require()const noexcept{return !required || _has_value;}
277 
278  private:
279  type _value;
280  bool _has_value;
281 };
282 
283 }}// end namespace nodamushi::svd
284 
285 # ifndef __DOXYGEN__DOCUMENT__PREPROCESSOR__
286 
287 # define SVD_VALUE(type,name,...) \
288  _SVD_VALUE_(type,name,false,__VA_ARGS__,_SVD_VALUE0_,_SVD_VALUE1_,0)
289 
290 # define SVD_ATTR(type,name,...) \
291  _SVD_VALUE_(type,name,true,__VA_ARGS__,_SVD_VALUE0_,_SVD_VALUE1_,0)
292 
293 # define SVD_TYPE(type,attr,require,name) \
294  value_t<type,attr,require,__SVD_VALUE_EXPAND_NAME__(name)>
295 
299 
300 # else
301 // for doxygen
302 
303 # define SVD_VALUE(type,name,...) \
304  value<type,ELEMENT> name
305 
306 # define SVD_ATTR(type,name,...) \
307  value<type,ATTR> name
308 
309 # define SVD_TYPE(type,attr,require,name) \
310  value<type,ATTR>
311 
312 # endif
313 
314 
315 #endif // __NODAMUSHI_SVD_VALUE_HPP__
value(const type &t)
constructor with initial value
Definition: value.hpp:66
bool empty() const noexcept
when this object is empty,return true.
Definition: value.hpp:263
value()
default constructor
Definition: value.hpp:64
type & get(type &&defaultValue) noexcept
get or else
Definition: value.hpp:249
const type & get(const type &defaultValue) const noexcept
get or else
Definition: value.hpp:255
macros for value
value<void>
constexpr bool is_required() const noexcept
Definition: value.hpp:72
bool operator!=(const value &&t) const noexcept
value != t
Definition: value.hpp:229
type & get_for_init() noexcept
Get value to initialize. After this method is called, empty() method returns false.
Definition: value.hpp:237
constexpr bool is_attribute() const noexcept
Definition: value.hpp:74
Definition: Access.hpp:143
bool check_require() const noexcept
Check not empty if this element/attribute is required.
Definition: value.hpp:276
const type * operator->() const
->
Definition: value.hpp:182
value_setter imple
auto operator=(SVD &src) -> typename std::enable_if< std::is_base_of< svd_reader, SVD >::value, bool >::type
set value from SVD reader
Definition: value.hpp:84
type * operator->()
->
Definition: value.hpp:176
bool operator==(const type &t) const noexcept
value == t
Definition: value.hpp:189
bool value_try_set(DST &dst, SVD &src)
bool operator==(const value &t) const noexcept
value == t
Definition: value.hpp:219
bool operator!=(type &&t) const noexcept
value != t
Definition: value.hpp:210
This class reperesents SVD(xml) element / attribute.
Definition: value.hpp:53
static constexpr bool ATTRIBUTE
Definition: value.hpp:60
type & get(type &defaultValue) noexcept
get or else
Definition: value.hpp:243
std::string get_name() const noexcept
Definition: imple.hpp:66
#define NODAMUSHI_CONSTEXPR_STRING
Definition: string_type.hpp:46
bool operator==(type &&t) const noexcept
value == t
Definition: value.hpp:203
value imple
value attriubte
constexpr small library
svd_reader
type & operator *()
get value
Definition: value.hpp:164
void clear() noexcept
clear this object. After calling this method,this object status will be empty.
Definition: imple.hpp:87
empty_value_access_error(std::string &message)
Definition: value.hpp:32
access to the empty value object.
Definition: value.hpp:30
static constexpr bool REQUIRED
Definition: value.hpp:59
bool operator!=(const type &t) const noexcept
value != t
Definition: value.hpp:196
const type & get() const
get value
Definition: imple.hpp:76