~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/constrained_value.h

  • Committer: Monty Taylor
  • Date: 2010-09-14 07:47:50 UTC
  • mto: (1863.1.3 template-sys-var)
  • mto: This revision was merged to the branch mainline in revision 1870.
  • Revision ID: mordred@inaugust.com-20100914074750-4gvzrm9pm3n9kdc4
Added a basic bounded_value type for use in variables system.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#ifndef DRIZZLED_CONSTRAINED_VALUE_H
 
21
#define DRIZZLED_CONSTRAINED_VALUE_H
 
22
 
 
23
#include <boost/program_options.hpp>
 
24
#include <boost/exception/all.hpp>
 
25
#include <iostream>
 
26
 
 
27
namespace drizzled
 
28
{
 
29
 
 
30
template<class T>
 
31
class constrained_value
 
32
{
 
33
public:
 
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(),
 
37
                       int in_align_to= 1) :
 
38
    m_val(in_value),
 
39
    m_max_val(in_max_val),
 
40
    m_min_val(in_min_val),
 
41
    m_align_to(in_align_to)
 
42
  { }
 
43
 
 
44
  constrained_value<T>(const constrained_value<T>& old) :
 
45
    m_val(old.m_val),
 
46
    m_max_val(old.m_max_val),
 
47
    m_min_val(old.m_min_val),
 
48
    m_align_to(old.m_align_to)
 
49
  { }
 
50
 
 
51
  constrained_value<T>& operator=(const constrained_value<T>& rhs)
 
52
  {
 
53
    (*this)= rhs.m_val;
 
54
    return *this;
 
55
  }
 
56
 
 
57
  constrained_value<T>& operator=(T rhs)
 
58
  {
 
59
    if ((rhs > m_max_val) || (rhs < m_min_val))
 
60
    {
 
61
      boost::throw_exception(boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value));
 
62
    }
 
63
    rhs-= rhs % m_align_to;
 
64
    m_val= rhs;
 
65
    return *this;
 
66
  }
 
67
 
 
68
  operator T()
 
69
  {
 
70
    return m_val;
 
71
  }
 
72
 
 
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)
 
77
  {
 
78
    T inner_val;
 
79
    is >> inner_val;
 
80
    bound_val= inner_val;
 
81
    return is;
 
82
  }
 
83
 
 
84
  friend
 
85
  std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v)
 
86
  {
 
87
    os << v.m_val;
 
88
    return os;
 
89
  }
 
90
 
 
91
private:
 
92
  T m_val;
 
93
  T m_max_val;
 
94
  T m_min_val;
 
95
  int m_align_to;
 
96
};
 
97
 
 
98
} /* namespace drizzled */
 
99
 
 
100
template<class T>
 
101
void validate(boost::any& v,
 
102
              const std::vector<std::string>& values,
 
103
              drizzled::constrained_value<T>, int)
 
104
{
 
105
  boost::program_options::validators::check_first_occurrence(v);
 
106
  const std::string& s= boost::program_options::validators::get_single_string(values);
 
107
 
 
108
  drizzled::constrained_value<T> &val= boost::any_cast<T>(v);
 
109
  try
 
110
  {
 
111
    val= boost::lexical_cast<T>(s);
 
112
  }
 
113
  catch (...)
 
114
  {
 
115
    boost::throw_exception(boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value));
 
116
  }
 
117
  v= boost::any(val);
 
118
}
 
119
 
 
120
#endif /* DRIZZLED_CONSTRAINED_VALUE_H */