Tapkee
Main Page
Namespaces
Classes
Files
File List
File Members
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 */
10
#include <
tapkee/parameters/policy.hpp
>
11
/* End of Tapkee includes */
12
13
namespace
tapkee
14
{
15
namespace
tapkee_internal
16
{
17
18
struct
EmptyType
19
{
20
};
21
22
class
ValueKeeper
23
{
24
25
public
:
26
template
<
typename
T>
27
explicit
ValueKeeper
(
const
T& value) :
28
policy
(
getPolicy
<T>()),
checker
(
getCheckerPolicy
<T>()),
value_ptr
(NULL)
29
{
30
policy
->
copyFromValue
(&value, &
value_ptr
);
31
}
32
33
ValueKeeper
() :
34
policy
(
getPolicy
<
EmptyType
>()),
checker
(
getCheckerPolicy
<
EmptyType
>()),
value_ptr
(NULL)
35
{
36
}
37
38
~ValueKeeper
()
39
{
40
policy
->
free
(&
value_ptr
);
41
}
42
43
ValueKeeper
(
const
ValueKeeper
& v) :
policy
(v.
policy
),
checker
(v.
checker
),
value_ptr
(NULL)
44
{
45
policy
->
clone
(&(v.
value_ptr
), &
value_ptr
);
46
}
47
48
ValueKeeper
&
operator=
(
const
ValueKeeper
& v)
49
{
50
policy
->
free
(&
value_ptr
);
51
policy
= v.
policy
;
52
checker
= v.
checker
;
53
policy
->
clone
(&(v.
value_ptr
), &
value_ptr
);
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
148
TypePolicyBase
*
policy
;
149
CheckerPolicyBase
*
checker
;
150
void
*
value_ptr
;
151
152
};
153
154
}
155
}
156
#endif
include
tapkee
parameters
value_keeper.hpp
Generated by
1.8.3.1