~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/constrained_value.h

  • Committer: Monty Taylor
  • Date: 2010-11-08 18:26:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1931.
  • Revision ID: mordred@inaugust.com-20101108182608-lci86acl7r53sbi3
Replaced auto_ptr with scoped_ptr.

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
};
111
95
 
112
 
namespace
113
 
{
114
 
template<class T, T min_val>
115
 
bool less_than_min(T val_to_check)
116
 
{
117
 
  return val_to_check < min_val;
118
 
}
119
 
 
120
 
template<>
121
 
inline bool less_than_min<uint16_t, 0>(uint16_t)
122
 
{
123
 
  return false;
124
 
}
125
 
 
126
 
template<>
127
 
inline bool less_than_min<uint32_t, 0>(uint32_t)
128
 
{
129
 
  return false;
130
 
}
131
 
 
132
 
template<>
133
 
inline bool less_than_min<uint64_t, 0>(uint64_t)
134
 
{
135
 
  return false;
136
 
}
137
 
 
138
 
template<class T, T min_val>
139
 
bool greater_than_max(T val_to_check)
140
 
{
141
 
  return val_to_check > min_val;
142
 
}
143
 
 
144
 
template<>
145
 
inline bool greater_than_max<uint16_t, UINT16_MAX>(uint16_t)
146
 
{
147
 
  return false;
148
 
}
149
 
 
150
 
template<>
151
 
inline bool greater_than_max<uint32_t, UINT32_MAX>(uint32_t)
152
 
{
153
 
  return false;
154
 
}
155
 
 
156
 
template<>
157
 
inline bool greater_than_max<uint64_t, UINT64_MAX>(uint64_t)
158
 
{
159
 
  return false;
160
 
}
161
 
162
 
 
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
 
 
166
96
template<class T,
167
97
  T MAXVAL,
168
98
  T MINVAL, unsigned int ALIGN= 1>
177
107
protected:
178
108
  constrained_value<T>& set_value(const constrained_value<T>& rhs)
179
109
  {
180
 
    return set_value(rhs.get());
 
110
    return set_value(rhs.getVal());
181
111
  }
182
112
 
183
113
  constrained_value<T>& set_value(T rhs)
184
114
  {
185
 
    if (greater_than_max<T,MAXVAL>(rhs))
186
 
    {
187
 
      boost::throw_exception(invalid_option_value(boost::lexical_cast<std::string>(rhs)) << invalid_max_info(static_cast<uint64_t>(MAXVAL)));
188
 
    }
189
 
      
190
 
    if (less_than_min<T,MINVAL>(rhs))
191
 
    {
192
 
      boost::throw_exception(invalid_option_value(boost::lexical_cast<std::string>(rhs)) << invalid_min_info(static_cast<int64_t>(MINVAL)));
 
115
    if ((rhs > MAXVAL) || (rhs < MINVAL))
 
116
    {
 
117
      boost::throw_exception(boost::program_options::invalid_option_value(boost::lexical_cast<std::string>(rhs)));
193
118
    }
194
119
    rhs-= rhs % ALIGN;
195
120
    this->setVal(rhs);
199
124
 
200
125
};
201
126
 
202
 
typedef constrained_check<uint64_t, UINT64_MAX, 0> uint64_constraint;
203
 
typedef constrained_check<uint32_t, UINT32_MAX, 0> uint32_constraint;
204
 
typedef constrained_check<uint64_t, UINT64_MAX, 1> uint64_nonzero_constraint;
205
 
typedef constrained_check<uint32_t, UINT32_MAX, 1> uint32_nonzero_constraint;
206
 
typedef drizzled::constrained_check<in_port_t, 65535, 0> port_constraint;
207
 
 
208
127
typedef constrained_check<uint32_t,65535,1> back_log_constraints;
209
128
 
210
129
} /* namespace drizzled */