ome-common  5.5.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 <cstdint>
43 #include <istream>
44 #include <limits>
45 #include <ostream>
46 
47 #ifdef _MSC_VER
48 #pragma push_macro("min")
49 #undef min
50 #pragma push_macro("max")
51 #undef max
52 #endif
53 
54 namespace ome
55 {
56  namespace common
57  {
58 
79  class boolean
80  {
81  public:
83  typedef uint8_t value_type;
84 
91  value(std::numeric_limits<uint8_t>::min()) // "false"
92  {}
93 
99  boolean(bool value):
100  value(value ? std::numeric_limits<uint8_t>::max() : std::numeric_limits<uint8_t>::min())
101  {}
102 
108  boolean(const boolean& value):
109  value(value.value)
110  {}
111 
117  operator bool() const
118  {
119  return value != std::numeric_limits<uint8_t>::min();
120  }
121 
128  boolean&
129  operator=(bool rhs)
130  {
131  this->value = (rhs ? std::numeric_limits<uint8_t>::max() : std::numeric_limits<uint8_t>::min());
132  return *this;
133  }
134 
141  boolean&
142  operator=(const boolean& rhs)
143  {
144  this->value = rhs.value;
145  return *this;
146  }
147 
153  boolean
154  operator!() const
155  {
156  return !static_cast<bool>(*this);
157  }
158 
159  private:
161  uint8_t value;
162  };
163 
171  inline bool
172  operator==(const boolean& lhs,
173  bool rhs)
174  {
175  return static_cast<bool>(lhs) == rhs;
176  }
177 
185  inline bool
186  operator==(bool lhs,
187  const boolean& rhs)
188  {
189  return lhs == static_cast<bool>(rhs);
190  }
191 
199  inline bool
200  operator==(const boolean& lhs,
201  const boolean& rhs)
202  {
203  return static_cast<bool>(lhs) == static_cast<bool>(rhs);
204  }
205 
213  inline bool
214  operator!=(const boolean& lhs,
215  bool rhs)
216  {
217  return static_cast<bool>(lhs) != rhs;
218  }
219 
227  inline bool
228  operator!=(bool lhs,
229  const boolean& rhs)
230  {
231  return lhs != static_cast<bool>(rhs);
232  }
233 
241  inline bool
242  operator!=(const boolean& lhs,
243  const boolean& rhs)
244  {
245  return static_cast<bool>(lhs) != static_cast<bool>(rhs);
246  }
247 
255  template<class charT, class traits>
256  inline std::basic_ostream<charT,traits>&
257  operator<< (std::basic_ostream<charT,traits>& os,
258  const boolean& rhs)
259  {
260  return os << static_cast<bool>(rhs);
261  }
262 
270  template<class charT, class traits>
271  inline std::basic_istream<charT,traits>&
272  operator>> (std::basic_istream<charT,traits>& is,
273  boolean& rhs)
274  {
275 
276  bool b;
277  if (is >> b)
278  rhs = b;
279  return is;
280  }
281 
282  }
283 }
284 
285 #ifdef _MSC_VER
286 #pragma pop_macro("min")
287 #pragma pop_macro("max")
288 #endif
289 
290 #endif // OME_COMMON_BOOLEAN_H
291 
292 /*
293  * Local Variables:
294  * mode:C++
295  * End:
296  */
uint8_t value
The boolean value.
Definition: boolean.h:161
boolean(bool value)
Construct with initial value.
Definition: boolean.h:99
bool operator!=(const boolean &lhs, bool rhs)
Compare boolean with bool for inequality.
Definition: boolean.h:214
STL namespace.
boolean()
Default construct.
Definition: boolean.h:90
bool operator==(const boolean &lhs, bool rhs)
Compare boolean with bool for equality.
Definition: boolean.h:172
boolean operator!() const
Not operator.
Definition: boolean.h:154
Boolean type with guaranteed size, alignment and storage values.
Definition: boolean.h:79
boolean & operator=(const boolean &rhs)
Assign value.
Definition: boolean.h:142
Open Microscopy Environment C++.
Definition: base64.h:48
boolean(const boolean &value)
Copy construct.
Definition: boolean.h:108
boolean & operator=(bool rhs)
Assign value.
Definition: boolean.h:129
uint8_t value_type
Value type for Boolean values.
Definition: boolean.h:83
std::basic_istream< charT, traits > & operator>>(std::basic_istream< charT, traits > &is, boolean &rhs)
Set boolean from input stream.
Definition: boolean.h:272