1// Boost.Units - A C++ library for zero-overhead dimensional analysis and
2// unit/quantity manipulation and conversion
3//
4// Copyright (C) 2003-2008 Matthias Christian Schabel
5// Copyright (C) 2007-2008 Steven Watanabe
6//
7// Distributed under the Boost Software License, Version 1.0. (See
8// accompanying file LICENSE_1_0.txt or copy at
9// http://www.boost.org/LICENSE_1_0.txt)
10
11#ifndef BOOST_UNITS_QUANTITY_HPP
12#define BOOST_UNITS_QUANTITY_HPP
13
14#include <algorithm>
15
16#include <boost/config.hpp>
17#include <boost/static_assert.hpp>
18#include <boost/mpl/bool.hpp>
19#include <boost/mpl/and.hpp>
20#include <boost/mpl/not.hpp>
21#include <boost/mpl/or.hpp>
22#include <boost/mpl/assert.hpp>
23#include <boost/utility/enable_if.hpp>
24#include <boost/type_traits/is_arithmetic.hpp>
25#include <boost/type_traits/is_convertible.hpp>
26#include <boost/type_traits/is_integral.hpp>
27#include <boost/type_traits/is_same.hpp>
28
29#include <boost/units/conversion.hpp>
30#include <boost/units/dimensionless_type.hpp>
31#include <boost/units/homogeneous_system.hpp>
32#include <boost/units/operators.hpp>
33#include <boost/units/static_rational.hpp>
34#include <boost/units/units_fwd.hpp>
35#include <boost/units/detail/dimensionless_unit.hpp>
36
37namespace boost {
38
39namespace units {
40
41namespace detail {
42
43template<class T, class Enable = void>
44struct is_base_unit : mpl::false_ {};
45
46template<class T>
47struct is_base_unit<T, typename T::boost_units_is_base_unit_type> : mpl::true_ {};
48
49template<class Source, class Destination>
50struct is_narrowing_conversion_impl : mpl::bool_<(sizeof(Source) > sizeof(Destination))> {};
51
52template<class Source, class Destination>
53struct is_non_narrowing_conversion :
54 mpl::and_<
55 boost::is_convertible<Source, Destination>,
56 mpl::not_<
57 mpl::and_<
58 boost::is_arithmetic<Source>,
59 boost::is_arithmetic<Destination>,
60 mpl::or_<
61 mpl::and_<
62 is_integral<Destination>,
63 mpl::not_<is_integral<Source> >
64 >,
65 is_narrowing_conversion_impl<Source, Destination>
66 >
67 >
68 >
69 >
70{};
71
72template<>
73struct is_non_narrowing_conversion<long double, double> : mpl::false_ {};
74
75// msvc 7.1 needs extra disambiguation
76template<class T, class U>
77struct disable_if_is_same
78{
79 typedef void type;
80};
81
82template<class T>
83struct disable_if_is_same<T, T> {};
84
85}
86
87/// class declaration
88template<class Unit,class Y>
89class quantity
90{
91 // base units are not the same as units.
92 BOOST_MPL_ASSERT_NOT((detail::is_base_unit<Unit>));
93 enum { force_instantiation_of_unit = sizeof(Unit) };
94 typedef void (quantity::*unspecified_null_pointer_constant_type)(int*******);
95 public:
96 typedef quantity<Unit,Y> this_type;
97
98 typedef Y value_type;
99 typedef Unit unit_type;
100
101 BOOST_CONSTEXPR quantity() : val_()
102 {
103 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
104 }
105
106 BOOST_CONSTEXPR quantity(unspecified_null_pointer_constant_type) : val_()
107 {
108 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
109 }
110
111 BOOST_CONSTEXPR quantity(const this_type& source) : val_(source.val_)
112 {
113 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
114 }
115
116 // Need to make sure that the destructor of
117 // Unit which contains the checking is instantiated,
118 // on sun.
119 #ifdef __SUNPRO_CC
120 ~quantity() {
121 unit_type force_unit_instantiation;
122 }
123 #endif
124
125 //~quantity() { }
126
127 BOOST_CXX14_CONSTEXPR this_type& operator=(const this_type& source)
128 {
129 val_ = source.val_;
130
131 return *this;
132 }
133
134 #ifndef BOOST_NO_SFINAE
135
136 /// implicit conversion between value types is allowed if allowed for value types themselves
137 template<class YY>
138 BOOST_CONSTEXPR quantity(const quantity<Unit,YY>& source,
139 typename boost::enable_if<detail::is_non_narrowing_conversion<YY, Y> >::type* = 0) :
140 val_(source.value())
141 {
142 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
143 }
144
145 /// implicit conversion between value types is not allowed if not allowed for value types themselves
146 template<class YY>
147 explicit BOOST_CONSTEXPR quantity(const quantity<Unit,YY>& source,
148 typename boost::disable_if<detail::is_non_narrowing_conversion<YY, Y> >::type* = 0) :
149 val_(static_cast<Y>(source.value()))
150 {
151 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
152 }
153
154 #else
155
156 /// implicit conversion between value types is allowed if allowed for value types themselves
157 template<class YY>
158 BOOST_CONSTEXPR quantity(const quantity<Unit,YY>& source) :
159 val_(source.value())
160 {
161 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
162 BOOST_STATIC_ASSERT((boost::is_convertible<YY, Y>::value == true));
163 }
164
165 #endif
166
167 /// implicit assignment between value types is allowed if allowed for value types themselves
168 template<class YY>
169 BOOST_CXX14_CONSTEXPR this_type& operator=(const quantity<Unit,YY>& source)
170 {
171 BOOST_STATIC_ASSERT((boost::is_convertible<YY, Y>::value == true));
172
173 *this = this_type(source);
174
175 return *this;
176 }
177
178 #ifndef BOOST_NO_SFINAE
179
180 /// explicit conversion between different unit systems is allowed if implicit conversion is disallowed
181 template<class Unit2,class YY>
182 explicit
183 BOOST_CONSTEXPR quantity(const quantity<Unit2,YY>& source,
184 typename boost::disable_if<
185 mpl::and_<
186 //is_implicitly_convertible should be undefined when the
187 //units are not convertible at all
188 typename is_implicitly_convertible<Unit2,Unit>::type,
189 detail::is_non_narrowing_conversion<YY, Y>
190 >,
191 typename detail::disable_if_is_same<Unit, Unit2>::type
192 >::type* = 0)
193 : val_(conversion_helper<quantity<Unit2,YY>,this_type>::convert(source).value())
194 {
195 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
196 BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
197 }
198
199 /// implicit conversion between different unit systems is allowed if each fundamental dimension is implicitly convertible
200 template<class Unit2,class YY>
201 BOOST_CONSTEXPR quantity(const quantity<Unit2,YY>& source,
202 typename boost::enable_if<
203 mpl::and_<
204 typename is_implicitly_convertible<Unit2,Unit>::type,
205 detail::is_non_narrowing_conversion<YY, Y>
206 >,
207 typename detail::disable_if_is_same<Unit, Unit2>::type
208 >::type* = 0)
209 : val_(conversion_helper<quantity<Unit2,YY>,this_type>::convert(source).value())
210 {
211 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
212 BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
213 }
214
215 #else
216
217 /// without SFINAE we can't distinguish between explicit and implicit conversions so
218 /// the conversion is always explicit
219 template<class Unit2,class YY>
220 explicit BOOST_CONSTEXPR quantity(const quantity<Unit2,YY>& source)
221 : val_(conversion_helper<quantity<Unit2,YY>,this_type>::convert(source).value())
222 {
223 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
224 BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
225 }
226
227 #endif
228
229 /// implicit assignment between different unit systems is allowed if each fundamental dimension is implicitly convertible
230 template<class Unit2,class YY>
231 BOOST_CXX14_CONSTEXPR this_type& operator=(const quantity<Unit2,YY>& source)
232 {
233
234 BOOST_STATIC_ASSERT((is_implicitly_convertible<Unit2,unit_type>::value == true));
235 BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
236
237 *this = this_type(source);
238
239 return *this;
240 }
241
242 BOOST_CONSTEXPR const value_type& value() const { return val_; } ///< constant accessor to value
243
244 ///< can add a quantity of the same type if add_typeof_helper<value_type,value_type>::type is convertible to value_type
245 template<class Unit2, class YY>
246 BOOST_CXX14_CONSTEXPR this_type& operator+=(const quantity<Unit2, YY>& source)
247 {
248 BOOST_STATIC_ASSERT((boost::is_same<typename add_typeof_helper<Unit, Unit2>::type, Unit>::value));
249 val_ += source.value();
250 return *this;
251 }
252
253 ///< can subtract a quantity of the same type if subtract_typeof_helper<value_type,value_type>::type is convertible to value_type
254 template<class Unit2, class YY>
255 BOOST_CXX14_CONSTEXPR this_type& operator-=(const quantity<Unit2, YY>& source)
256 {
257 BOOST_STATIC_ASSERT((boost::is_same<typename subtract_typeof_helper<Unit, Unit2>::type, Unit>::value));
258 val_ -= source.value();
259 return *this;
260 }
261
262 template<class Unit2, class YY>
263 BOOST_CXX14_CONSTEXPR this_type& operator*=(const quantity<Unit2, YY>& source)
264 {
265 BOOST_STATIC_ASSERT((boost::is_same<typename multiply_typeof_helper<Unit, Unit2>::type, Unit>::value));
266 val_ *= source.value();
267 return *this;
268 }
269
270 template<class Unit2, class YY>
271 BOOST_CXX14_CONSTEXPR this_type& operator/=(const quantity<Unit2, YY>& source)
272 {
273 BOOST_STATIC_ASSERT((boost::is_same<typename divide_typeof_helper<Unit, Unit2>::type, Unit>::value));
274 val_ /= source.value();
275 return *this;
276 }
277
278 ///< can multiply a quantity by a scalar value_type if multiply_typeof_helper<value_type,value_type>::type is convertible to value_type
279 BOOST_CXX14_CONSTEXPR this_type& operator*=(const value_type& source) { val_ *= source; return *this; }
280 ///< can divide a quantity by a scalar value_type if divide_typeof_helper<value_type,value_type>::type is convertible to value_type
281 BOOST_CXX14_CONSTEXPR this_type& operator/=(const value_type& source) { val_ /= source; return *this; }
282
283 /// Construct quantity directly from @c value_type (potentially dangerous).
284 static BOOST_CONSTEXPR this_type from_value(const value_type& val) { return this_type(val, 0); }
285
286 protected:
287 explicit BOOST_CONSTEXPR quantity(const value_type& val, int) : val_(val) { }
288
289 private:
290 value_type val_;
291};
292
293/// Specialization for dimensionless quantities. Implicit conversions between
294/// unit systems are allowed because all dimensionless quantities are equivalent.
295/// Implicit construction and assignment from and conversion to @c value_type is
296/// also allowed.
297template<class System,class Y>
298class quantity<BOOST_UNITS_DIMENSIONLESS_UNIT(System),Y>
299{
300 public:
301 typedef quantity<unit<dimensionless_type,System>,Y> this_type;
302
303 typedef Y value_type;
304 typedef System system_type;
305 typedef dimensionless_type dimension_type;
306 typedef unit<dimension_type,system_type> unit_type;
307
308 BOOST_CONSTEXPR quantity() : val_()
309 {
310 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
311 }
312
313 /// construction from raw @c value_type is allowed
314 BOOST_CONSTEXPR quantity(value_type val) : val_(val)
315 {
316 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
317 }
318
319 BOOST_CONSTEXPR quantity(const this_type& source) : val_(source.val_)
320 {
321 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
322 }
323
324 //~quantity() { }
325
326 BOOST_CXX14_CONSTEXPR this_type& operator=(const this_type& source)
327 {
328 val_ = source.val_;
329
330 return *this;
331 }
332
333 #ifndef BOOST_NO_SFINAE
334
335 /// implicit conversion between value types is allowed if allowed for value types themselves
336 template<class YY>
337 BOOST_CONSTEXPR quantity(const quantity<unit<dimension_type,system_type>,YY>& source,
338 typename boost::enable_if<detail::is_non_narrowing_conversion<YY, Y> >::type* = 0) :
339 val_(source.value())
340 {
341 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
342 }
343
344 /// implicit conversion between value types is not allowed if not allowed for value types themselves
345 template<class YY>
346 explicit BOOST_CONSTEXPR quantity(const quantity<unit<dimension_type,system_type>,YY>& source,
347 typename boost::disable_if<detail::is_non_narrowing_conversion<YY, Y> >::type* = 0) :
348 val_(static_cast<Y>(source.value()))
349 {
350 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
351 }
352
353 #else
354
355 /// implicit conversion between value types is allowed if allowed for value types themselves
356 template<class YY>
357 BOOST_CONSTEXPR quantity(const quantity<unit<dimension_type,system_type>,YY>& source) :
358 val_(source.value())
359 {
360 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
361 BOOST_STATIC_ASSERT((boost::is_convertible<YY, Y>::value == true));
362 }
363
364 #endif
365
366 /// implicit assignment between value types is allowed if allowed for value types themselves
367 template<class YY>
368 BOOST_CXX14_CONSTEXPR this_type& operator=(const quantity<unit<dimension_type,system_type>,YY>& source)
369 {
370 BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
371
372 *this = this_type(source);
373
374 return *this;
375 }
376
377 #if 1
378
379 /// implicit conversion between different unit systems is allowed
380 template<class System2, class Y2>
381 BOOST_CONSTEXPR quantity(const quantity<unit<dimensionless_type, System2>,Y2>& source,
382 #ifdef __SUNPRO_CC
383 typename boost::enable_if<
384 boost::mpl::and_<
385 detail::is_non_narrowing_conversion<Y2, Y>,
386 detail::is_dimensionless_system<System2>
387 >
388 >::type* = 0
389 #else
390 typename boost::enable_if<detail::is_non_narrowing_conversion<Y2, Y> >::type* = 0,
391 typename detail::disable_if_is_same<System, System2>::type* = 0,
392 typename boost::enable_if<detail::is_dimensionless_system<System2> >::type* = 0
393 #endif
394 ) :
395 val_(source.value())
396 {
397 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
398 }
399
400 /// implicit conversion between different unit systems is allowed
401 template<class System2, class Y2>
402 explicit BOOST_CONSTEXPR quantity(const quantity<unit<dimensionless_type, System2>,Y2>& source,
403 #ifdef __SUNPRO_CC
404 typename boost::enable_if<
405 boost::mpl::and_<
406 boost::mpl::not_<detail::is_non_narrowing_conversion<Y2, Y> >,
407 detail::is_dimensionless_system<System2>
408 >
409 >::type* = 0
410 #else
411 typename boost::disable_if<detail::is_non_narrowing_conversion<Y2, Y> >::type* = 0,
412 typename detail::disable_if_is_same<System, System2>::type* = 0,
413 typename boost::enable_if<detail::is_dimensionless_system<System2> >::type* = 0
414 #endif
415 ) :
416 val_(static_cast<Y>(source.value()))
417 {
418 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
419 }
420
421 #else
422
423 /// implicit conversion between different unit systems is allowed
424 template<class System2, class Y2>
425 BOOST_CONSTEXPR quantity(const quantity<unit<dimensionless_type,homogeneous_system<System2> >,Y2>& source) :
426 val_(source.value())
427 {
428 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
429 BOOST_STATIC_ASSERT((boost::is_convertible<Y2, Y>::value == true));
430 }
431
432 #endif
433
434 /// conversion between different unit systems is explicit when
435 /// the units are not equivalent.
436 template<class System2, class Y2>
437 explicit BOOST_CONSTEXPR quantity(const quantity<unit<dimensionless_type, System2>,Y2>& source,
438 typename boost::disable_if<detail::is_dimensionless_system<System2> >::type* = 0) :
439 val_(conversion_helper<quantity<unit<dimensionless_type, System2>,Y2>, this_type>::convert(source).value())
440 {
441 BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
442 }
443
444 #ifndef __SUNPRO_CC
445
446 /// implicit assignment between different unit systems is allowed
447 template<class System2>
448 BOOST_CXX14_CONSTEXPR this_type& operator=(const quantity<BOOST_UNITS_DIMENSIONLESS_UNIT(System2),Y>& source)
449 {
450 *this = this_type(source);
451
452 return *this;
453 }
454
455 #endif
456
457 /// implicit conversion to @c value_type is allowed
458 BOOST_CONSTEXPR operator value_type() const { return val_; }
459
460 BOOST_CONSTEXPR const value_type& value() const { return val_; } ///< constant accessor to value
461
462 ///< can add a quantity of the same type if add_typeof_helper<value_type,value_type>::type is convertible to value_type
463 BOOST_CXX14_CONSTEXPR this_type& operator+=(const this_type& source) { val_ += source.val_; return *this; }
464
465 ///< can subtract a quantity of the same type if subtract_typeof_helper<value_type,value_type>::type is convertible to value_type
466 BOOST_CXX14_CONSTEXPR this_type& operator-=(const this_type& source) { val_ -= source.val_; return *this; }
467
468 ///< can multiply a quantity by a scalar value_type if multiply_typeof_helper<value_type,value_type>::type is convertible to value_type
469 BOOST_CXX14_CONSTEXPR this_type& operator*=(const value_type& val) { val_ *= val; return *this; }
470
471 ///< can divide a quantity by a scalar value_type if divide_typeof_helper<value_type,value_type>::type is convertible to value_type
472 BOOST_CXX14_CONSTEXPR this_type& operator/=(const value_type& val) { val_ /= val; return *this; }
473
474 /// Construct quantity directly from @c value_type.
475 static BOOST_CONSTEXPR this_type from_value(const value_type& val) { return this_type(val); }
476
477 private:
478 value_type val_;
479};
480
481#ifdef BOOST_MSVC
482// HACK: For some obscure reason msvc 8.0 needs these specializations
483template<class System, class T>
484class quantity<unit<int, System>, T> {};
485template<class T>
486class quantity<int, T> {};
487#endif
488
489} // namespace units
490
491} // namespace boost
492
493#if BOOST_UNITS_HAS_BOOST_TYPEOF
494
495#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
496
497BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::quantity, 2)
498
499#endif
500
501namespace boost {
502
503namespace units {
504
505namespace detail {
506
507/// helper class for quantity_cast
508template<class X,class Y> struct quantity_cast_helper;
509
510/// specialization for casting to the value type
511template<class Y,class X,class Unit>
512struct quantity_cast_helper<Y,quantity<Unit,X> >
513{
514 typedef Y type;
515
516 BOOST_CONSTEXPR type operator()(quantity<Unit,X>& source) const { return const_cast<X&>(source.value()); }
517};
518
519/// specialization for casting to the value type
520template<class Y,class X,class Unit>
521struct quantity_cast_helper<Y,const quantity<Unit,X> >
522{
523 typedef Y type;
524
525 BOOST_CONSTEXPR type operator()(const quantity<Unit,X>& source) const { return source.value(); }
526};
527
528} // namespace detail
529
530/// quantity_cast provides mutating access to underlying quantity value_type
531template<class X,class Y>
532inline
533BOOST_CONSTEXPR
534X
535quantity_cast(Y& source)
536{
537 return detail::quantity_cast_helper<X,Y>()(source);
538}
539
540template<class X,class Y>
541inline
542BOOST_CONSTEXPR
543X
544quantity_cast(const Y& source)
545{
546 return detail::quantity_cast_helper<X,const Y>()(source);
547}
548
549/// swap quantities
550template<class Unit,class Y>
551inline void swap(quantity<Unit,Y>& lhs, quantity<Unit,Y>& rhs)
552{
553 using std::swap;
554 swap(quantity_cast<Y&>(lhs),quantity_cast<Y&>(rhs));
555}
556
557/// specialize unary plus typeof helper
558/// INTERNAL ONLY
559template<class Unit,class Y>
560struct unary_plus_typeof_helper< quantity<Unit,Y> >
561{
562 typedef typename unary_plus_typeof_helper<Y>::type value_type;
563 typedef typename unary_plus_typeof_helper<Unit>::type unit_type;
564 typedef quantity<unit_type,value_type> type;
565};
566
567/// specialize unary minus typeof helper
568/// INTERNAL ONLY
569template<class Unit,class Y>
570struct unary_minus_typeof_helper< quantity<Unit,Y> >
571{
572 typedef typename unary_minus_typeof_helper<Y>::type value_type;
573 typedef typename unary_minus_typeof_helper<Unit>::type unit_type;
574 typedef quantity<unit_type,value_type> type;
575};
576
577/// specialize add typeof helper
578/// INTERNAL ONLY
579template<class Unit1,
580 class Unit2,
581 class X,
582 class Y>
583struct add_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >
584{
585 typedef typename add_typeof_helper<X,Y>::type value_type;
586 typedef typename add_typeof_helper<Unit1,Unit2>::type unit_type;
587 typedef quantity<unit_type,value_type> type;
588};
589
590/// for sun CC we need to invoke SFINAE at
591/// the top level, otherwise it will silently
592/// return int.
593template<class Dim1, class System1,
594 class Dim2, class System2,
595 class X,
596 class Y>
597struct add_typeof_helper< quantity<unit<Dim1, System1>,X>,quantity<unit<Dim2, System2>,Y> >
598{
599};
600
601template<class Dim,
602 class System,
603 class X,
604 class Y>
605struct add_typeof_helper< quantity<unit<Dim, System>,X>,quantity<unit<Dim, System>,Y> >
606{
607 typedef typename add_typeof_helper<X,Y>::type value_type;
608 typedef unit<Dim, System> unit_type;
609 typedef quantity<unit_type,value_type> type;
610};
611
612/// specialize subtract typeof helper
613/// INTERNAL ONLY
614template<class Unit1,
615 class Unit2,
616 class X,
617 class Y>
618struct subtract_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >
619{
620 typedef typename subtract_typeof_helper<X,Y>::type value_type;
621 typedef typename subtract_typeof_helper<Unit1,Unit2>::type unit_type;
622 typedef quantity<unit_type,value_type> type;
623};
624
625// Force adding different units to fail on sun.
626template<class Dim1, class System1,
627 class Dim2, class System2,
628 class X,
629 class Y>
630struct subtract_typeof_helper< quantity<unit<Dim1, System1>,X>,quantity<unit<Dim2, System2>,Y> >
631{
632};
633
634template<class Dim,
635 class System,
636 class X,
637 class Y>
638struct subtract_typeof_helper< quantity<unit<Dim, System>,X>,quantity<unit<Dim, System>,Y> >
639{
640 typedef typename subtract_typeof_helper<X,Y>::type value_type;
641 typedef unit<Dim, System> unit_type;
642 typedef quantity<unit_type,value_type> type;
643};
644
645/// scalar times unit typeof helper
646/// INTERNAL ONLY
647template<class System,
648 class Dim,
649 class X>
650struct multiply_typeof_helper< X,unit<Dim,System> >
651{
652 typedef X value_type;
653 typedef unit<Dim,System> unit_type;
654 typedef quantity<unit_type,value_type> type;
655};
656
657/// unit times scalar typeof helper
658/// INTERNAL ONLY
659template<class System,
660 class Dim,
661 class X>
662struct multiply_typeof_helper< unit<Dim,System>,X >
663{
664 typedef X value_type;
665 typedef unit<Dim,System> unit_type;
666 typedef quantity<unit_type,value_type> type;
667};
668
669/// scalar times quantity typeof helper
670/// INTERNAL ONLY
671template<class Unit,
672 class X,
673 class Y>
674struct multiply_typeof_helper< X,quantity<Unit,Y> >
675{
676 typedef typename multiply_typeof_helper<X,Y>::type value_type;
677 typedef Unit unit_type;
678 typedef quantity<unit_type,value_type> type;
679};
680
681/// disambiguate
682/// INTERNAL ONLY
683template<class Unit,
684 class Y>
685struct multiply_typeof_helper< one,quantity<Unit,Y> >
686{
687 typedef quantity<Unit,Y> type;
688};
689
690/// quantity times scalar typeof helper
691/// INTERNAL ONLY
692template<class Unit,
693 class X,
694 class Y>
695struct multiply_typeof_helper< quantity<Unit,X>,Y >
696{
697 typedef typename multiply_typeof_helper<X,Y>::type value_type;
698 typedef Unit unit_type;
699 typedef quantity<unit_type,value_type> type;
700};
701
702/// disambiguate
703/// INTERNAL ONLY
704template<class Unit,
705 class X>
706struct multiply_typeof_helper< quantity<Unit,X>,one >
707{
708 typedef quantity<Unit,X> type;
709};
710
711/// unit times quantity typeof helper
712/// INTERNAL ONLY
713template<class Unit,
714 class System,
715 class Dim,
716 class X>
717struct multiply_typeof_helper< unit<Dim,System>,quantity<Unit,X> >
718{
719 typedef X value_type;
720 typedef typename multiply_typeof_helper< unit<Dim,System>,Unit >::type unit_type;
721 typedef quantity<unit_type,value_type> type;
722};
723
724/// quantity times unit typeof helper
725/// INTERNAL ONLY
726template<class Unit,
727 class System,
728 class Dim,
729 class X>
730struct multiply_typeof_helper< quantity<Unit,X>,unit<Dim,System> >
731{
732 typedef X value_type;
733 typedef typename multiply_typeof_helper< Unit,unit<Dim,System> >::type unit_type;
734 typedef quantity<unit_type,value_type> type;
735};
736
737/// quantity times quantity typeof helper
738/// INTERNAL ONLY
739template<class Unit1,
740 class Unit2,
741 class X,
742 class Y>
743struct multiply_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >
744{
745 typedef typename multiply_typeof_helper<X,Y>::type value_type;
746 typedef typename multiply_typeof_helper<Unit1,Unit2>::type unit_type;
747 typedef quantity<unit_type,value_type> type;
748};
749
750/// scalar divided by unit typeof helper
751/// INTERNAL ONLY
752template<class System,
753 class Dim,
754 class X>
755struct divide_typeof_helper< X,unit<Dim,System> >
756{
757 typedef X value_type;
758 typedef typename power_typeof_helper< unit<Dim,System>,static_rational<-1> >::type unit_type;
759 typedef quantity<unit_type,value_type> type;
760};
761
762/// unit divided by scalar typeof helper
763/// INTERNAL ONLY
764template<class System,
765 class Dim,
766 class X>
767struct divide_typeof_helper< unit<Dim,System>,X >
768{
769 typedef typename divide_typeof_helper<X,X>::type value_type;
770 typedef unit<Dim,System> unit_type;
771 typedef quantity<unit_type,value_type> type;
772};
773
774/// scalar divided by quantity typeof helper
775/// INTERNAL ONLY
776template<class Unit,
777 class X,
778 class Y>
779struct divide_typeof_helper< X,quantity<Unit,Y> >
780{
781 typedef typename divide_typeof_helper<X,Y>::type value_type;
782 typedef typename power_typeof_helper< Unit,static_rational<-1> >::type unit_type;
783 typedef quantity<unit_type,value_type> type;
784};
785
786/// disambiguate
787/// INTERNAL ONLY
788template<class Unit,
789 class Y>
790struct divide_typeof_helper< one,quantity<Unit,Y> >
791{
792 typedef quantity<Unit,Y> type;
793};
794
795/// quantity divided by scalar typeof helper
796/// INTERNAL ONLY
797template<class Unit,
798 class X,
799 class Y>
800struct divide_typeof_helper< quantity<Unit,X>,Y >
801{
802 typedef typename divide_typeof_helper<X,Y>::type value_type;
803 typedef Unit unit_type;
804 typedef quantity<unit_type,value_type> type;
805};
806
807/// disambiguate
808/// INTERNAL ONLY
809template<class Unit,
810 class X>
811struct divide_typeof_helper< quantity<Unit,X>,one >
812{
813 typedef quantity<Unit,X> type;
814};
815
816/// unit divided by quantity typeof helper
817/// INTERNAL ONLY
818template<class Unit,
819 class System,
820 class Dim,
821 class X>
822struct divide_typeof_helper< unit<Dim,System>,quantity<Unit,X> >
823{
824 typedef typename divide_typeof_helper<X,X>::type value_type;
825 typedef typename divide_typeof_helper< unit<Dim,System>,Unit >::type unit_type;
826 typedef quantity<unit_type,value_type> type;
827};
828
829/// quantity divided by unit typeof helper
830/// INTERNAL ONLY
831template<class Unit,
832 class System,
833 class Dim,
834 class X>
835struct divide_typeof_helper< quantity<Unit,X>,unit<Dim,System> >
836{
837 typedef X value_type;
838 typedef typename divide_typeof_helper< Unit,unit<Dim,System> >::type unit_type;
839 typedef quantity<unit_type,value_type> type;
840};
841
842/// quantity divided by quantity typeof helper
843/// INTERNAL ONLY
844template<class Unit1,
845 class Unit2,
846 class X,
847 class Y>
848struct divide_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >
849{
850 typedef typename divide_typeof_helper<X,Y>::type value_type;
851 typedef typename divide_typeof_helper<Unit1,Unit2>::type unit_type;
852 typedef quantity<unit_type,value_type> type;
853};
854
855/// specialize power typeof helper
856/// INTERNAL ONLY
857template<class Unit,long N,long D,class Y>
858struct power_typeof_helper< quantity<Unit,Y>,static_rational<N,D> >
859{
860 typedef typename power_typeof_helper<Y,static_rational<N,D> >::type value_type;
861 typedef typename power_typeof_helper<Unit,static_rational<N,D> >::type unit_type;
862 typedef quantity<unit_type,value_type> type;
863
864 static BOOST_CONSTEXPR type value(const quantity<Unit,Y>& x)
865 {
866 return type::from_value(power_typeof_helper<Y,static_rational<N,D> >::value(x.value()));
867 }
868};
869
870/// specialize root typeof helper
871/// INTERNAL ONLY
872template<class Unit,long N,long D,class Y>
873struct root_typeof_helper< quantity<Unit,Y>,static_rational<N,D> >
874{
875 typedef typename root_typeof_helper<Y,static_rational<N,D> >::type value_type;
876 typedef typename root_typeof_helper<Unit,static_rational<N,D> >::type unit_type;
877 typedef quantity<unit_type,value_type> type;
878
879 static BOOST_CONSTEXPR type value(const quantity<Unit,Y>& x)
880 {
881 return type::from_value(root_typeof_helper<Y,static_rational<N,D> >::value(x.value()));
882 }
883};
884
885/// runtime unit times scalar
886/// INTERNAL ONLY
887template<class System,
888 class Dim,
889 class Y>
890inline
891BOOST_CONSTEXPR
892typename multiply_typeof_helper< unit<Dim,System>,Y >::type
893operator*(const unit<Dim,System>&,const Y& rhs)
894{
895 typedef typename multiply_typeof_helper< unit<Dim,System>,Y >::type type;
896
897 return type::from_value(rhs);
898}
899
900/// runtime unit divided by scalar
901template<class System,
902 class Dim,
903 class Y>
904inline
905BOOST_CONSTEXPR
906typename divide_typeof_helper< unit<Dim,System>,Y >::type
907operator/(const unit<Dim,System>&,const Y& rhs)
908{
909 typedef typename divide_typeof_helper<unit<Dim,System>,Y>::type type;
910
911 return type::from_value(Y(1)/rhs);
912}
913
914/// runtime scalar times unit
915template<class System,
916 class Dim,
917 class Y>
918inline
919BOOST_CONSTEXPR
920typename multiply_typeof_helper< Y,unit<Dim,System> >::type
921operator*(const Y& lhs,const unit<Dim,System>&)
922{
923 typedef typename multiply_typeof_helper< Y,unit<Dim,System> >::type type;
924
925 return type::from_value(lhs);
926}
927
928/// runtime scalar divided by unit
929template<class System,
930 class Dim,
931 class Y>
932inline
933BOOST_CONSTEXPR
934typename divide_typeof_helper< Y,unit<Dim,System> >::type
935operator/(const Y& lhs,const unit<Dim,System>&)
936{
937 typedef typename divide_typeof_helper< Y,unit<Dim,System> >::type type;
938
939 return type::from_value(lhs);
940}
941
942///// runtime quantity times scalar
943//template<class Unit,
944// class X,
945// class Y>
946//inline
947//BOOST_CONSTEXPR
948//typename multiply_typeof_helper< quantity<Unit,X>,Y >::type
949//operator*(const quantity<Unit,X>& lhs,const Y& rhs)
950//{
951// typedef typename multiply_typeof_helper< quantity<Unit,X>,Y >::type type;
952//
953// return type::from_value(lhs.value()*rhs);
954//}
955//
956///// runtime scalar times quantity
957//template<class Unit,
958// class X,
959// class Y>
960//inline
961//BOOST_CONSTEXPR
962//typename multiply_typeof_helper< X,quantity<Unit,Y> >::type
963//operator*(const X& lhs,const quantity<Unit,Y>& rhs)
964//{
965// typedef typename multiply_typeof_helper< X,quantity<Unit,Y> >::type type;
966//
967// return type::from_value(lhs*rhs.value());
968//}
969
970/// runtime quantity times scalar
971template<class Unit,
972 class X>
973inline
974BOOST_CONSTEXPR
975typename multiply_typeof_helper< quantity<Unit,X>,X >::type
976operator*(const quantity<Unit,X>& lhs,const X& rhs)
977{
978 typedef typename multiply_typeof_helper< quantity<Unit,X>,X >::type type;
979
980 return type::from_value(lhs.value()*rhs);
981}
982
983/// runtime scalar times quantity
984template<class Unit,
985 class X>
986inline
987BOOST_CONSTEXPR
988typename multiply_typeof_helper< X,quantity<Unit,X> >::type
989operator*(const X& lhs,const quantity<Unit,X>& rhs)
990{
991 typedef typename multiply_typeof_helper< X,quantity<Unit,X> >::type type;
992
993 return type::from_value(lhs*rhs.value());
994}
995
996///// runtime quantity divided by scalar
997//template<class Unit,
998// class X,
999// class Y>
1000//inline
1001//BOOST_CONSTEXPR
1002//typename divide_typeof_helper< quantity<Unit,X>,Y >::type
1003//operator/(const quantity<Unit,X>& lhs,const Y& rhs)
1004//{
1005// typedef typename divide_typeof_helper< quantity<Unit,X>,Y >::type type;
1006//
1007// return type::from_value(lhs.value()/rhs);
1008//}
1009//
1010///// runtime scalar divided by quantity
1011//template<class Unit,
1012// class X,
1013// class Y>
1014//inline
1015//BOOST_CONSTEXPR
1016//typename divide_typeof_helper< X,quantity<Unit,Y> >::type
1017//operator/(const X& lhs,const quantity<Unit,Y>& rhs)
1018//{
1019// typedef typename divide_typeof_helper< X,quantity<Unit,Y> >::type type;
1020//
1021// return type::from_value(lhs/rhs.value());
1022//}
1023
1024/// runtime quantity divided by scalar
1025template<class Unit,
1026 class X>
1027inline
1028BOOST_CONSTEXPR
1029typename divide_typeof_helper< quantity<Unit,X>,X >::type
1030operator/(const quantity<Unit,X>& lhs,const X& rhs)
1031{
1032 typedef typename divide_typeof_helper< quantity<Unit,X>,X >::type type;
1033
1034 return type::from_value(lhs.value()/rhs);
1035}
1036
1037/// runtime scalar divided by quantity
1038template<class Unit,
1039 class X>
1040inline
1041BOOST_CONSTEXPR
1042typename divide_typeof_helper< X,quantity<Unit,X> >::type
1043operator/(const X& lhs,const quantity<Unit,X>& rhs)
1044{
1045 typedef typename divide_typeof_helper< X,quantity<Unit,X> >::type type;
1046
1047 return type::from_value(lhs/rhs.value());
1048}
1049
1050/// runtime unit times quantity
1051template<class System1,
1052 class Dim1,
1053 class Unit2,
1054 class Y>
1055inline
1056BOOST_CONSTEXPR
1057typename multiply_typeof_helper< unit<Dim1,System1>,quantity<Unit2,Y> >::type
1058operator*(const unit<Dim1,System1>&,const quantity<Unit2,Y>& rhs)
1059{
1060 typedef typename multiply_typeof_helper< unit<Dim1,System1>,quantity<Unit2,Y> >::type type;
1061
1062 return type::from_value(rhs.value());
1063}
1064
1065/// runtime unit divided by quantity
1066template<class System1,
1067 class Dim1,
1068 class Unit2,
1069 class Y>
1070inline
1071BOOST_CONSTEXPR
1072typename divide_typeof_helper< unit<Dim1,System1>,quantity<Unit2,Y> >::type
1073operator/(const unit<Dim1,System1>&,const quantity<Unit2,Y>& rhs)
1074{
1075 typedef typename divide_typeof_helper< unit<Dim1,System1>,quantity<Unit2,Y> >::type type;
1076
1077 return type::from_value(Y(1)/rhs.value());
1078}
1079
1080/// runtime quantity times unit
1081template<class Unit1,
1082 class System2,
1083 class Dim2,
1084 class Y>
1085inline
1086BOOST_CONSTEXPR
1087typename multiply_typeof_helper< quantity<Unit1,Y>,unit<Dim2,System2> >::type
1088operator*(const quantity<Unit1,Y>& lhs,const unit<Dim2,System2>&)
1089{
1090 typedef typename multiply_typeof_helper< quantity<Unit1,Y>,unit<Dim2,System2> >::type type;
1091
1092 return type::from_value(lhs.value());
1093}
1094
1095/// runtime quantity divided by unit
1096template<class Unit1,
1097 class System2,
1098 class Dim2,
1099 class Y>
1100inline
1101BOOST_CONSTEXPR
1102typename divide_typeof_helper< quantity<Unit1,Y>,unit<Dim2,System2> >::type
1103operator/(const quantity<Unit1,Y>& lhs,const unit<Dim2,System2>&)
1104{
1105 typedef typename divide_typeof_helper< quantity<Unit1,Y>,unit<Dim2,System2> >::type type;
1106
1107 return type::from_value(lhs.value());
1108}
1109
1110/// runtime unary plus quantity
1111template<class Unit,class Y>
1112BOOST_CONSTEXPR
1113typename unary_plus_typeof_helper< quantity<Unit,Y> >::type
1114operator+(const quantity<Unit,Y>& val)
1115{
1116 typedef typename unary_plus_typeof_helper< quantity<Unit,Y> >::type type;
1117
1118 return type::from_value(+val.value());
1119}
1120
1121/// runtime unary minus quantity
1122template<class Unit,class Y>
1123BOOST_CONSTEXPR
1124typename unary_minus_typeof_helper< quantity<Unit,Y> >::type
1125operator-(const quantity<Unit,Y>& val)
1126{
1127 typedef typename unary_minus_typeof_helper< quantity<Unit,Y> >::type type;
1128
1129 return type::from_value(-val.value());
1130}
1131
1132/// runtime quantity plus quantity
1133template<class Unit1,
1134 class Unit2,
1135 class X,
1136 class Y>
1137inline
1138BOOST_CONSTEXPR
1139typename add_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type
1140operator+(const quantity<Unit1,X>& lhs,
1141 const quantity<Unit2,Y>& rhs)
1142{
1143 typedef typename add_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type type;
1144
1145 return type::from_value(lhs.value()+rhs.value());
1146}
1147
1148/// runtime quantity minus quantity
1149template<class Unit1,
1150 class Unit2,
1151 class X,
1152 class Y>
1153inline
1154BOOST_CONSTEXPR
1155typename subtract_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type
1156operator-(const quantity<Unit1,X>& lhs,
1157 const quantity<Unit2,Y>& rhs)
1158{
1159 typedef typename subtract_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type type;
1160
1161 return type::from_value(lhs.value()-rhs.value());
1162}
1163
1164/// runtime quantity times quantity
1165template<class Unit1,
1166 class Unit2,
1167 class X,
1168 class Y>
1169inline
1170BOOST_CONSTEXPR
1171typename multiply_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type
1172operator*(const quantity<Unit1,X>& lhs,
1173 const quantity<Unit2,Y>& rhs)
1174{
1175 typedef typename multiply_typeof_helper< quantity<Unit1,X>,
1176 quantity<Unit2,Y> >::type type;
1177
1178 return type::from_value(lhs.value()*rhs.value());
1179}
1180
1181/// runtime quantity divided by quantity
1182template<class Unit1,
1183 class Unit2,
1184 class X,
1185 class Y>
1186inline
1187BOOST_CONSTEXPR
1188typename divide_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type
1189operator/(const quantity<Unit1,X>& lhs,
1190 const quantity<Unit2,Y>& rhs)
1191{
1192 typedef typename divide_typeof_helper< quantity<Unit1,X>,
1193 quantity<Unit2,Y> >::type type;
1194
1195 return type::from_value(lhs.value()/rhs.value());
1196}
1197
1198/// runtime operator==
1199template<class Unit,
1200 class X,
1201 class Y>
1202inline
1203BOOST_CONSTEXPR
1204bool
1205operator==(const quantity<Unit,X>& val1,
1206 const quantity<Unit,Y>& val2)
1207{
1208 return val1.value() == val2.value();
1209}
1210
1211/// runtime operator!=
1212template<class Unit,
1213 class X,
1214 class Y>
1215inline
1216BOOST_CONSTEXPR
1217bool
1218operator!=(const quantity<Unit,X>& val1,
1219 const quantity<Unit,Y>& val2)
1220{
1221 return val1.value() != val2.value();
1222}
1223
1224/// runtime operator<
1225template<class Unit,
1226 class X,
1227 class Y>
1228inline
1229BOOST_CONSTEXPR
1230bool
1231operator<(const quantity<Unit,X>& val1,
1232 const quantity<Unit,Y>& val2)
1233{
1234 return val1.value() < val2.value();
1235}
1236
1237/// runtime operator<=
1238template<class Unit,
1239 class X,
1240 class Y>
1241inline
1242BOOST_CONSTEXPR
1243bool
1244operator<=(const quantity<Unit,X>& val1,
1245 const quantity<Unit,Y>& val2)
1246{
1247 return val1.value() <= val2.value();
1248}
1249
1250/// runtime operator>
1251template<class Unit,
1252 class X,
1253 class Y>
1254inline
1255BOOST_CONSTEXPR
1256bool
1257operator>(const quantity<Unit,X>& val1,
1258 const quantity<Unit,Y>& val2)
1259{
1260 return val1.value() > val2.value();
1261}
1262
1263/// runtime operator>=
1264template<class Unit,
1265 class X,
1266 class Y>
1267inline
1268BOOST_CONSTEXPR
1269bool
1270operator>=(const quantity<Unit,X>& val1,
1271 const quantity<Unit,Y>& val2)
1272{
1273 return val1.value() >= val2.value();
1274}
1275
1276} // namespace units
1277
1278} // namespace boost
1279
1280#endif // BOOST_UNITS_QUANTITY_HPP
1281

source code of boost/libs/units/include/boost/units/quantity.hpp