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
22
#include <boost/exception/info.hpp>
23
#include <boost/program_options.hpp>
24
#include <boost/program_options/errors.hpp>
26
#include <netinet/in.h> /* for in_port_t */
31
/* We have to make this mixin exception class because boost program_option
32
exceptions don't derive from f-ing boost::exception. FAIL
34
class invalid_option_value :
35
public boost::exception,
36
public boost::program_options::invalid_option_value
39
invalid_option_value(const std::string &option_value) :
41
boost::program_options::invalid_option_value(option_value)
45
template<class T> class constrained_value;
47
std::istream& operator>>(std::istream& is, constrained_value<T>& bound_val);
49
std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v);
52
class constrained_value
57
virtual constrained_value<T>& set_value(const constrained_value<T>& rhs)= 0;
58
virtual constrained_value<T>& set_value(T rhs)= 0;
61
explicit constrained_value<T>(T in_value= 0) :
65
virtual ~constrained_value<T>()
73
constrained_value<T>& operator=(const constrained_value<T>& rhs)
75
return set_value(rhs);
78
constrained_value<T>& operator=(T rhs)
80
return set_value(rhs);
94
operator>>(std::istream& is,
95
constrained_value<T>& bound_val)
104
std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v)
113
template<class T, T min_val>
114
bool less_than_min(T val_to_check)
116
return val_to_check < min_val;
120
inline bool less_than_min<uint16_t, 0>(uint16_t)
126
inline bool less_than_min<uint32_t, 0>(uint32_t)
132
inline bool less_than_min<uint64_t, 0>(uint64_t)
137
template<class T, T min_val>
138
bool greater_than_max(T val_to_check)
140
return val_to_check > min_val;
144
inline bool greater_than_max<uint16_t, UINT16_MAX>(uint16_t)
150
inline bool greater_than_max<uint32_t, UINT32_MAX>(uint32_t)
156
inline bool greater_than_max<uint64_t, UINT64_MAX>(uint64_t)
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;
164
typedef boost::error_info<struct tag_invalid_min,std::string> invalid_value;
168
T MINVAL, unsigned int ALIGN= 1>
169
class constrained_check :
170
public constrained_value<T>
173
constrained_check<T,MAXVAL,MINVAL,ALIGN>(T in_value= 0) :
174
constrained_value<T>(in_value)
178
constrained_value<T>& set_value(const constrained_value<T>& rhs)
180
return set_value(rhs.get());
183
constrained_value<T>& set_value(T rhs)
185
if (greater_than_max<T,MAXVAL>(rhs))
187
boost::throw_exception(invalid_option_value(boost::lexical_cast<std::string>(rhs)) << invalid_max_info(static_cast<uint64_t>(MAXVAL)));
190
if (less_than_min<T,MINVAL>(rhs))
192
boost::throw_exception(invalid_option_value(boost::lexical_cast<std::string>(rhs)) << invalid_min_info(static_cast<int64_t>(MINVAL)));
202
typedef constrained_check<uint64_t, UINT64_MAX, 0> uint64_constraint;
203
typedef constrained_check<uint32_t, UINT32_MAX, 0> uint32_constraint;
204
typedef constrained_check<uint64_t, UINT64_MAX, 1> uint64_nonzero_constraint;
205
typedef constrained_check<uint32_t, UINT32_MAX, 1> uint32_nonzero_constraint;
206
typedef drizzled::constrained_check<in_port_t, 65535, 0> port_constraint;
208
typedef constrained_check<uint32_t,65535,1> back_log_constraints;
210
} /* namespace drizzled */
213
void validate(boost::any& v,
214
const std::vector<std::string>& values,
215
drizzled::constrained_value<T> val, int)
217
boost::program_options::validators::check_first_occurrence(v);
218
const std::string& s= boost::program_options::validators::get_single_string(values);
220
val= boost::lexical_cast<T>(s);