ome-xml  5.6.0
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 
165  inline Quantity&
166  operator-= (const Quantity& quantity);
167 
174  inline Quantity&
175  operator*= (const value_type& value)
176  {
177  this->value *= value;
178  return *this;
179  }
180 
187  inline Quantity&
188  operator/= (const value_type& value)
189  {
190  this->value /= value;
191  return *this;
192  }
193 
200  inline Quantity&
201  operator%= (const value_type& value)
202  {
203  this->value %= value;
204  return *this;
205  }
206 
212  inline Quantity&
214  {
215  ++this->value;
216  return *this;
217  }
218 
224  inline Quantity&
226  {
227  --this->value;
228  return *this;
229  }
230 
237  inline bool
238  operator< (const Quantity& quantity) const;
239 
240  // Note operator> (const Quantity& value) const is
241  // provided by Boost.Operators.
242 
249  inline bool
250  operator== (const Quantity& quantity) const;
251 
252  private:
254  value_type value;
256  unit_type unit;
257  };
258 
266  template<typename Unit, typename Value>
268  {
279  operator() (const Quantity<Unit, Value>& quantity,
280  typename Quantity<Unit, Value>::unit_type unit) const;
281  };
282 
292  template<class Unit, typename Value>
293  inline
297  {
299  return conv(quantity, unit);
300  }
301 
302  template<class Unit, typename Value>
303  inline
306  {
307  this->value += convert(quantity, this->unit).getValue();
308  return *this;
309  }
310 
311  template<class Unit, typename Value>
312  inline
315  {
316  this->value -= convert(quantity, this->unit).getValue();
317  return *this;
318  }
319 
320  template<class Unit, typename Value>
321  inline
322  bool
324  {
325  return this->value < convert(quantity, this->unit).getValue();
326  }
327 
328  template<class Unit, typename Value>
329  inline
330  bool
332  {
333  return this->value == convert(quantity, this->unit).getValue();
334  }
335 
343  template<class charT, class traits, typename Unit, typename Value>
344  inline std::basic_ostream<charT,traits>&
345  operator<< (std::basic_ostream<charT,traits>& os,
346  const Quantity<Unit, Value>& quantity)
347  {
348  return os << quantity.getValue() << ' ' << quantity.getUnit();
349  }
350 
358  template<class charT, class traits, typename Unit, typename Value>
359  inline std::basic_istream<charT,traits>&
360  operator>> (std::basic_istream<charT,traits>& is,
361  Quantity<Unit, Value>& quantity)
362  {
363  Value v;
364  Unit u(typename Unit::enum_value(0));
365  if (is >> v >> u)
366  {
367  quantity = Quantity<Unit, Value>(v, u);
368  }
369  return is;
370  }
371 
372  }
373  }
374  }
375 }
376 
377 #endif // OME_XML_MODEL_PRIMITIVES_QUANTITY_H
378 
379 /*
380  * Local Variables:
381  * mode:C++
382  * End:
383  */
UnitsElectricPotential enumeration.
Definition: UnitsElectricPotential.h:74
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:323
Quantity & operator*=(const value_type &value)
Multiply the quantity by a value.
Definition: Quantity.h:175
Quantity< Unit, Value > convert(const Quantity< Unit, Value > &quantity, typename Quantity< Unit, Value >::unit_type unit)
Convert quantity to another unit.
Definition: Quantity.h:295
Quantity & operator=(const Quantity &quantity)
Assign the quantity from a quantity.
Definition: Quantity.h:143
~Quantity()
Destructor.
Definition: Quantity.h:110
Quantity & operator/=(const value_type &value)
Divide the quantity by a value.
Definition: Quantity.h:188
Quantity & operator++()
Increment the quantity by one.
Definition: Quantity.h:213
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:201
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
value_type value
Quantity value.
Definition: Quantity.h:254
Convert a quantity to a different unit.
Definition: Quantity.h:267
Quantity & operator--()
Decrement the quantity by one.
Definition: Quantity.h:225
bool operator==(const Quantity &quantity) const
Check if the quantity is equal to a quantity.
Definition: Quantity.h:331
Quantity & operator+=(const Quantity &quantity)
Add a quantity to the quantity.
Definition: Quantity.h:305
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:478
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)
Subtract a quantity from the quantity.
Definition: Quantity.h:314
unit_type getUnit() const
Get the unit for this quantity.
Definition: Quantity.h:131
unit_type unit
Quantity unit.
Definition: Quantity.h:256