~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2010 Monty Taylor
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef DRIZZLED_CONSTRAINED_VALUE_H
#define DRIZZLED_CONSTRAINED_VALUE_H

#include <boost/program_options.hpp>
#include <boost/program_options/errors.hpp>
#include <iostream>

namespace drizzled
{

template<class T> class constrained_value;
template<class T>
std::istream& operator>>(std::istream& is, constrained_value<T>& bound_val);
template<class T>
std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v);

template<class T>
class constrained_value
{
  T m_val;
protected:

  virtual constrained_value<T>& set_value(const constrained_value<T>& rhs)= 0;
  virtual constrained_value<T>& set_value(T rhs)= 0;

public:
  explicit constrained_value<T>(T in_value= 0) :
    m_val(in_value)
  { }

  virtual ~constrained_value<T>()
  {}

  operator T() const
  {
    return m_val;
  }

  constrained_value<T>& operator=(const constrained_value<T>& rhs)
  {
    return set_value(rhs);
  }

  constrained_value<T>& operator=(T rhs)
  {
    return set_value(rhs);
  }

  T getVal() const
  {
    return m_val;
  }

  void setVal(T in_val)
  {
    m_val= in_val;
  }

  friend std::istream&
  operator>>(std::istream& is,
             constrained_value<T>& bound_val)
  {
    T inner_val;
    is >> inner_val;
    bound_val= inner_val;
    return is;
  }

  friend
  std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v)
  {
    os << v.getVal();
    return os;
  }
};

template<class T, uint64_t MAXVAL=UINT64_MAX, int64_t MINVAL=INT64_MIN, int ALIGN=1>
class constrained_check :
  public constrained_value<T>
{
public:
  constrained_check<T,MAXVAL,MINVAL,ALIGN>(T in_value= 0) :
    constrained_value<T>(in_value)
  { }

protected:
  constrained_value<T>& set_value(const constrained_value<T>& rhs)
  {
    return set_value(rhs.getVal());
  }

  constrained_value<T>& set_value(T rhs)
  {
    if ((rhs > MAXVAL) || (rhs < MINVAL))
    {
      boost::throw_exception(boost::program_options::invalid_option_value(boost::lexical_cast<std::string>(rhs)));
    }
    rhs-= rhs % ALIGN;
    setVal(rhs);
    return *this;
  }


};

typedef constrained_check<uint32_t,65535,1> back_log_constraints;

} /* namespace drizzled */

template<class T>
void validate(boost::any& v,
              const std::vector<std::string>& values,
              drizzled::constrained_value<T> val, int)
{
  boost::program_options::validators::check_first_occurrence(v);
  const std::string& s= boost::program_options::validators::get_single_string(values);

  val= boost::lexical_cast<T>(s);
  v= boost::any(val);
}


#endif /* DRIZZLED_CONSTRAINED_VALUE_H */