1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Monty Taylor
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.
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.
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
20
#ifndef DRIZZLED_CONSTRAINED_VALUE_H
21
#define DRIZZLED_CONSTRAINED_VALUE_H
23
#include <boost/exception/info.hpp>
24
#include <boost/program_options.hpp>
25
#include <boost/program_options/errors.hpp>
27
#include <netinet/in.h> /* for in_port_t */
32
/* We have to make this mixin exception class because boost program_option
33
exceptions don't derive from f-ing boost::exception. FAIL
35
class invalid_option_value :
36
public boost::exception,
37
public boost::program_options::invalid_option_value
40
invalid_option_value(const std::string &option_value) :
42
boost::program_options::invalid_option_value(option_value)
46
template<class T> class constrained_value;
48
std::istream& operator>>(std::istream& is, constrained_value<T>& bound_val);
50
std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v);
53
class constrained_value
58
virtual constrained_value<T>& set_value(const constrained_value<T>& rhs)= 0;
59
virtual constrained_value<T>& set_value(T rhs)= 0;
62
explicit constrained_value<T>(T in_value= 0) :
66
virtual ~constrained_value<T>()
74
constrained_value<T>& operator=(const constrained_value<T>& rhs)
76
return set_value(rhs);
79
constrained_value<T>& operator=(T rhs)
81
return set_value(rhs);
95
operator>>(std::istream& is,
96
constrained_value<T>& bound_val)
100
bound_val= inner_val;
105
std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v)
114
template<class T, T min_val>
115
bool less_than_min(T val_to_check)
117
return val_to_check < min_val;
121
inline bool less_than_min<uint16_t, 0>(uint16_t)
127
inline bool less_than_min<uint32_t, 0>(uint32_t)
133
inline bool less_than_min<uint64_t, 0>(uint64_t)
138
template<class T, T min_val>
139
bool greater_than_max(T val_to_check)
141
return val_to_check > min_val;
145
inline bool greater_than_max<uint16_t, UINT16_MAX>(uint16_t)
151
inline bool greater_than_max<uint32_t, UINT32_MAX>(uint32_t)
157
inline bool greater_than_max<uint64_t, UINT64_MAX>(uint64_t)
163
typedef boost::error_info<struct tag_invalid_max,uint64_t> invalid_max_info;
164
typedef boost::error_info<struct tag_invalid_min,int64_t> invalid_min_info;
165
typedef boost::error_info<struct tag_invalid_min,std::string> invalid_value;
169
T MINVAL, unsigned int ALIGN= 1>
170
class constrained_check :
171
public constrained_value<T>
174
constrained_check<T,MAXVAL,MINVAL,ALIGN>(T in_value= 0) :
175
constrained_value<T>(in_value)
179
constrained_value<T>& set_value(const constrained_value<T>& rhs)
181
return set_value(rhs.get());
184
constrained_value<T>& set_value(T rhs)
186
if (greater_than_max<T,MAXVAL>(rhs))
188
boost::throw_exception(invalid_option_value(boost::lexical_cast<std::string>(rhs)) << invalid_max_info(static_cast<uint64_t>(MAXVAL)));
191
if (less_than_min<T,MINVAL>(rhs))
193
boost::throw_exception(invalid_option_value(boost::lexical_cast<std::string>(rhs)) << invalid_min_info(static_cast<int64_t>(MINVAL)));
203
typedef constrained_check<uint64_t, UINT64_MAX, 0> uint64_constraint;
204
typedef constrained_check<uint32_t, UINT32_MAX, 0> uint32_constraint;
205
typedef constrained_check<uint64_t, UINT64_MAX, 1> uint64_nonzero_constraint;
206
typedef constrained_check<uint32_t, UINT32_MAX, 1> uint32_nonzero_constraint;
207
typedef drizzled::constrained_check<in_port_t, 65535, 0> port_constraint;
209
typedef constrained_check<uint32_t,65535,1> back_log_constraints;
211
} /* namespace drizzled */
214
void validate(boost::any& v,
215
const std::vector<std::string>& values,
216
drizzled::constrained_value<T> val, int)
218
boost::program_options::validators::check_first_occurrence(v);
219
const std::string& s= boost::program_options::validators::get_single_string(values);
221
val= boost::lexical_cast<T>(s);
226
#endif /* DRIZZLED_CONSTRAINED_VALUE_H */