~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/constrained_value.h

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

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>
23
24
#include <boost/program_options.hpp>
24
25
#include <boost/program_options/errors.hpp>
25
26
#include <iostream>
 
27
#include <netinet/in.h> /* for in_port_t */
26
28
 
27
29
namespace drizzled
28
30
{
29
31
 
 
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
 
30
46
template<class T> class constrained_value;
31
47
template<class T>
32
48
std::istream& operator>>(std::istream& is, constrained_value<T>& bound_val);
65
81
    return set_value(rhs);
66
82
  }
67
83
 
68
 
  T getVal() const
 
84
  T get() const
69
85
  {
70
86
    return m_val;
71
87
  }
88
104
  friend
89
105
  std::ostream& operator<<(std::ostream& os, const constrained_value<T>& v)
90
106
  {
91
 
    os << v.getVal();
 
107
    os << v.get();
92
108
    return os;
93
109
  }
94
110
};
95
111
 
 
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
typedef boost::error_info<struct tag_invalid_min,std::string> invalid_value;
 
166
 
96
167
template<class T,
97
168
  T MAXVAL,
98
169
  T MINVAL, unsigned int ALIGN= 1>
107
178
protected:
108
179
  constrained_value<T>& set_value(const constrained_value<T>& rhs)
109
180
  {
110
 
    return set_value(rhs.getVal());
 
181
    return set_value(rhs.get());
111
182
  }
112
183
 
113
184
  constrained_value<T>& set_value(T rhs)
114
185
  {
115
 
    if ((rhs > MAXVAL) || (rhs < MINVAL))
116
 
    {
117
 
      boost::throw_exception(boost::program_options::invalid_option_value(boost::lexical_cast<std::string>(rhs)));
 
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)));
118
194
    }
119
195
    rhs-= rhs % ALIGN;
120
196
    this->setVal(rhs);
124
200
 
125
201
};
126
202
 
 
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
 
127
209
typedef constrained_check<uint32_t,65535,1> back_log_constraints;
128
210
 
129
211
} /* namespace drizzled */