drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
constrained_value.h
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#pragma once
21
22#include <boost/exception/info.hpp>
23#include <boost/program_options.hpp>
24#include <boost/program_options/errors.hpp>
25#include <iosfwd>
26#include <netinet/in.h> /* for in_port_t */
27
28namespace drizzled
29{
30
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*/
34class invalid_option_value :
35 public boost::exception,
36 public boost::program_options::invalid_option_value
37{
38public:
39 invalid_option_value(const std::string &option_value) :
40 boost::exception(),
41 boost::program_options::invalid_option_value(option_value)
42 {}
43};
44
45template<class T> class constrained_value;
46template<class T>
47std::istream& operator>>(std::istream& is, constrained_value<T>& bound_val);
48template<class T>
49std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v);
50
51template<class T>
52class constrained_value
53{
54 T m_val;
55protected:
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
60public:
61 explicit constrained_value(T in_value= 0) :
62 m_val(in_value)
63 { }
64
65 constrained_value(const constrained_value<T>& rhs) :
66 m_val(rhs.m_val)
67 { }
68
69 virtual ~constrained_value()
70 {}
71
72 operator T() const
73 {
74 return m_val;
75 }
76
77 constrained_value<T>& operator=(const constrained_value<T>& rhs)
78 {
79 return set_value(rhs);
80 }
81
82 constrained_value<T>& operator=(T rhs)
83 {
84 return set_value(rhs);
85 }
86
87 T get() const
88 {
89 return m_val;
90 }
91
92 void setVal(T in_val)
93 {
94 m_val= in_val;
95 }
96
97 friend std::istream&
98 operator>>(std::istream& is,
99 constrained_value<T>& bound_val)
100 {
101 T inner_val;
102 is >> inner_val;
103 bound_val= inner_val;
104 return is;
105 }
106
107 friend
108 std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v)
109 {
110 os << v.get();
111 return os;
112 }
113};
114
115namespace
116{
117template<class T, T min_val>
118bool less_than_min(T val_to_check)
119{
120 return val_to_check < min_val;
121}
122
123template<>
124inline bool less_than_min<uint16_t, 0>(uint16_t)
125{
126 return false;
127}
128
129template<>
130inline bool less_than_min<uint32_t, 0>(uint32_t)
131{
132 return false;
133}
134
135template<>
136inline bool less_than_min<uint64_t, 0>(uint64_t)
137{
138 return false;
139}
140
141template<class T, T min_val>
142bool greater_than_max(T val_to_check)
143{
144 return val_to_check > min_val;
145}
146
147template<>
148inline bool greater_than_max<uint16_t, UINT16_MAX>(uint16_t)
149{
150 return false;
151}
152
153template<>
154inline bool greater_than_max<uint32_t, UINT32_MAX>(uint32_t)
155{
156 return false;
157}
158
159template<>
160inline bool greater_than_max<uint64_t, UINT64_MAX>(uint64_t)
161{
162 return false;
163}
164}
165
166typedef boost::error_info<struct tag_invalid_max,uint64_t> invalid_max_info;
167typedef boost::error_info<struct tag_invalid_min,int64_t> invalid_min_info;
168typedef boost::error_info<struct tag_invalid_min,std::string> invalid_value;
169
170template<class T,
171 T MAXVAL,
172 T MINVAL, unsigned int ALIGN= 1>
173class constrained_check :
174 public constrained_value<T>
175{
176public:
177 constrained_check(T in_value= 0) :
178 constrained_value<T>(in_value)
179 { }
180
181protected:
182 constrained_value<T>& set_value(const constrained_value<T>& rhs)
183 {
184 return set_value(rhs.get());
185 }
186
187 constrained_value<T>& set_value(T rhs)
188 {
189 if (greater_than_max<T,MAXVAL>(rhs))
190 {
191 boost::throw_exception(invalid_option_value(boost::lexical_cast<std::string>(rhs)) << invalid_max_info(static_cast<uint64_t>(MAXVAL)));
192 }
193
194 if (less_than_min<T,MINVAL>(rhs))
195 {
196 boost::throw_exception(invalid_option_value(boost::lexical_cast<std::string>(rhs)) << invalid_min_info(static_cast<int64_t>(MINVAL)));
197 }
198 rhs-= rhs % ALIGN;
199 this->setVal(rhs);
200 return *this;
201 }
202
203
204};
205
206typedef constrained_check<uint64_t, UINT64_MAX, 0> uint64_constraint;
207typedef constrained_check<uint32_t, UINT32_MAX, 0> uint32_constraint;
208typedef constrained_check<uint64_t, UINT64_MAX, 1> uint64_nonzero_constraint;
209typedef constrained_check<uint32_t, UINT32_MAX, 1> uint32_nonzero_constraint;
211
212typedef constrained_check<uint32_t,65535,1> back_log_constraints;
213
214} /* namespace drizzled */
215
216template<class T>
217void validate(boost::any& v,
218 const std::vector<std::string>& values,
220{
221 boost::program_options::validators::check_first_occurrence(v);
222 const std::string& s= boost::program_options::validators::get_single_string(values);
223
224 val= boost::lexical_cast<T>(s);
225 v= boost::any(val);
226}
227
TODO: Rename this file - func.h is stupid.