ome-xml  5.2.0-m5
Quantity.h
1 /*
2  * #%L
3  * OME-XML C++ library for working with OME-XML metadata structures.
4  * %%
5  * Copyright © 2016 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_XML_MODEL_PRIMITIVES_QUANTITY_H
40 #define OME_XML_MODEL_PRIMITIVES_QUANTITY_H
41 
42 #include <boost/operators.hpp>
43 
44 namespace ome
45 {
46  namespace xml
47  {
48  namespace model
49  {
50  namespace primitives
51  {
52 
56  template<class Unit, typename Value = double>
57  class Quantity : private boost::partially_ordered<Quantity<Unit, Value>,
58  boost::equality_comparable<Quantity<Unit, Value>,
59  boost::addable<Quantity<Unit, Value>,
60  boost::subtractable<Quantity<Unit, Value>,
61  boost::dividable2<Quantity<Unit, Value>, Value,
62  boost::multipliable2<Quantity<Unit, Value>, Value,
63  boost::incrementable<Quantity<Unit, Value>,
64  boost::decrementable<Quantity<Unit, Value> > > > > > > > >
65  {
66  public:
68  typedef Unit unit_type;
70  typedef Value value_type;
71 
75  inline
77  value(),
78  unit(typename unit_type::enum_value(0))
79  {
80  }
81 
88  inline
89  Quantity (value_type value,
90  unit_type unit):
91  value(value),
92  unit(unit)
93  {
94  }
95 
101  inline
102  Quantity(const Quantity& quantity):
103  value(quantity.value),
104  unit(quantity.unit)
105  {
106  }
107 
109  inline
111  {
112  }
113 
119  value_type
120  getValue() const
121  {
122  return value;
123  }
124 
130  unit_type
131  getUnit() const
132  {
133  return unit;
134  }
135 
142  inline Quantity&
143  operator= (const Quantity& quantity)
144  {
145  this->value = quantity.value;
146  this->unit = quantity.unit;
147  return *this;
148  }
149 
156  inline Quantity&
157  operator+= (const Quantity& quantity)
158  {
159  this->value += convert_value(quantity, this->unit);
160  return *this;
161  }
162 
169  inline Quantity&
170  operator-= (const Quantity& quantity)
171  {
172  this->value -= convert_value(quantity, this->unit);
173  return *this;
174  }
175 
182  inline Quantity&
183  operator*= (const value_type& value)
184  {
185  this->value *= value;
186  return *this;
187  }
188 
195  inline Quantity&
196  operator/= (const value_type& value)
197  {
198  this->value /= value;
199  return *this;
200  }
201 
208  inline Quantity&
209  operator%= (const value_type& value)
210  {
211  this->value %= value;
212  return *this;
213  }
214 
220  inline Quantity&
222  {
223  ++this->value;
224  return *this;
225  }
226 
232  inline Quantity&
234  {
235  --this->value;
236  return *this;
237  }
238 
245  inline bool
246  operator< (const Quantity& quantity) const
247  {
248  return this->value < convert_value(quantity, this->unit);
249  }
250 
251  // Note operator> (const Quantity& value) const is
252  // provided by Boost.Operators.
253 
260  inline bool
261  operator== (const Quantity& quantity) const
262  {
263  return this->value == convert_value(quantity, this->unit);
264  }
265 
266  private:
274  value_type
275  convert_value(const Quantity& quantity,
276  unit_type unit) const;
277 
279  value_type value;
281  unit_type unit;
282  };
283 
293  template<typename T>
295  convert(const Quantity<T>& quantity,
296  typename Quantity<T>::unit_type unit);
297 
298  template<class Unit, typename Value>
299  inline typename Quantity<Unit, Value>::value_type
301  typename Quantity<Unit, Value>::unit_type unit) const
302  {
303  return convert<Quantity<Unit, Value> >(quantity, unit).getValue();
304  }
305 
313  template<class charT, class traits, typename Unit, typename Value>
314  inline std::basic_ostream<charT,traits>&
315  operator<< (std::basic_ostream<charT,traits>& os,
316  const Quantity<Unit, Value>& quantity)
317  {
318  return os << quantity.getValue() << ' ' << quantity.getUnit();
319  }
320 
328  template<class charT, class traits, typename Unit, typename Value>
329  inline std::basic_istream<charT,traits>&
330  operator>> (std::basic_istream<charT,traits>& is,
331  Quantity<Unit, Value>& quantity)
332  {
333  Value v;
334  Unit u(typename Unit::enum_value(0));
335  if (is >> v >> u)
336  {
337  quantity = Quantity<Unit, Value>(v, u);
338  }
339  return is;
340  }
341 
342  }
343  }
344  }
345 }
346 
347 #endif // OME_XML_MODEL_PRIMITIVES_QUANTITY_H
348 
349 /*
350  * Local Variables:
351  * mode:C++
352  * End:
353  */
Quantity(const Quantity &quantity)
Copy construct a Quantity.
Definition: Quantity.h:102
bool operator<(const Quantity &quantity) const
Check if the quantity is less than a quantity.
Definition: Quantity.h:246
Quantity & operator*=(const value_type &value)
Multiply the quantity by a value.
Definition: Quantity.h:183
value_type convert_value(const Quantity &quantity, unit_type unit) const
Convert a quantity to a specific unit.
Definition: Quantity.h:300
Quantity & operator=(const Quantity &quantity)
Assign the quantity from a quantity.
Definition: Quantity.h:143
~Quantity()
Destructor.
Definition: Quantity.h:110
UnitsElectricPotential enumeration.
Definition: ElectricPotentialQuantity.h:68
Quantity & operator/=(const value_type &value)
Divide the quantity by a value.
Definition: Quantity.h:196
Quantity & operator++()
Increment the quantity by one.
Definition: Quantity.h:221
Open Microscopy Environment C++ implementation.
Quantity()
Default construct a Quantity.
Definition: Quantity.h:76
Quantity & operator%=(const value_type &value)
Modulo of the quantity by a value.
Definition: Quantity.h:209
Unit unit_type
The type of a unit.
Definition: Quantity.h:68
Quantity(value_type value, unit_type unit)
Construct a Quantity from a value and unit.
Definition: Quantity.h:89
unit_type getUnit() const
Get the unit for this quantity.
Definition: Quantity.h:131
Quantity & operator-=(const Quantity &quantity)
Subtract a quantity from the quantity.
Definition: Quantity.h:170
value_type value
Quantity value.
Definition: Quantity.h:279
bool operator==(const Quantity &quantity) const
Check if the quantity is equal to a quantity.
Definition: Quantity.h:261
Quantity & operator--()
Decrement the quantity by one.
Definition: Quantity.h:233
A quantity of a defined unit.
Definition: Quantity.h:57
std::basic_istream< charT, traits > & operator>>(std::basic_istream< charT, traits > &is, Color &color)
Set Color from input stream.
Definition: Color.h:479
Value value_type
The type of a value.
Definition: Quantity.h:70
value_type getValue() const
Get the value for this quantity.
Definition: Quantity.h:120
Quantity & operator+=(const Quantity &quantity)
Add a quantity to the quantity.
Definition: Quantity.h:157
unit_type unit
Quantity unit.
Definition: Quantity.h:281