~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/constrained_value.h

Merge Revision revid:marko.makela@oracle.com-20100505101406-u4low2x26q6itck0 from MySQL InnoDB

Original revid:marko.makela@oracle.com-20100505101406-u4low2x26q6itck0

Original Authors: Marko Mäkelä <marko.makela@oracle.com>
Original commit message:
Merge from mysql-5.1-innodb:

  ------------------------------------------------------------
  revno: 3446
  revision-id: marko.makela@oracle.com-20100505100507-6kcd2hf32hruxbv7
  parent: marko.makela@oracle.com-20100505095328-vetnl0flhmhao7p5
  committer: Marko Mäkelä <marko.makela@oracle.com>
  branch nick: 5.1-innodb
  timestamp: Wed 2010-05-05 13:05:07 +0300
  message:
    Add Valgrind diagnostics to track down Bug #38999.
  ------------------------------------------------------------

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,
 
97
  T MAXVAL,
 
98
  T MINVAL, unsigned int ALIGN= 1>
 
99
class constrained_check :
 
100
  public constrained_value<T>
 
101
{
 
102
public:
 
103
  constrained_check<T,MAXVAL,MINVAL,ALIGN>(T in_value= 0) :
 
104
    constrained_value<T>(in_value)
 
105
  { }
 
106
 
 
107
protected:
 
108
  constrained_value<T>& set_value(const constrained_value<T>& rhs)
 
109
  {
 
110
    return set_value(rhs.getVal());
 
111
  }
 
112
 
 
113
  constrained_value<T>& set_value(T rhs)
 
114
  {
 
115
    if ((rhs > MAXVAL) || (rhs < MINVAL))
 
116
    {
 
117
      boost::throw_exception(boost::program_options::invalid_option_value(boost::lexical_cast<std::string>(rhs)));
 
118
    }
 
119
    rhs-= rhs % ALIGN;
 
120
    this->setVal(rhs);
 
121
    return *this;
 
122
  }
 
123
 
 
124
 
 
125
};
 
126
 
 
127
typedef constrained_check<uint32_t,65535,1> back_log_constraints;
 
128
 
 
129
} /* namespace drizzled */
 
130
 
 
131
template<class T>
 
132
void validate(boost::any& v,
 
133
              const std::vector<std::string>& values,
 
134
              drizzled::constrained_value<T> val, int)
 
135
{
 
136
  boost::program_options::validators::check_first_occurrence(v);
 
137
  const std::string& s= boost::program_options::validators::get_single_string(values);
 
138
 
 
139
  val= boost::lexical_cast<T>(s);
 
140
  v= boost::any(val);
 
141
}
 
142
 
 
143
 
 
144
#endif /* DRIZZLED_CONSTRAINED_VALUE_H */