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 |
||
2234
by Brian Aker
Mass removal of ifdef/endif in favor of pragma once. |
20 |
#pragma once
|
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
21 |
|
1964.1.4
by Monty Taylor
Use boost::exception data bundling instead of co-opting the exceptions |
22 |
#include <boost/exception/info.hpp> |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
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> |
2371.1.1
by Brian Aker
Fedora fix/use fwd header for iostream. |
25 |
#include <iosfwd> |
1976.2.1
by Monty Taylor
Updated PBMS. |
26 |
#include <netinet/in.h> /* for in_port_t */ |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
27 |
|
28 |
namespace drizzled |
|
29 |
{
|
|
30 |
||
1964.1.4
by Monty Taylor
Use boost::exception data bundling instead of co-opting the exceptions |
31 |
/* We have to make this mixin exception class because boost program_option
|
32 |
exceptions don't derive from f-ing boost::exception. FAIL
|
|
33 |
*/
|
|
34 |
class invalid_option_value : |
|
35 |
public boost::exception, |
|
36 |
public boost::program_options::invalid_option_value |
|
37 |
{
|
|
38 |
public: |
|
39 |
invalid_option_value(const std::string &option_value) : |
|
40 |
boost::exception(), |
|
41 |
boost::program_options::invalid_option_value(option_value) |
|
42 |
{}
|
|
43 |
};
|
|
44 |
||
1863.1.10
by Monty Taylor
Predeclare friend operators for Sun Studio. |
45 |
template<class T> class constrained_value; |
46 |
template<class T> |
|
47 |
std::istream& operator>>(std::istream& is, constrained_value<T>& bound_val); |
|
48 |
template<class T> |
|
49 |
std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v); |
|
50 |
||
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
51 |
template<class T> |
52 |
class constrained_value |
|
53 |
{
|
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
54 |
T m_val; |
1897.4.17
by Monty Taylor
Reverted a debugging change that snuck in. |
55 |
protected: |
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
56 |
|
57 |
virtual constrained_value<T>& set_value(const constrained_value<T>& rhs)= 0; |
|
58 |
virtual constrained_value<T>& set_value(T rhs)= 0; |
|
59 |
||
1897.4.17
by Monty Taylor
Reverted a debugging change that snuck in. |
60 |
public: |
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
61 |
explicit constrained_value<T>(T in_value= 0) : |
62 |
m_val(in_value) |
|
63 |
{ } |
|
64 |
||
65 |
virtual ~constrained_value<T>() |
|
66 |
{}
|
|
67 |
||
68 |
operator T() const |
|
69 |
{
|
|
70 |
return m_val; |
|
71 |
}
|
|
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
72 |
|
73 |
constrained_value<T>& operator=(const constrained_value<T>& rhs) |
|
74 |
{
|
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
75 |
return set_value(rhs); |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
76 |
}
|
77 |
||
78 |
constrained_value<T>& operator=(T rhs) |
|
79 |
{
|
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
80 |
return set_value(rhs); |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
81 |
}
|
82 |
||
1964.2.3
by Monty Taylor
Remove need for casts out of constrained_value - use get() method as well - |
83 |
T get() const |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
84 |
{
|
85 |
return m_val; |
|
86 |
}
|
|
87 |
||
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
88 |
void setVal(T in_val) |
89 |
{
|
|
90 |
m_val= in_val; |
|
91 |
}
|
|
92 |
||
1863.1.10
by Monty Taylor
Predeclare friend operators for Sun Studio. |
93 |
friend std::istream& |
94 |
operator>>(std::istream& is, |
|
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
95 |
constrained_value<T>& bound_val) |
96 |
{
|
|
97 |
T inner_val; |
|
98 |
is >> inner_val; |
|
99 |
bound_val= inner_val; |
|
100 |
return is; |
|
101 |
}
|
|
102 |
||
103 |
friend
|
|
104 |
std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v) |
|
105 |
{
|
|
1964.2.3
by Monty Taylor
Remove need for casts out of constrained_value - use get() method as well - |
106 |
os << v.get(); |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
107 |
return os; |
108 |
}
|
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
109 |
};
|
110 |
||
1945.1.2
by Monty Taylor
Add better support in constrained_check for min/max support. Add string ref class. |
111 |
namespace
|
112 |
{
|
|
113 |
template<class T, T min_val> |
|
114 |
bool less_than_min(T val_to_check) |
|
115 |
{
|
|
116 |
return val_to_check < min_val; |
|
117 |
}
|
|
118 |
||
119 |
template<> |
|
120 |
inline bool less_than_min<uint16_t, 0>(uint16_t) |
|
121 |
{
|
|
122 |
return false; |
|
123 |
}
|
|
124 |
||
125 |
template<> |
|
126 |
inline bool less_than_min<uint32_t, 0>(uint32_t) |
|
127 |
{
|
|
128 |
return false; |
|
129 |
}
|
|
130 |
||
131 |
template<> |
|
132 |
inline bool less_than_min<uint64_t, 0>(uint64_t) |
|
133 |
{
|
|
134 |
return false; |
|
135 |
}
|
|
136 |
||
137 |
template<class T, T min_val> |
|
138 |
bool greater_than_max(T val_to_check) |
|
139 |
{
|
|
140 |
return val_to_check > min_val; |
|
141 |
}
|
|
142 |
||
143 |
template<> |
|
144 |
inline bool greater_than_max<uint16_t, UINT16_MAX>(uint16_t) |
|
145 |
{
|
|
146 |
return false; |
|
147 |
}
|
|
148 |
||
149 |
template<> |
|
150 |
inline bool greater_than_max<uint32_t, UINT32_MAX>(uint32_t) |
|
151 |
{
|
|
152 |
return false; |
|
153 |
}
|
|
154 |
||
155 |
template<> |
|
156 |
inline bool greater_than_max<uint64_t, UINT64_MAX>(uint64_t) |
|
157 |
{
|
|
158 |
return false; |
|
159 |
}
|
|
160 |
}
|
|
161 |
||
1964.1.4
by Monty Taylor
Use boost::exception data bundling instead of co-opting the exceptions |
162 |
typedef boost::error_info<struct tag_invalid_max,uint64_t> invalid_max_info; |
163 |
typedef boost::error_info<struct tag_invalid_min,int64_t> invalid_min_info; |
|
2070.2.1
by Monty Taylor
First step in getting that anonymous union out of set_var. |
164 |
typedef boost::error_info<struct tag_invalid_min,std::string> invalid_value; |
1964.1.4
by Monty Taylor
Use boost::exception data bundling instead of co-opting the exceptions |
165 |
|
1897.4.2
by Monty Taylor
Fixed default min/max values to use numeric_limits. |
166 |
template<class T, |
1897.4.6
by Monty Taylor
Replace optional max/min args with mandatory args. There is just no way to |
167 |
T MAXVAL, |
168 |
T MINVAL, unsigned int ALIGN= 1> |
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
169 |
class constrained_check : |
170 |
public constrained_value<T> |
|
171 |
{
|
|
172 |
public: |
|
173 |
constrained_check<T,MAXVAL,MINVAL,ALIGN>(T in_value= 0) : |
|
174 |
constrained_value<T>(in_value) |
|
175 |
{ } |
|
176 |
||
1897.4.17
by Monty Taylor
Reverted a debugging change that snuck in. |
177 |
protected: |
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
178 |
constrained_value<T>& set_value(const constrained_value<T>& rhs) |
179 |
{
|
|
1964.2.3
by Monty Taylor
Remove need for casts out of constrained_value - use get() method as well - |
180 |
return set_value(rhs.get()); |
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
181 |
}
|
182 |
||
183 |
constrained_value<T>& set_value(T rhs) |
|
184 |
{
|
|
1964.1.2
by Monty Taylor
Fixed error message thrown when we try to set a variable to an incorrect |
185 |
if (greater_than_max<T,MAXVAL>(rhs)) |
186 |
{
|
|
1975.1.4
by Monty Taylor
Fixed a couple of icc warnings. |
187 |
boost::throw_exception(invalid_option_value(boost::lexical_cast<std::string>(rhs)) << invalid_max_info(static_cast<uint64_t>(MAXVAL))); |
1964.1.2
by Monty Taylor
Fixed error message thrown when we try to set a variable to an incorrect |
188 |
}
|
189 |
||
190 |
if (less_than_min<T,MINVAL>(rhs)) |
|
191 |
{
|
|
1975.1.4
by Monty Taylor
Fixed a couple of icc warnings. |
192 |
boost::throw_exception(invalid_option_value(boost::lexical_cast<std::string>(rhs)) << invalid_min_info(static_cast<int64_t>(MINVAL))); |
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
193 |
}
|
194 |
rhs-= rhs % ALIGN; |
|
1891.2.1
by Monty Taylor
Fixed things to make things compile with clang |
195 |
this->setVal(rhs); |
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
196 |
return *this; |
197 |
}
|
|
198 |
||
199 |
||
200 |
};
|
|
201 |
||
1964.2.7
by Monty Taylor
Refactored syslog module and changed it to use sys_var directly. |
202 |
typedef constrained_check<uint64_t, UINT64_MAX, 0> uint64_constraint; |
1964.2.17
by Monty Taylor
Did logging_query. Fixed rabbitmq. |
203 |
typedef constrained_check<uint32_t, UINT32_MAX, 0> uint32_constraint; |
1976.2.7
by Monty Taylor
four more variables |
204 |
typedef constrained_check<uint64_t, UINT64_MAX, 1> uint64_nonzero_constraint; |
205 |
typedef constrained_check<uint32_t, UINT32_MAX, 1> uint32_nonzero_constraint; |
|
1976.2.1
by Monty Taylor
Updated PBMS. |
206 |
typedef drizzled::constrained_check<in_port_t, 65535, 0> port_constraint; |
1964.2.7
by Monty Taylor
Refactored syslog module and changed it to use sys_var directly. |
207 |
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
208 |
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. |
209 |
|
210 |
} /* namespace drizzled */ |
|
211 |
||
212 |
template<class T> |
|
213 |
void validate(boost::any& v, |
|
214 |
const std::vector<std::string>& values, |
|
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
215 |
drizzled::constrained_value<T> val, int) |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
216 |
{
|
217 |
boost::program_options::validators::check_first_occurrence(v); |
|
218 |
const std::string& s= boost::program_options::validators::get_single_string(values); |
|
219 |
||
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
220 |
val= boost::lexical_cast<T>(s); |
1863.2.1
by Monty Taylor
Added a basic bounded_value type for use in variables system. |
221 |
v= boost::any(val); |
222 |
}
|
|
223 |
||
1863.1.7
by Monty Taylor
Add a constrained_value class which allows us to set compile-time |
224 |