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/program_options.hpp>
24
#include <boost/exception/all.hpp>
31
class constrained_value
34
constrained_value<T>(T in_value= 0,
35
T in_max_val= std::numeric_limits<T>::max(),
36
T in_min_val= std::numeric_limits<T>::min(),
39
m_max_val(in_max_val),
40
m_min_val(in_min_val),
41
m_align_to(in_align_to)
44
constrained_value<T>(const constrained_value<T>& old) :
46
m_max_val(old.m_max_val),
47
m_min_val(old.m_min_val),
48
m_align_to(old.m_align_to)
51
constrained_value<T>& operator=(const constrained_value<T>& rhs)
57
constrained_value<T>& operator=(T rhs)
59
if ((rhs > m_max_val) || (rhs < m_min_val))
61
boost::throw_exception(boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value));
63
rhs-= rhs % m_align_to;
73
template<class CharT, class Traits>
74
friend std::basic_istream<CharT,Traits>&
75
operator>>(std::basic_istream<CharT,Traits>& is,
76
constrained_value<T>& bound_val)
85
std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v)
98
} /* namespace drizzled */
101
void validate(boost::any& v,
102
const std::vector<std::string>& values,
103
drizzled::constrained_value<T>, int)
105
boost::program_options::validators::check_first_occurrence(v);
106
const std::string& s= boost::program_options::validators::get_single_string(values);
108
drizzled::constrained_value<T> &val= boost::any_cast<T>(v);
111
val= boost::lexical_cast<T>(s);
115
boost::throw_exception(boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value));
120
#endif /* DRIZZLED_CONSTRAINED_VALUE_H */