Tapkee
value_keeper.hpp
Go to the documentation of this file.
1 
29 #ifndef STICHWORT_KEEPER_H_
30 #define STICHWORT_KEEPER_H_
31 
32 #include <stichwort/policy.hpp>
33 #include <stichwort/exceptions.hpp>
34 
35 namespace stichwort
36 {
37 namespace stichwort_internal
38 {
39 
40 struct EmptyType
41 {
42 };
43 
45 {
46 
47 public:
48  template <typename T>
49  explicit ValueKeeper(const T& value) :
50  policy(getPolicy<T>()), value_ptr(NULL)
51  {
52  policy->copyFromValue(&value, &value_ptr);
53  }
54 
57  {
58  }
59 
61  {
63  }
64 
66  {
68  }
69 
71  {
73  policy = v.policy;
75  return *this;
76  }
77 
78  template <typename T>
79  inline T getValue() const
80  {
81  T* v;
82  if (!isInitialized())
83  throw missed_parameter_error("Parameter is missed");
84 
85  if (isTypeCorrect<T>())
86  {
87  void* vv = policy->getValue(const_cast<void**>(&value_ptr));
88  v = reinterpret_cast<T*>(vv);
89  }
90  else
91  throw wrong_parameter_type_error("Wrong value type");
92  return *v;
93  }
94 
95  template <typename T>
96  inline bool isTypeCorrect() const
97  {
98  return getPolicy<T>() == policy;
99  }
100 
101  inline bool isInitialized() const
102  {
103  return getPolicy<EmptyType>() != policy;
104  }
105 
106  template <template<class> class F, class Q>
107  inline bool isCondition(F<Q> cond) const
108  {
109  Q value = getValue<Q>();
110  return cond(value);
111  }
112 
113  inline std::string repr() const
114  {
115  return policy->repr(const_cast<void**>(&value_ptr));
116  }
117 
118 private:
119 
121  void* value_ptr;
122 
123 };
124 
125 }
126 }
127 #endif
virtual void * getValue(void **) const =0
An exception type that is thrown in case of missed parameter, i.e. when some required parameter is no...
TypePolicyBase * getPolicy()
Definition: policy.hpp:144
The namespace that contains implementations for the keywords.
virtual void copyFromValue(void const *, void **) const =0
ValueKeeper & operator=(const ValueKeeper &v)
virtual std::string repr(void **) const =0
An exception type that is thrown in case if wrong parameter value is passed.
virtual void free(void **) const =0
virtual void clone(void *const *, void **) const =0