FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
quantity.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** FougTools
3 ** Copyright Fougue (1 Mar. 2011)
4 ** contact@fougue.pro
5 **
6 ** This software is a computer program whose purpose is to provide utility
7 ** tools for the C++ language and the Qt toolkit.
8 **
9 ** This software is governed by the CeCILL-C license under French law and
10 ** abiding by the rules of distribution of free software. You can use,
11 ** modify and/ or redistribute the software under the terms of the CeCILL-C
12 ** license as circulated by CEA, CNRS and INRIA at the following URL
13 ** "http://www.cecill.info".
14 ****************************************************************************/
15 
16 #pragma once
17 
18 namespace cpp {
19 
20 template<typename NUMERIC_TRAITS, typename TRAIT = void>
21 class Quantity
22 {
23 public:
24  typedef typename NUMERIC_TRAITS::Type NumericType;
26 
27  Quantity();
28  explicit Quantity(NumericType v);
29  Quantity(const QuantityType& other);
30 
31  //operator NumericType() const;
32  NumericType value() const;
33  void setValue(NumericType v);
34 
35  QuantityType& operator=(const QuantityType& other);
36  QuantityType& operator+=(const QuantityType& other);
37  QuantityType& operator-=(const QuantityType& other);
38  QuantityType& operator*=(const QuantityType& other);
39  QuantityType& operator/=(const QuantityType& other);
40 
41  QuantityType& operator+=(NumericType v);
42  QuantityType& operator-=(NumericType v);
43  QuantityType& operator*=(NumericType v);
44  QuantityType& operator/=(NumericType v);
45 
46 private:
47  NumericType m_value;
48 };
49 
50 // Operator <
51 template<typename NUMERIC_TRAITS, typename TRAIT>
52 bool operator<(const Quantity<NUMERIC_TRAITS, TRAIT>& lhs,
54 
55 // Operator >
56 template<typename NUMERIC_TRAITS, typename TRAIT>
59 
60 // Operator +
61 template<typename NUMERIC_TRAITS, typename TRAIT>
65 
66 template<typename NUMERIC_TRAITS, typename TRAIT>
69  typename NUMERIC_TRAITS::Type k);
70 
71 template<typename NUMERIC_TRAITS, typename TRAIT>
73  typename NUMERIC_TRAITS::Type k,
75 
76 // Operator -
77 template<typename NUMERIC_TRAITS, typename TRAIT>
81 
82 template<typename NUMERIC_TRAITS, typename TRAIT>
85  typename NUMERIC_TRAITS::Type k);
86 
87 template<typename NUMERIC_TRAITS, typename TRAIT>
89  typename NUMERIC_TRAITS::Type k,
91 
92 // Operator *
93 template<typename NUMERIC_TRAITS, typename TRAIT>
97 
98 template<typename NUMERIC_TRAITS, typename TRAIT>
101  typename NUMERIC_TRAITS::Type k);
102 
103 template<typename NUMERIC_TRAITS, typename TRAIT>
105  typename NUMERIC_TRAITS::Type k,
107 
108 // Operator /
109 template<typename NUMERIC_TRAITS, typename TRAIT>
113 
114 template<typename NUMERIC_TRAITS, typename TRAIT>
117  typename NUMERIC_TRAITS::Type k);
118 
119 template<typename NUMERIC_TRAITS, typename TRAIT>
121  typename NUMERIC_TRAITS::Type k,
123 
124 template<typename TYPE>
126 {
127  typedef TYPE Type;
128  static TYPE zero()
129  { return static_cast<TYPE>(0); }
130 };
131 
135 
136 // --
137 // -- Implementation
138 // --
139 
148 template<typename NUMERIC_TRAITS, typename TRAIT>
150  : m_value(NUMERIC_TRAITS::zero())
151 {
152 }
153 
154 template<typename NUMERIC_TRAITS, typename TRAIT>
156  : m_value(v)
157 {
158 }
159 
160 template<typename NUMERIC_TRAITS, typename TRAIT>
162  : m_value(other.value())
163 {
164 }
165 
166 //template<typename NUMERIC_TRAITS, typename TRAIT>
167 //Quantity<NUMERIC_TRAITS, TRAIT>::operator typename NUMERIC_TRAITS::Type() const
168 //{
169 // return m_value;
170 //}
171 
172 template<typename NUMERIC_TRAITS, typename TRAIT>
173 typename NUMERIC_TRAITS::Type Quantity<NUMERIC_TRAITS, TRAIT>::value() const
174 {
175  return m_value;
176 }
177 
178 template<typename NUMERIC_TRAITS, typename TRAIT>
180 {
181  m_value = v;
182 }
183 
184 template<typename NUMERIC_TRAITS, typename TRAIT>
187 {
188  if (this != &other)
189  m_value = other.value();
190  return *this;
191 }
192 
193 template<typename NUMERIC_TRAITS, typename TRAIT>
196 {
197  if (this != &other)
198  m_value += other.value();
199  return *this;
200 }
201 
202 template<typename NUMERIC_TRAITS, typename TRAIT>
205 {
206  if (this != &other)
207  m_value -= other.value();
208  return *this;
209 }
210 
211 template<typename NUMERIC_TRAITS, typename TRAIT>
214 {
215  if (this != &other)
216  m_value *= other.value();
217  return *this;
218 }
219 
220 template<typename NUMERIC_TRAITS, typename TRAIT>
223 {
224  if (this != &other)
225  m_value /= other.value();
226  return *this;
227 }
228 
229 template<typename NUMERIC_TRAITS, typename TRAIT>
232 {
233  m_value += v;
234  return *this;
235 }
236 
237 template<typename NUMERIC_TRAITS, typename TRAIT>
240 {
241  m_value -= v;
242  return *this;
243 }
244 
245 template<typename NUMERIC_TRAITS, typename TRAIT>
248 {
249  m_value *= v;
250  return *this;
251 }
252 
253 template<typename NUMERIC_TRAITS, typename TRAIT>
255 {
256  m_value /= v;
257  return *this;
258 }
259 
260 // Operator <
262 template<typename NUMERIC_TRAITS, typename TRAIT>
263 bool operator<(const Quantity<NUMERIC_TRAITS, TRAIT>& lhs,
265 {
266  if (&lhs == &rhs)
267  return false;
268  return lhs.value() < rhs.value();
269 }
270 
271 // Operator >
273 template<typename NUMERIC_TRAITS, typename TRAIT>
276 {
277  if (&lhs == &rhs)
278  return false;
279  return lhs.value() > rhs.value();
280 }
281 
282 // Operator +
283 
285 template<typename NUMERIC_TRAITS, typename TRAIT>
289 {
290  return Quantity<NUMERIC_TRAITS, TRAIT>(lhs) += rhs;
291 }
292 
294 template<typename NUMERIC_TRAITS, typename TRAIT>
297  typename NUMERIC_TRAITS::Type k)
298 {
299  return Quantity<NUMERIC_TRAITS, TRAIT>(lhs) += k;
300 }
301 
303 template<typename NUMERIC_TRAITS, typename TRAIT>
305  typename NUMERIC_TRAITS::Type k,
307 {
308  return Quantity<NUMERIC_TRAITS, TRAIT>(rhs) += k;
309 }
310 
311 // Operator -
312 
314 template<typename NUMERIC_TRAITS, typename TRAIT>
318 {
319  return Quantity<NUMERIC_TRAITS, TRAIT>(lhs) -= rhs;
320 }
321 
323 template<typename NUMERIC_TRAITS, typename TRAIT>
326  typename NUMERIC_TRAITS::Type k)
327 {
328  return Quantity<NUMERIC_TRAITS, TRAIT>(lhs) -= k;
329 }
330 
332 template<typename NUMERIC_TRAITS, typename TRAIT>
334  typename NUMERIC_TRAITS::Type k,
336 {
337  return Quantity<NUMERIC_TRAITS, TRAIT>(rhs) -= k;
338 }
339 
340 // Operator *
341 
343 template<typename NUMERIC_TRAITS, typename TRAIT>
347 {
348  return Quantity<NUMERIC_TRAITS, TRAIT>(lhs) *= rhs;
349 }
350 
352 template<typename NUMERIC_TRAITS, typename TRAIT>
355  typename NUMERIC_TRAITS::Type k)
356 {
357  return Quantity<NUMERIC_TRAITS, TRAIT>(lhs) *= k;
358 }
359 
361 template<typename NUMERIC_TRAITS, typename TRAIT>
363  typename NUMERIC_TRAITS::Type k,
365 {
366  return Quantity<NUMERIC_TRAITS, TRAIT>(rhs) *= k;
367 }
368 
369 // Operator /
370 
372 template<typename NUMERIC_TRAITS, typename TRAIT>
376 {
377  return Quantity<NUMERIC_TRAITS, TRAIT>(lhs) /= rhs;
378 }
379 
381 template<typename NUMERIC_TRAITS, typename TRAIT>
384  typename NUMERIC_TRAITS::Type k)
385 {
386  return Quantity<NUMERIC_TRAITS, TRAIT>(lhs) /= k;
387 }
388 
390 template<typename NUMERIC_TRAITS, typename TRAIT>
392  typename NUMERIC_TRAITS::Type k,
394 {
395  return Quantity<NUMERIC_TRAITS, TRAIT>(rhs) /= k;
396 }
397 
398 } // namespace cpp
QuantityType & operator/=(const QuantityType &other)
Definition: quantity.h:222
Quantity()
Definition: quantity.h:149
Definition: basic_shared_pointer.h:20
bool operator>(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
Definition: quantity.h:274
const Quantity< NUMERIC_TRAITS, TRAIT > operator/(typename NUMERIC_TRAITS::Type k, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
Definition: quantity.h:391
QuantityType & operator+=(const QuantityType &other)
Definition: quantity.h:195
QuantityType & operator=(const QuantityType &other)
Definition: quantity.h:186
const Quantity< NUMERIC_TRAITS, TRAIT > operator/(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
TYPE Type
Definition: quantity.h:127
const Quantity< NUMERIC_TRAITS, TRAIT > operator-(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, typename NUMERIC_TRAITS::Type k)
Definition: quantity.h:324
NumericTraits< float > FloatNumericTraits
Definition: quantity.h:133
const Quantity< NUMERIC_TRAITS, TRAIT > operator/(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, typename NUMERIC_TRAITS::Type k)
Definition: quantity.h:382
const Quantity< NUMERIC_TRAITS, TRAIT > operator-(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
QuantityType & operator*=(const QuantityType &other)
Definition: quantity.h:213
const Quantity< NUMERIC_TRAITS, TRAIT > operator*(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, typename NUMERIC_TRAITS::Type k)
Definition: quantity.h:353
static TYPE zero()
Definition: quantity.h:128
const Quantity< NUMERIC_TRAITS, TRAIT > operator/(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
Definition: quantity.h:373
QuantityType & operator-=(const QuantityType &other)
Definition: quantity.h:204
const Quantity< NUMERIC_TRAITS, TRAIT > operator+(typename NUMERIC_TRAITS::Type k, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
Definition: quantity.h:304
T zero()
const Quantity< NUMERIC_TRAITS, TRAIT > operator-(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
Definition: quantity.h:315
Definition: quantity.h:125
NumericTraits< int > IntNumericTraits
Definition: quantity.h:134
NUMERIC_TRAITS::Type NumericType
Definition: quantity.h:24
const Quantity< NUMERIC_TRAITS, TRAIT > operator+(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, typename NUMERIC_TRAITS::Type k)
Definition: quantity.h:295
const Quantity< NUMERIC_TRAITS, TRAIT > operator+(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
const Quantity< NUMERIC_TRAITS, TRAIT > operator*(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
const Quantity< NUMERIC_TRAITS, TRAIT > operator*(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
Definition: quantity.h:344
const Quantity< NUMERIC_TRAITS, TRAIT > operator+(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
Definition: quantity.h:286
Represents an amount of a certain unit.
Definition: quantity.h:21
NumericTraits< double > DoubleNumericTraits
Definition: quantity.h:132
Quantity< NUMERIC_TRAITS, TRAIT > QuantityType
Definition: quantity.h:25
NumericType value() const
Definition: quantity.h:173
void setValue(NumericType v)
Definition: quantity.h:179
bool operator>(const Quantity< NUMERIC_TRAITS, TRAIT > &lhs, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
const Quantity< NUMERIC_TRAITS, TRAIT > operator-(typename NUMERIC_TRAITS::Type k, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
Definition: quantity.h:333
const Quantity< NUMERIC_TRAITS, TRAIT > operator*(typename NUMERIC_TRAITS::Type k, const Quantity< NUMERIC_TRAITS, TRAIT > &rhs)
Definition: quantity.h:362