bioformats  5.1.3
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 namespace ome
49 {
50  namespace common
51  {
52 
73  class boolean
74  {
75  public:
77  typedef uint8_t value_type;
78 
85  value(std::numeric_limits<uint8_t>::min()) // "false"
86  {}
87 
93  boolean(bool value):
94  value(value ? std::numeric_limits<uint8_t>::max() : std::numeric_limits<uint8_t>::min())
95  {}
96 
102  boolean(const boolean& value):
103  value(value)
104  {}
105 
111  operator bool() const
112  {
113  return static_cast<bool>(value);
114  }
115 
122  boolean&
123  operator=(bool rhs)
124  {
125  this->value = (rhs ? std::numeric_limits<uint8_t>::max() : std::numeric_limits<uint8_t>::min());
126  return *this;
127  }
128 
135  boolean&
136  operator=(const boolean& rhs)
137  {
138  this->value = rhs.value;
139  return *this;
140  }
141 
147  boolean
148  operator!() const
149  {
150  return !static_cast<bool>(*this);
151  }
152 
153  private:
155  uint8_t value;
156  };
157 
165  inline bool
166  operator==(const boolean& lhs,
167  bool rhs)
168  {
169  return static_cast<bool>(lhs) == rhs;
170  }
171 
179  inline bool
180  operator==(bool lhs,
181  const boolean& rhs)
182  {
183  return lhs == static_cast<bool>(rhs);
184  }
185 
193  inline bool
194  operator==(const boolean& lhs,
195  const boolean& rhs)
196  {
197  return static_cast<bool>(lhs) == static_cast<bool>(rhs);
198  }
199 
207  inline bool
208  operator!=(const boolean& lhs,
209  bool rhs)
210  {
211  return static_cast<bool>(lhs) != rhs;
212  }
213 
221  inline bool
222  operator!=(bool lhs,
223  const boolean& rhs)
224  {
225  return lhs != static_cast<bool>(rhs);
226  }
227 
235  inline bool
236  operator!=(const boolean& lhs,
237  const boolean& rhs)
238  {
239  return static_cast<bool>(lhs) != static_cast<bool>(rhs);
240  }
241 
249  template<class charT, class traits>
250  inline std::basic_ostream<charT,traits>&
251  operator<< (std::basic_ostream<charT,traits>& os,
252  const boolean& rhs)
253  {
254  return os << static_cast<bool>(rhs);
255  }
256 
264  template<class charT, class traits>
265  inline std::basic_istream<charT,traits>&
266  operator>> (std::basic_istream<charT,traits>& is,
267  boolean& rhs)
268  {
269 
270  bool b;
271  if (is >> b)
272  rhs = b;
273  return is;
274  }
275 
276  }
277 }
278 
279 #endif // OME_COMMON_BOOLEAN_H
280 
281 /*
282  * Local Variables:
283  * mode:C++
284  * End:
285  */
boolean operator!() const
Not operator.
Definition: boolean.h:148
uint8_t value
The boolean value.
Definition: boolean.h:155
boolean(bool value)
Construct with initial value.
Definition: boolean.h:93
bool operator!=(const boolean &lhs, bool rhs)
Compare boolean with bool for inequality.
Definition: boolean.h:208
STL namespace.
boolean()
Default construct.
Definition: boolean.h:84
bool operator==(const boolean &lhs, bool rhs)
Compare boolean with bool for equality.
Definition: boolean.h:166
Boolean type with guaranteed size, alignment and storage values.
Definition: boolean.h:73
boolean & operator=(const boolean &rhs)
Assign value.
Definition: boolean.h:136
Open Microscopy Environment C++ implementation.
Definition: CoreMetadata.cpp:40
boolean(const boolean &value)
Copy construct.
Definition: boolean.h:102
boolean & operator=(bool rhs)
Assign value.
Definition: boolean.h:123
uint8_t value_type
Value type for Boolean values.
Definition: boolean.h:77
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:266