Tapkee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
value_keeper.hpp
Go to the documentation of this file.
1 /* This software is distributed under BSD 3-clause license (see LICENSE file).
2  *
3  * Copyright (c) 2012-2013 Sergey Lisitsyn
4  */
5 
6 #ifndef TAPKEE_VALUE_KEEPER_H_
7 #define TAPKEE_VALUE_KEEPER_H_
8 
9 /* Tapkee includes */
11 /* End of Tapkee includes */
12 
13 namespace tapkee
14 {
15 namespace tapkee_internal
16 {
17 
18 struct EmptyType
19 {
20 };
21 
23 {
24 
25 public:
26  template <typename T>
27  explicit ValueKeeper(const T& value) :
29  {
30  policy->copyFromValue(&value, &value_ptr);
31  }
32 
35  {
36  }
37 
39  {
41  }
42 
44  {
46  }
47 
49  {
51  policy = v.policy;
52  checker = v.checker;
54  return *this;
55  }
56 
57  template <typename T>
58  inline T getValue() const
59  {
60  T* v;
61  if (!isInitialized())
62  {
63  throw missed_parameter_error("Parameter is missed");
64  }
65  if (isTypeCorrect<T>())
66  {
67  void* vv = policy->getValue(const_cast<void**>(&value_ptr));
68  v = reinterpret_cast<T*>(vv);
69  }
70  else
71  throw wrong_parameter_type_error("Wrong value type");
72  return *v;
73  }
74 
75  template <typename T>
76  inline bool isTypeCorrect() const
77  {
78  return getPolicy<T>() == policy;
79  }
80 
81  inline bool isInitialized() const
82  {
83  return getPolicy<EmptyType>() != policy;
84  }
85 
86  template <typename T>
87  inline bool inRange(T lower, T upper) const
88  {
89  if (!isTypeCorrect<T>() && isInitialized())
90  throw std::domain_error("Wrong range bounds type");
91  return checker->isInRange(&value_ptr,&lower,&upper);
92  }
93 
94  template <typename T>
95  inline bool equal(T value) const
96  {
97  if (!isTypeCorrect<T>() && isInitialized())
98  throw std::domain_error("Wrong equality value type");
99  return checker->isEqual(&value_ptr,&value);
100  }
101 
102  template <typename T>
103  inline bool notEqual(T value) const
104  {
105  if (!isTypeCorrect<T>() && isInitialized())
106  throw std::domain_error("Wrong non-equality value type");
107  return checker->isNotEqual(&value_ptr,&value);
108  }
109 
110  inline bool positive() const
111  {
112  return checker->isPositive(&value_ptr);
113  }
114 
115  inline bool nonNegative() const
116  {
117  return checker->isNonNegative(&value_ptr);
118  }
119 
120  inline bool negative() const
121  {
122  return checker->isNegative(&value_ptr);
123  }
124 
125  inline bool nonPositive() const
126  {
127  return checker->isNonPositive(&value_ptr);
128  }
129 
130  template <typename T>
131  inline bool greater(T lower) const
132  {
133  if (!isTypeCorrect<T>() && isInitialized())
134  throw std::domain_error("Wrong greater check bound type");
135  return checker->isGreater(&value_ptr,&lower);
136  }
137 
138  template <typename T>
139  inline bool lesser(T upper) const
140  {
141  if (!isTypeCorrect<T>() && isInitialized())
142  throw std::domain_error("Wrong lesser check bound type");
143  return checker->isLesser(&value_ptr,&upper);
144  }
145 
146 private:
147 
150  void* value_ptr;
151 
152 };
153 
154 }
155 }
156 #endif
virtual bool isLesser(void *const *, void *) const =0
virtual void free(void **) const =0
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...
Definition: exceptions.hpp:37
TypePolicyBase * getPolicy()
Definition: policy.hpp:55
virtual bool isNegative(void *const *) const =0
An exception type that is thrown in case if wrong parameter value is passed.
Definition: exceptions.hpp:27
virtual bool isNotEqual(void *const *, void *) const =0
virtual bool isNonPositive(void *const *) const =0
virtual void clone(void *const *, void **) const =0
virtual void copyFromValue(void const *, void **) const =0
virtual bool isNonNegative(void *const *) const =0
virtual bool isPositive(void *const *) const =0
bool inRange(T lower, T upper) const
ValueKeeper & operator=(const ValueKeeper &v)
virtual bool isInRange(void *const *, void *, void *) const =0
CheckerPolicyBase * getCheckerPolicy()
Definition: policy.hpp:180
virtual bool isEqual(void *const *, void *) const =0
virtual bool isGreater(void *const *, void *) const =0