~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/constrained_value.h

  • Committer: Brian Aker
  • Date: 2010-10-22 07:16:38 UTC
  • mfrom: (1863.1.11 template-sys-var)
  • Revision ID: brian@tangent.org-20101022071638-pk02309d027gs6zf
Merge Monty

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/program_options/errors.hpp>
 
25
#include <iostream>
 
26
 
 
27
namespace drizzled
 
28
{
 
29
 
 
30
template<class T> class constrained_value;
 
31
template<class T>
 
32
std::istream& operator>>(std::istream& is, constrained_value<T>& bound_val);
 
33
template<class T>
 
34
std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v);
 
35
 
 
36
template<class T>
 
37
class constrained_value
 
38
{
 
39
  T m_val;
 
40
protected:
 
41
 
 
42
  virtual constrained_value<T>& set_value(const constrained_value<T>& rhs)= 0;
 
43
  virtual constrained_value<T>& set_value(T rhs)= 0;
 
44
 
 
45
public:
 
46
  explicit constrained_value<T>(T in_value= 0) :
 
47
    m_val(in_value)
 
48
  { }
 
49
 
 
50
  virtual ~constrained_value<T>()
 
51
  {}
 
52
 
 
53
  operator T() const
 
54
  {
 
55
    return m_val;
 
56
  }
 
57
 
 
58
  constrained_value<T>& operator=(const constrained_value<T>& rhs)
 
59
  {
 
60
    return set_value(rhs);
 
61
  }
 
62
 
 
63
  constrained_value<T>& operator=(T rhs)
 
64
  {
 
65
    return set_value(rhs);
 
66
  }
 
67
 
 
68
  T getVal() const
 
69
  {
 
70
    return m_val;
 
71
  }
 
72
 
 
73
  void setVal(T in_val)
 
74
  {
 
75
    m_val= in_val;
 
76
  }
 
77
 
 
78
  friend std::istream&
 
79
  operator>>(std::istream& is,
 
80
             constrained_value<T>& bound_val)
 
81
  {
 
82
    T inner_val;
 
83
    is >> inner_val;
 
84
    bound_val= inner_val;
 
85
    return is;
 
86
  }
 
87
 
 
88
  friend
 
89
  std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v)
 
90
  {
 
91
    os << v.getVal();
 
92
    return os;
 
93
  }
 
94
};
 
95
 
 
96
template<class T, uint64_t MAXVAL=UINT64_MAX, int64_t MINVAL=INT64_MIN, int ALIGN=1>
 
97
class constrained_check :
 
98
  public constrained_value<T>
 
99
{
 
100
public:
 
101
  constrained_check<T,MAXVAL,MINVAL,ALIGN>(T in_value= 0) :
 
102
    constrained_value<T>(in_value)
 
103
  { }
 
104
 
 
105
protected:
 
106
  constrained_value<T>& set_value(const constrained_value<T>& rhs)
 
107
  {
 
108
    return set_value(rhs.getVal());
 
109
  }
 
110
 
 
111
  constrained_value<T>& set_value(T rhs)
 
112
  {
 
113
    if ((rhs > MAXVAL) || (rhs < MINVAL))
 
114
    {
 
115
      boost::throw_exception(boost::program_options::invalid_option_value(boost::lexical_cast<std::string>(rhs)));
 
116
    }
 
117
    rhs-= rhs % ALIGN;
 
118
    setVal(rhs);
 
119
    return *this;
 
120
  }
 
121
 
 
122
 
 
123
};
 
124
 
 
125
typedef constrained_check<uint32_t,65535,1> back_log_constraints;
 
126
 
 
127
} /* namespace drizzled */
 
128
 
 
129
template<class T>
 
130
void validate(boost::any& v,
 
131
              const std::vector<std::string>& values,
 
132
              drizzled::constrained_value<T> val, int)
 
133
{
 
134
  boost::program_options::validators::check_first_occurrence(v);
 
135
  const std::string& s= boost::program_options::validators::get_single_string(values);
 
136
 
 
137
  val= boost::lexical_cast<T>(s);
 
138
  v= boost::any(val);
 
139
}
 
140
 
 
141
 
 
142
#endif /* DRIZZLED_CONSTRAINED_VALUE_H */