1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
1 |
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
3 |
*
|
|
4 |
* Copyright (C) 2010 Monty Taylor
|
|
5 |
*
|
|
6 |
* This program is free software; you can redistribute it and/or modify
|
|
7 |
* it under the terms of the GNU General Public License as published by
|
|
8 |
* the Free Software Foundation; version 2 of the License.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 |
*/
|
|
19 |
||
20 |
#ifndef DRIZZLED_CONSTRAINED_VALUE_H
|
|
21 |
#define DRIZZLED_CONSTRAINED_VALUE_H
|
|
22 |
||
23 |
#include <boost/program_options.hpp> |
|
1863.1.8
by Monty Taylor
Use invalid_option_value exception directly. More version neutral. |
24 |
#include <boost/program_options/errors.hpp> |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
25 |
#include <iostream> |
26 |
||
27 |
namespace drizzled |
|
28 |
{
|
|
29 |
||
1863.1.10
by Monty Taylor
Predeclare friend operators for Sun Studio. |
30 |
template<class T> class constrained_value; |
31 |
template<class T> |
|
32 |
std::istream& operator>>(std::istream& is, constrained_value<T>& bound_val); |
|
33 |
template<class T> |
|
34 |
std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v); |
|
35 |
||
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
36 |
template<class T> |
37 |
class constrained_value |
|
38 |
{
|
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
39 |
T m_val; |
40 |
protected: |
|
41 |
||
42 |
virtual constrained_value<T>& set_value(const constrained_value<T>& rhs)= 0; |
|
43 |
virtual constrained_value<T>& set_value(T rhs)= 0; |
|
44 |
||
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
45 |
public: |
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
46 |
explicit constrained_value<T>(T in_value= 0) : |
47 |
m_val(in_value) |
|
48 |
{ } |
|
49 |
||
50 |
virtual ~constrained_value<T>() |
|
51 |
{}
|
|
52 |
||
53 |
operator T() const |
|
54 |
{
|
|
55 |
return m_val; |
|
56 |
}
|
|
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
57 |
|
58 |
constrained_value<T>& operator=(const constrained_value<T>& rhs) |
|
59 |
{
|
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
60 |
return set_value(rhs); |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
61 |
}
|
62 |
||
63 |
constrained_value<T>& operator=(T rhs) |
|
64 |
{
|
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
65 |
return set_value(rhs); |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
66 |
}
|
67 |
||
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
68 |
T getVal() const |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
69 |
{
|
70 |
return m_val; |
|
71 |
}
|
|
72 |
||
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
73 |
void setVal(T in_val) |
74 |
{
|
|
75 |
m_val= in_val; |
|
76 |
}
|
|
77 |
||
1863.1.10
by Monty Taylor
Predeclare friend operators for Sun Studio. |
78 |
friend std::istream& |
79 |
operator>>(std::istream& is, |
|
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
80 |
constrained_value<T>& bound_val) |
81 |
{
|
|
82 |
T inner_val; |
|
83 |
is >> inner_val; |
|
84 |
bound_val= inner_val; |
|
85 |
return is; |
|
86 |
}
|
|
87 |
||
88 |
friend
|
|
89 |
std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v) |
|
90 |
{
|
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
91 |
os << v.getVal(); |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
92 |
return os; |
93 |
}
|
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
94 |
};
|
95 |
||
1897.4.2
by Monty Taylor
Fixed default min/max values to use numeric_limits. |
96 |
template<class T, |
1897.4.6
by Monty Taylor
Replace optional max/min args with mandatory args. There is just no way to |
97 |
T MAXVAL, |
98 |
T MINVAL, unsigned int ALIGN= 1> |
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
99 |
class constrained_check : |
100 |
public constrained_value<T> |
|
101 |
{
|
|
102 |
public: |
|
103 |
constrained_check<T,MAXVAL,MINVAL,ALIGN>(T in_value= 0) : |
|
104 |
constrained_value<T>(in_value) |
|
105 |
{ } |
|
106 |
||
107 |
protected: |
|
108 |
constrained_value<T>& set_value(const constrained_value<T>& rhs) |
|
109 |
{
|
|
110 |
return set_value(rhs.getVal()); |
|
111 |
}
|
|
112 |
||
113 |
constrained_value<T>& set_value(T rhs) |
|
114 |
{
|
|
115 |
if ((rhs > MAXVAL) || (rhs < MINVAL)) |
|
116 |
{
|
|
1863.1.8
by Monty Taylor
Use invalid_option_value exception directly. More version neutral. |
117 |
boost::throw_exception(boost::program_options::invalid_option_value(boost::lexical_cast<std::string>(rhs))); |
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
118 |
}
|
119 |
rhs-= rhs % ALIGN; |
|
1891.2.1
by Monty Taylor
Fixed things to make things compile with clang |
120 |
this->setVal(rhs); |
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
121 |
return *this; |
122 |
}
|
|
123 |
||
124 |
||
125 |
};
|
|
126 |
||
127 |
typedef constrained_check<uint32_t,65535,1> back_log_constraints; |
|
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
128 |
|
129 |
} /* namespace drizzled */ |
|
130 |
||
131 |
template<class T> |
|
132 |
void validate(boost::any& v, |
|
133 |
const std::vector<std::string>& values, |
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
134 |
drizzled::constrained_value<T> val, int) |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
135 |
{
|
136 |
boost::program_options::validators::check_first_occurrence(v); |
|
137 |
const std::string& s= boost::program_options::validators::get_single_string(values); |
|
138 |
||
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
139 |
val= boost::lexical_cast<T>(s); |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
140 |
v= boost::any(val); |
141 |
}
|
|
142 |
||
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
143 |
|
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
144 |
#endif /* DRIZZLED_CONSTRAINED_VALUE_H */ |