ome-common  5.3.0
boolean.h
1 /*
2  * #%L
3  * OME-COMMON C++ library for C++ compatibility/portability
4  * %%
5  * Copyright © 2006 - 2015 Open Microscopy Environment:
6  * - Massachusetts Institute of Technology
7  * - National Institutes of Health
8  * - University of Dundee
9  * - Board of Regents of the University of Wisconsin-Madison
10  * - Glencoe Software, Inc.
11  * %%
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * The views and conclusions contained in the software and documentation are
34  * those of the authors and should not be interpreted as representing official
35  * policies, either expressed or implied, of any organization.
36  * #L%
37  */
38 
39 #ifndef OME_COMMON_BOOLEAN_H
40 #define OME_COMMON_BOOLEAN_H
41 
42 #include <istream>
43 #include <limits>
44 #include <ostream>
45 
46 #include <ome/compat/cstdint.h>
47 
48 #ifdef _MSC_VER
49 #pragma push_macro("min")
50 #undef min
51 #pragma push_macro("max")
52 #undef max
53 #endif
54 
55 namespace ome
56 {
57  namespace common
58  {
59 
80  class boolean
81  {
82  public:
84  typedef uint8_t value_type;
85 
92  value(std::numeric_limits<uint8_t>::min()) // "false"
93  {}
94 
100  boolean(bool value):
101  value(value ? std::numeric_limits<uint8_t>::max() : std::numeric_limits<uint8_t>::min())
102  {}
103 
109  boolean(const boolean& value):
110  value(value.value)
111  {}
112 
118  operator bool() const
119  {
120  return value != std::numeric_limits<uint8_t>::min();
121  }
122 
129  boolean&
130  operator=(bool rhs)
131  {
132  this->value = (rhs ? std::numeric_limits<uint8_t>::max() : std::numeric_limits<uint8_t>::min());
133  return *this;
134  }
135 
142  boolean&
143  operator=(const boolean& rhs)
144  {
145  this->value = rhs.value;
146  return *this;
147  }
148 
154  boolean
155  operator!() const
156  {
157  return !static_cast<bool>(*this);
158  }
159 
160  private:
162  uint8_t value;
163  };
164 
172  inline bool
173  operator==(const boolean& lhs,
174  bool rhs)
175  {
176  return static_cast<bool>(lhs) == rhs;
177  }
178 
186  inline bool
187  operator==(bool lhs,
188  const boolean& rhs)
189  {
190  return lhs == static_cast<bool>(rhs);
191  }
192 
200  inline bool
201  operator==(const boolean& lhs,
202  const boolean& rhs)
203  {
204  return static_cast<bool>(lhs) == static_cast<bool>(rhs);
205  }
206 
214  inline bool
215  operator!=(const boolean& lhs,
216  bool rhs)
217  {
218  return static_cast<bool>(lhs) != rhs;
219  }
220 
228  inline bool
229  operator!=(bool lhs,
230  const boolean& rhs)
231  {
232  return lhs != static_cast<bool>(rhs);
233  }
234 
242  inline bool
243  operator!=(const boolean& lhs,
244  const boolean& rhs)
245  {
246  return static_cast<bool>(lhs) != static_cast<bool>(rhs);
247  }
248 
256  template<class charT, class traits>
257  inline std::basic_ostream<charT,traits>&
258  operator<< (std::basic_ostream<charT,traits>& os,
259  const boolean& rhs)
260  {
261  return os << static_cast<bool>(rhs);
262  }
263 
271  template<class charT, class traits>
272  inline std::basic_istream<charT,traits>&
273  operator>> (std::basic_istream<charT,traits>& is,
274  boolean& rhs)
275  {
276 
277  bool b;
278  if (is >> b)
279  rhs = b;
280  return is;
281  }
282 
283  }
284 }
285 
286 #ifdef _MSC_VER
287 #pragma pop_macro("min")
288 #pragma pop_macro("max")
289 #endif
290 
291 #endif // OME_COMMON_BOOLEAN_H
292 
293 /*
294  * Local Variables:
295  * mode:C++
296  * End:
297  */
boolean operator!() const
Not operator.
Definition: boolean.h:155
uint8_t value
The boolean value.
Definition: boolean.h:162
boolean(bool value)
Construct with initial value.
Definition: boolean.h:100
bool operator!=(const boolean &lhs, bool rhs)
Compare boolean with bool for inequality.
Definition: boolean.h:215
STL namespace.
boolean()
Default construct.
Definition: boolean.h:91
bool operator==(const boolean &lhs, bool rhs)
Compare boolean with bool for equality.
Definition: boolean.h:173
Boolean type with guaranteed size, alignment and storage values.
Definition: boolean.h:80
boolean & operator=(const boolean &rhs)
Assign value.
Definition: boolean.h:143
Definition: base64.h:49
boolean(const boolean &value)
Copy construct.
Definition: boolean.h:109
boolean & operator=(bool rhs)
Assign value.
Definition: boolean.h:130
uint8_t value_type
Value type for Boolean values.
Definition: boolean.h:84
Standard integer types.
std::basic_istream< charT, traits > & operator>>(std::basic_istream< charT, traits > &is, boolean &rhs)
Set boolean from input stream.
Definition: boolean.h:273