Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
NumberRestriction.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "NumberRestriction.h"
14 
15 
16 using namespace kome::core;
17 
18 
19 #include <crtdbg.h>
20 #ifdef _DEBUG
21  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
22  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
23 #endif // _DEBUG
24 
25 
26 // constructor
28 }
29 
30 // destructor
32 }
33 
34 // reset
36  m_minInclusive.reset();
37  m_maxInclusive.reset();
38  m_minExclusive.reset();
39  m_maxExclusive.reset();
40 }
41 
42 // set inclusive min value
43 void NumberRestriction::setInclusiveMinValue( const double value ) {
44  m_minInclusive = value;
45 }
46 
47 // judge whether to have inclusive min value
49  if( m_minInclusive ) {
50  return true;
51  }
52  return false;
53 }
54 
55 // get inclusive min value
56 double NumberRestriction::getInclusiveMinValue( const double dfValue ) {
57  if( m_minInclusive ) {
58  return m_minInclusive.get();
59  }
60  return dfValue;
61 }
62 
63 // set inclusive max value
64 void NumberRestriction::setInclusiveMaxValue( const double value ) {
65  m_maxInclusive = value;
66 }
67 
68 // judge whether to have inclusive max value
70  if( m_maxInclusive ) {
71  return true;
72  }
73  return false;
74 }
75 
76 // get inclusive max value
77 double NumberRestriction::getInclusiveMaxValue( const double dfValue ) {
78  if( m_maxInclusive ) {
79  return m_maxInclusive.get();
80  }
81  return dfValue;
82 }
83 
84 // set exclusive min value
85 void NumberRestriction::setExclusiveMinValue( const double value ) {
86  m_minExclusive = value;
87 }
88 
89 // judge whether to have exclusive min value
91  if( m_minExclusive ) {
92  return true;
93  }
94  return false;
95 }
96 
97 // get exclusive min value
98 double NumberRestriction::getExclusiveMinValue( const double dfValue ) {
99  if( m_minExclusive ) {
100  return m_minExclusive.get();
101  }
102  return dfValue;
103 }
104 
105 // set exclusive max value
106 void NumberRestriction::setExclusiveMaxValue( const double value ) {
107  m_maxExclusive = value;
108 }
109 
110 // judge whether to have exclusive max value
112  if( m_maxExclusive ) {
113  return true;
114  }
115  return false;
116 }
117 
118 // get exclusive max value
119 double NumberRestriction::getExclusiveMaxValue( const double dfValue ) {
120  if( m_maxExclusive ) {
121  return m_maxExclusive.get();
122  }
123  return dfValue;
124 }
125 
126 // get lower bound value
127 double NumberRestriction::getLowerBound( const double dfValue ) {
128  if( m_minInclusive ) {
129  double v0 = m_minInclusive.get();
130  if( m_minExclusive ) {
131  double v1 = m_minExclusive.get();
132 
133  // compare
134  return MAX( v0, v1 );
135  }
136  return v0;
137  }
138  else if( m_minExclusive ) {
139  double v1 = m_minExclusive.get();
140  return v1;
141  }
142 
143  // default value
144  return dfValue;
145 }
146 
147 // get upper bound value
148 double NumberRestriction::getUpperBound( const double dfValue ) {
149  if( m_maxInclusive ) {
150  double v0 = m_maxInclusive.get();
151  if( m_maxExclusive ) {
152  double v1 = m_maxExclusive.get();
153 
154  // compare
155  return MIN( v0, v1 );
156  }
157  return v0;
158  }
159  else if( m_maxExclusive ) {
160  double v1 = m_maxExclusive.get();
161  return v1;
162  }
163 
164  // default value
165  return dfValue;
166 }
167 
168 // check double range
169 bool NumberRestriction::checkDoubleValue( const double value ) {
170  // min inclusive
171  if( m_minInclusive ) {
172  if( value < m_minInclusive.get() ) {
173  return false;
174  }
175  }
176 
177  // max inclusive
178  if( m_maxInclusive ) {
179  if( value > m_maxInclusive.get() ) {
180  return false;
181  }
182  }
183 
184  // min exclusive
185  if( m_minExclusive ) {
186  if( value <= m_minExclusive.get() ) {
187  return false;
188  }
189  }
190 
191  // max exclusive
192  if( m_maxExclusive ) {
193  if( value >= m_maxExclusive.get() ) {
194  return false;
195  }
196  }
197 
198  return true;
199 }
200 
201 // check int range
202 bool NumberRestriction::checkIntValue( const int value ) {
203  // min inclusive
204  if( m_minInclusive ) {
205  if( value < roundnum( m_minInclusive.get() ) ) {
206  return false;
207  }
208  }
209 
210  // max inclusive
211  if( m_maxInclusive ) {
212  if( value > roundnum( m_maxInclusive.get() ) ) {
213  return false;
214  }
215  }
216 
217  // min exclusive
218  if( m_minExclusive ) {
219  if( value <= roundnum( m_minExclusive.get() ) ) {
220  return false;
221  }
222  }
223 
224  // max exclusive
225  if( m_maxExclusive ) {
226  if( value >= roundnum( m_maxExclusive.get() ) ) {
227  return false;
228  }
229  }
230 
231  return true;
232 }
233 
234 // get range string
235 std::string NumberRestriction::getRangeStr( const char* name ) {
236  // string
237  std::string range;
238 
239  // variables
240  boost::optional< double > v;
241  bool inclusive = false;
242 
243  // get min value
244  if( m_minExclusive ) {
245  v = m_minExclusive.get();
246  }
247  if( m_minInclusive ) {
248  if( !v || m_minInclusive.get() > v.get() ) {
249  v = m_minInclusive.get();
250  inclusive = true;
251  }
252  }
253 
254  // min value
255  if( v ) {
256 
258  {
259  range = FMT( "%g <", v.get() );
260  if( inclusive ) {
261  range.append( "=" );
262  }
263  range.append( " " );
264  range.append( NVL( name, "" ) );
265  }
266  else
267  {
268  // In the case where no maximum value is going to be expressed
269  // in the range string, it is better to put the name first.
270  range.append( NVL( name, "" ) );
271  range.append( " " );
272  if( inclusive ) {
273  range += FMT( ">= %g", v.get() );
274  }
275  else
276  {
277  range += FMT( "> %g", v.get() );
278  }
279  }
280  }
281 
282  // get max value
283  v.reset();
284  inclusive = false;
285 
286  if( m_maxExclusive ) {
287  v = m_maxExclusive.get();
288  }
289  if( m_maxInclusive ) {
290  if( !v || m_maxInclusive.get() < v.get() ) {
291  v = m_maxInclusive.get();
292  inclusive = true;
293  }
294  }
295 
296  // print maxvalue
297  if( v ) {
298  if( range.empty() ) {
299  range = NVL( name, "" );
300  }
301  range.append( " <" );
302  if( inclusive ) {
303  range.append( "=" );
304  }
305  range.append( FMT( " %g", v.get() ) );
306  }
307 
308  return range;
309 }
bool hasExclusiveMaxValue()
judges whether this argument exclusive max value.
void reset()
resets restriction
virtual ~NumberRestriction()
destructor
void setExclusiveMinValue(const double value)
sets exclusive min value
std::string getRangeStr(const char *name)
gets character string that means value range
double getUpperBound(const double dfValue=0.0)
gets upper bound value
int roundnum(const double v)
gets the closest integer to the argument
#define MIN(x, y)
Definition: CoreMacros.h:30
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
bool hasInclusiveMinValue()
judges whether this argument inclusive min value.
boost::optional< double > m_maxInclusive
void setInclusiveMinValue(const double value)
sets inclusive min value
bool hasExclusiveMinValue()
judges whether this argument exclusive min value.
double getInclusiveMinValue(const double dfValue=0.0)
gets min inclusive value
void setInclusiveMaxValue(const double value)
sets inclusive max value
#define MAX(x, y)
Definition: CoreMacros.h:27
double getInclusiveMaxValue(const double dfValue=0.0)
gets max inclusive value
boost::optional< double > m_maxExclusive
interfaces of NumberRestriction class
bool hasInclusiveMaxValue()
judges whether this argument inclusive max value.
boost::optional< double > m_minExclusive
void setExclusiveMaxValue(const double value)
sets exclusive max value
boost::optional< double > m_minInclusive
bool checkDoubleValue(const double value)
checks real number value is in the specified range
double getExclusiveMaxValue(const double dfValue=0.0)
gets max exclusive value
double getLowerBound(const double dfValue=0.0)
gets lower bound value
bool checkIntValue(const int value)
checks integer value is in the specified range
double getExclusiveMinValue(const double dfValue=0.0)
gets min exclusive value