~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/constrained_value.h

  • Committer: Monty Taylor
  • Date: 2010-11-25 17:05:36 UTC
  • mto: (1953.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1955.
  • Revision ID: mordred@inaugust.com-20101125170536-lik3sg2qmxjgowle
Fixed formats on 32-bit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#ifndef DRIZZLED_CONSTRAINED_VALUE_H
21
21
#define DRIZZLED_CONSTRAINED_VALUE_H
22
22
 
23
 
#include <boost/exception/info.hpp>
24
23
#include <boost/program_options.hpp>
25
24
#include <boost/program_options/errors.hpp>
26
25
#include <iostream>
27
 
#include <netinet/in.h> /* for in_port_t */
28
26
 
29
27
namespace drizzled
30
28
{
31
29
 
32
 
/* We have to make this mixin exception class because boost program_option
33
 
  exceptions don't derive from f-ing boost::exception. FAIL
34
 
*/
35
 
class invalid_option_value :
36
 
  public boost::exception,
37
 
  public boost::program_options::invalid_option_value
38
 
{
39
 
public:
40
 
  invalid_option_value(const std::string &option_value) :
41
 
    boost::exception(),
42
 
    boost::program_options::invalid_option_value(option_value)
43
 
  {}
44
 
};
45
 
 
46
30
template<class T> class constrained_value;
47
31
template<class T>
48
32
std::istream& operator>>(std::istream& is, constrained_value<T>& bound_val);
81
65
    return set_value(rhs);
82
66
  }
83
67
 
84
 
  T get() const
 
68
  T getVal() const
85
69
  {
86
70
    return m_val;
87
71
  }
104
88
  friend
105
89
  std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v)
106
90
  {
107
 
    os << v.get();
 
91
    os << v.getVal();
108
92
    return os;
109
93
  }
110
94
};
160
144
}
161
145
162
146
 
163
 
typedef boost::error_info<struct tag_invalid_max,uint64_t> invalid_max_info;
164
 
typedef boost::error_info<struct tag_invalid_min,int64_t> invalid_min_info;
165
 
typedef boost::error_info<struct tag_invalid_min,std::string> invalid_value;
166
 
 
167
147
template<class T,
168
148
  T MAXVAL,
169
149
  T MINVAL, unsigned int ALIGN= 1>
178
158
protected:
179
159
  constrained_value<T>& set_value(const constrained_value<T>& rhs)
180
160
  {
181
 
    return set_value(rhs.get());
 
161
    return set_value(rhs.getVal());
182
162
  }
183
163
 
184
164
  constrained_value<T>& set_value(T rhs)
185
165
  {
186
 
    if (greater_than_max<T,MAXVAL>(rhs))
187
 
    {
188
 
      boost::throw_exception(invalid_option_value(boost::lexical_cast<std::string>(rhs)) << invalid_max_info(static_cast<uint64_t>(MAXVAL)));
189
 
    }
190
 
      
191
 
    if (less_than_min<T,MINVAL>(rhs))
192
 
    {
193
 
      boost::throw_exception(invalid_option_value(boost::lexical_cast<std::string>(rhs)) << invalid_min_info(static_cast<int64_t>(MINVAL)));
 
166
    if (greater_than_max<T,MAXVAL>(rhs) || less_than_min<T,MINVAL>(rhs))
 
167
    {
 
168
      boost::throw_exception(boost::program_options::invalid_option_value(boost::lexical_cast<std::string>(rhs)));
194
169
    }
195
170
    rhs-= rhs % ALIGN;
196
171
    this->setVal(rhs);
200
175
 
201
176
};
202
177
 
203
 
typedef constrained_check<uint64_t, UINT64_MAX, 0> uint64_constraint;
204
 
typedef constrained_check<uint32_t, UINT32_MAX, 0> uint32_constraint;
205
 
typedef constrained_check<uint64_t, UINT64_MAX, 1> uint64_nonzero_constraint;
206
 
typedef constrained_check<uint32_t, UINT32_MAX, 1> uint32_nonzero_constraint;
207
 
typedef drizzled::constrained_check<in_port_t, 65535, 0> port_constraint;
208
 
 
209
178
typedef constrained_check<uint32_t,65535,1> back_log_constraints;
210
179
 
211
180
} /* namespace drizzled */