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_SCALE_HPP_INCLUDED
12 #define BOOST_UNITS_SCALE_HPP_INCLUDED
13 
14 ///
15 /// \file
16 /// \brief 10^3 Engineering & 2^10 binary scaling factors for autoprefixing.
17 /// \details
18 ///
19 
20 #include <string>
21 
22 #include <boost/units/config.hpp>
23 #include <boost/units/static_rational.hpp>
24 #include <boost/units/units_fwd.hpp>
25 #include <boost/units/detail/one.hpp>
26 #include <boost/units/detail/static_rational_power.hpp>
27 
28 namespace boost {
29 
30 namespace units {
31 
32 template<class S, class Scale>
33 struct scaled_base_unit;
34 
35 /// class representing a scaling factor such as 10^3
36 /// The exponent must be a static rational.
37 template<long Base, class Exponent>
38 struct scale
39 {
40     BOOST_STATIC_CONSTEXPR long base = Base;
41     typedef Exponent exponent;
42     typedef double value_type;
valueboost::units::scale43     static BOOST_CONSTEXPR value_type value() { return(detail::static_rational_power<Exponent>(static_cast<double>(base))); }
44     // These need to be defined in specializations for
45     // printing to work.
46     // static std::string name();
47     // static std::string symbol();
48 };
49 
50 template<long Base, class Exponent>
51 BOOST_CONSTEXPR_OR_CONST long scale<Base, Exponent>::base;
52 
53 /// INTERNAL ONLY
54 template<long Base>
55 struct scale<Base, static_rational<0> >
56 {
57     BOOST_STATIC_CONSTEXPR long base = Base;
58     typedef static_rational<0> exponent;
59     typedef one value_type;
valueboost::units::scale60     static BOOST_CONSTEXPR one value() { return(one()); }
nameboost::units::scale61     static std::string name() { return(""); }
symbolboost::units::scale62     static std::string symbol() { return(""); }
63 };
64 
65 template<long Base>
66 BOOST_CONSTEXPR_OR_CONST long scale<Base, static_rational<0> >::base;
67 
68 template<long Base,class Exponent>
symbol_string(const scale<Base,Exponent> &)69 std::string symbol_string(const scale<Base,Exponent>&)
70 {
71     return scale<Base,Exponent>::symbol();
72 }
73 
74 template<long Base,class Exponent>
name_string(const scale<Base,Exponent> &)75 std::string name_string(const scale<Base,Exponent>&)
76 {
77     return scale<Base,Exponent>::name();
78 }
79 
80 #ifndef BOOST_UNITS_DOXYGEN
81 
82 #define BOOST_UNITS_SCALE_SPECIALIZATION(base_,exponent_,val_,name_,symbol_) \
83 template<>                                                                   \
84 struct scale<base_, exponent_ >                                              \
85 {                                                                            \
86     BOOST_STATIC_CONSTEXPR long base = base_;                                \
87     typedef exponent_ exponent;                                              \
88     typedef double value_type;                                               \
89     static BOOST_CONSTEXPR value_type value()   { return(val_); }            \
90     static std::string name()   { return(#name_); }                          \
91     static std::string symbol() { return(#symbol_); }                        \
92 }
93 
94 #define BOOST_UNITS_SCALE_DEF(exponent_,value_,name_,symbol_)                 \
95 BOOST_UNITS_SCALE_SPECIALIZATION(10,static_rational<exponent_>,value_, name_, symbol_)
96 
97 BOOST_UNITS_SCALE_DEF(-24, 1e-24, yocto, y);
98 BOOST_UNITS_SCALE_DEF(-21, 1e-21, zepto, z);
99 BOOST_UNITS_SCALE_DEF(-18, 1e-18, atto, a);
100 BOOST_UNITS_SCALE_DEF(-15, 1e-15, femto, f);
101 BOOST_UNITS_SCALE_DEF(-12, 1e-12, pico, p);
102 BOOST_UNITS_SCALE_DEF(-9, 1e-9, nano, n);
103 BOOST_UNITS_SCALE_DEF(-6, 1e-6, micro, u);
104 BOOST_UNITS_SCALE_DEF(-3, 1e-3, milli, m);
105 BOOST_UNITS_SCALE_DEF(-2, 1e-2, centi, c);
106 BOOST_UNITS_SCALE_DEF(-1, 1e-1, deci, d);
107 
108 BOOST_UNITS_SCALE_DEF(1, 1e1, deka, da);
109 BOOST_UNITS_SCALE_DEF(2, 1e2, hecto, h);
110 BOOST_UNITS_SCALE_DEF(3, 1e3, kilo, k);
111 BOOST_UNITS_SCALE_DEF(6, 1e6, mega, M);
112 BOOST_UNITS_SCALE_DEF(9, 1e9, giga, G);
113 BOOST_UNITS_SCALE_DEF(12, 1e12, tera, T);
114 BOOST_UNITS_SCALE_DEF(15, 1e15, peta, P);
115 BOOST_UNITS_SCALE_DEF(18, 1e18, exa, E);
116 BOOST_UNITS_SCALE_DEF(21, 1e21, zetta, Z);
117 BOOST_UNITS_SCALE_DEF(24, 1e24, yotta, Y);
118 
119 BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<10>, 1024.0, kibi, Ki);
120 BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<20>, 1048576.0, mebi, Mi);
121 BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<30>, 1073741824.0, gibi, Gi);
122 BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<40>, 1099511627776.0, tebi, Ti);
123 BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<50>, 1125899906842624.0, pebi, Pi);
124 BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<60>, 1152921504606846976.0, exbi, Ei);
125 BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<70>, 1180591620717411303424.0, zebi, Zi);
126 BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<80>, 1208925819614629174706176.0, yobi, Yi);
127 
128 #undef BOOST_UNITS_SCALE_DEF
129 #undef BOOST_UNITS_SCALE_SPECIALIZATION
130 
131 #endif
132 
133 } // namespace units
134 
135 } // namespace boost
136 
137 #if BOOST_UNITS_HAS_BOOST_TYPEOF
138 
139 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
140 
141 BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::scale, (long)(class))
142 
143 #endif
144 
145 #endif
146