• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) 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 #include <iostream>
12 
13 #include <boost/units/units_fwd.hpp>
14 
15 #include <boost/units/base_dimension.hpp>
16 #include <boost/units/base_unit.hpp>
17 #include <boost/units/derived_dimension.hpp>
18 #include <boost/units/make_system.hpp>
19 #include <boost/units/io.hpp>
20 #include <boost/units/quantity.hpp>
21 #include <boost/units/static_constant.hpp>
22 #include <boost/units/unit.hpp>
23 
24 namespace boost {
25 
26 namespace units {
27 
28 struct length_base_dimension : boost::units::base_dimension<length_base_dimension,1> { };       ///> base dimension of length
29 struct time_base_dimension : boost::units::base_dimension<time_base_dimension,3> { };           ///> base dimension of time
30 
31 typedef length_base_dimension::dimension_type    length_dimension;
32 typedef time_base_dimension::dimension_type      time_dimension;
33 
34 struct length1_base_unit : base_unit<length1_base_unit,length_dimension,1>
35 {
nameboost::units::length1_base_unit36     static std::string name()               { return "length 1"; }
symbolboost::units::length1_base_unit37     static std::string symbol()             { return "l1"; }
38 };
39 
40 struct length2_base_unit : base_unit<length2_base_unit,length_dimension,2>
41 {
nameboost::units::length2_base_unit42     static std::string name()               { return "length2"; }
symbolboost::units::length2_base_unit43     static std::string symbol()             { return "l2"; }
44 };
45 
46 struct time1_base_unit : base_unit<time1_base_unit,time_dimension,3>
47 {
nameboost::units::time1_base_unit48     static std::string name()               { return "time1"; }
symbolboost::units::time1_base_unit49     static std::string symbol()             { return "t1"; }
50 };
51 
52 struct time2_base_unit : base_unit<time2_base_unit,time_dimension,4>
53 {
nameboost::units::time2_base_unit54     static std::string name()               { return "time2"; }
symbolboost::units::time2_base_unit55     static std::string symbol()             { return "t2"; }
56 };
57 
58 namespace s1 {
59 
60 typedef make_system<length1_base_unit,time1_base_unit>::type   system;
61 
62 /// unit typedefs
63 typedef unit<dimensionless_type,system>     dimensionless;
64 
65 typedef unit<length_dimension,system>       length;
66 typedef unit<time_dimension,system>         time;
67 
68 /// unit constants
69 BOOST_UNITS_STATIC_CONSTANT(length1,length);
70 BOOST_UNITS_STATIC_CONSTANT(time1,time);
71 
72 } // namespace s1
73 
74 namespace s2 {
75 
76 typedef make_system<length2_base_unit,time2_base_unit>::type   system;
77 
78 /// unit typedefs
79 typedef unit<dimensionless_type,system>     dimensionless;
80 
81 typedef unit<length_dimension,system>       length;
82 typedef unit<time_dimension,system>         time;
83 
84 /// unit constants
85 BOOST_UNITS_STATIC_CONSTANT(length2,length);
86 BOOST_UNITS_STATIC_CONSTANT(time2,time);
87 
88 } // namespace s2
89 
90 template<class X,class Y>
91 struct conversion_helper< quantity<s1::length,X>,quantity<s2::length,Y> >
92 {
convertboost::units::conversion_helper93     static quantity<s2::length,Y> convert(const quantity<s1::length,X>& source)
94     {
95         return quantity<s2::length,Y>::from_value(2.5*source.value());
96     }
97 };
98 
99 template<class X,class Y>
100 struct conversion_helper< quantity<s2::length,X>,quantity<s1::length,Y> >
101 {
convertboost::units::conversion_helper102     static quantity<s1::length,Y> convert(const quantity<s2::length,X>& source)
103     {
104         return quantity<s1::length,Y>::from_value((1.0/2.5)*source.value());
105     }
106 };
107 
108 template<class X,class Y>
109 struct conversion_helper< quantity<s1::time,X>,quantity<s2::time,Y> >
110 {
convertboost::units::conversion_helper111     static quantity<s2::time,Y> convert(const quantity<s1::time,X>& source)
112     {
113         return quantity<s2::time,Y>::from_value(0.5*source.value());
114     }
115 };
116 
117 } // namespace units
118 
119 } // namespace boost
120 
main(void)121 int main(void)
122 {
123     using namespace boost::units;
124 
125     quantity<s1::length,float>  l1(1.0*s1::length1);
126     quantity<s2::length,double> l2(1.5*l1);
127     quantity<s1::length,float>  l3(2.0*l2/3.0);
128 
129     quantity<s1::time,float>    t1(1.0*s1::time1);
130     quantity<s2::time,double>   t2(1.5*t1);
131 //    quantity<s1::time,float>    t3(2.0*t2/3.0);
132 
133     return 0;
134 }
135 
136 /*
137 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
138 // unit/quantity manipulation and conversion
139 //
140 // Copyright (C) 2003-2008 Matthias Christian Schabel
141 // Copyright (C) 2008 Steven Watanabe
142 //
143 // Distributed under the Boost Software License, Version 1.0. (See
144 // accompanying file LICENSE_1_0.txt or copy at
145 // http://www.boost.org/LICENSE_1_0.txt)
146 
147 #include <iostream>
148 
149 #include <boost/units/units_fwd.hpp>
150 
151 #include <boost/units/base_dimension.hpp>
152 #include <boost/units/base_unit.hpp>
153 #include <boost/units/derived_dimension.hpp>
154 #include <boost/units/make_system.hpp>
155 #include <boost/units/io.hpp>
156 #include <boost/units/quantity.hpp>
157 #include <boost/units/static_constant.hpp>
158 #include <boost/units/unit.hpp>
159 
160 namespace boost {
161 
162 namespace units {
163 
164 struct length_base_dimension : boost::units::base_dimension<length_base_dimension,1> { };       ///> base dimension of length
165 struct mass_base_dimension : boost::units::base_dimension<mass_base_dimension,2> { };           ///> base dimension of mass
166 struct time_base_dimension : boost::units::base_dimension<time_base_dimension,3> { };           ///> base dimension of time
167 
168 typedef length_base_dimension::dimension_type    length_dimension;
169 typedef mass_base_dimension::dimension_type      mass_dimension;
170 typedef time_base_dimension::dimension_type      time_dimension;
171 
172 struct centimeter_base_unit : base_unit<centimeter_base_unit,length_dimension,1>
173 {
174     static std::string name()               { return "centimeter"; }
175     static std::string symbol()             { return "cm"; }
176 };
177 
178 struct gram_base_unit : base_unit<gram_base_unit,mass_dimension,2>
179 {
180     static std::string name()               { return "gram"; }
181     static std::string symbol()             { return "g"; }
182 };
183 
184 struct second_base_unit : base_unit<second_base_unit,time_dimension,3>
185 {
186     static std::string name()               { return "second"; }
187     static std::string symbol()             { return "s"; }
188 };
189 
190 namespace CG {
191 
192 typedef make_system<centimeter_base_unit,gram_base_unit>::type   system;
193 
194 /// unit typedefs
195 typedef unit<dimensionless_type,system>     dimensionless;
196 
197 typedef unit<length_dimension,system>       length;
198 typedef unit<mass_dimension,system>         mass;
199 
200 /// unit constants
201 BOOST_UNITS_STATIC_CONSTANT(centimeter,length);
202 BOOST_UNITS_STATIC_CONSTANT(gram,mass);
203 
204 } // namespace CG
205 
206 namespace cgs {
207 
208 typedef make_system<centimeter_base_unit,gram_base_unit,second_base_unit>::type system;
209 
210 /// unit typedefs
211 typedef unit<dimensionless_type,system>     dimensionless;
212 
213 typedef unit<length_dimension,system>       length;
214 typedef unit<mass_dimension,system>         mass;
215 typedef unit<time_dimension,system>         time;
216 
217 /// unit constants
218 BOOST_UNITS_STATIC_CONSTANT(centimeter,length);
219 BOOST_UNITS_STATIC_CONSTANT(gram,mass);
220 BOOST_UNITS_STATIC_CONSTANT(second,time);
221 
222 } // namespace cgs
223 
224 namespace esu {
225 
226 typedef make_system<centimeter_base_unit,
227                     gram_base_unit,
228                     second_base_unit>::type system;
229 
230 /// derived dimension for force in electrostatic units : L M T^-2
231 typedef derived_dimension<length_base_dimension,1,
232                           mass_base_dimension,1,
233                           time_base_dimension,-2>::type                                             force_dimension;
234 
235 /// derived dimension for charge in electrostatic units : L^3/2 M^1/2 T^-1
236 typedef make_dimension_list< mpl::list< dim<length_base_dimension,static_rational<3,2> >,
237                                         dim<mass_base_dimension,static_rational<1,2> >,
238                                         dim<time_base_dimension,static_rational<-1> > > >::type     charge_dimension;
239 
240 /// derived dimension for current in electrostatic units : L^3/2 M^1/2 T^-2
241 typedef make_dimension_list< mpl::list< dim<length_base_dimension,static_rational<3,2> >,
242                                         dim<mass_base_dimension,static_rational<1,2> >,
243                                         dim<time_base_dimension,static_rational<-2> > > >::type     current_dimension;
244 
245 /// derived dimension for electric potential in electrostatic units : L^1/2 M^1/2 T^-1
246 typedef make_dimension_list< mpl::list< dim<length_base_dimension,static_rational<1,2> >,
247                                         dim<mass_base_dimension,static_rational<1,2> >,
248                                         dim<time_base_dimension,static_rational<-1> > > >::type     electric_potential_dimension;
249 
250 /// derived dimension for electric field in electrostatic units : L^-1/2 M^1/2 T^-1
251 typedef make_dimension_list< mpl::list< dim<length_base_dimension,static_rational<-1,2> >,
252                                         dim<mass_base_dimension,static_rational<1,2> >,
253                                         dim<time_base_dimension,static_rational<-1> > > >::type     electric_field_dimension;
254 
255 /// unit typedefs
256 typedef unit<dimensionless_type,system>     dimensionless;
257 
258 typedef unit<length_dimension,system>       length;
259 typedef unit<mass_dimension,system>         mass;
260 typedef unit<time_dimension,system>         time;
261 
262 typedef unit<force_dimension,system>        force;
263 
264 typedef unit<charge_dimension,system>               charge;
265 typedef unit<current_dimension,system>              current;
266 typedef unit<electric_potential_dimension,system>   electric_potential;
267 typedef unit<electric_field_dimension,system>       electric_field;
268 
269 /// unit constants
270 BOOST_UNITS_STATIC_CONSTANT(centimeter,length);
271 BOOST_UNITS_STATIC_CONSTANT(gram,mass);
272 BOOST_UNITS_STATIC_CONSTANT(second,time);
273 
274 BOOST_UNITS_STATIC_CONSTANT(dyne,force);
275 
276 BOOST_UNITS_STATIC_CONSTANT(esu,charge);
277 BOOST_UNITS_STATIC_CONSTANT(statvolt,electric_potential);
278 
279 } // namespace esu
280 
281 template<class Y>
282 quantity<esu::force,Y> coulombLaw(const quantity<esu::charge,Y>& q1,
283                                   const quantity<esu::charge,Y>& q2,
284                                   const quantity<esu::length,Y>& r)
285 {
286     return q1*q2/(r*r);
287 }
288 
289 } // namespace units
290 
291 } // namespace boost
292 
293 int main(void)
294 {
295     using namespace boost::units;
296 
297     quantity<CG::length>    cg_length(1.0*CG::centimeter);
298     quantity<cgs::length>   cgs_length(1.0*cgs::centimeter);
299 
300     std::cout << cg_length/cgs_length << std::endl;
301 
302     std::cout << esu::gram*pow<2>(esu::centimeter/esu::second)/esu::esu << std::endl;
303     std::cout << esu::statvolt/esu::centimeter << std::endl;
304 
305     quantity<esu::charge>   q1 = 1.0*esu::esu,
306                             q2 = 2.0*esu::esu;
307     quantity<esu::length>   r = 1.0*esu::centimeter;
308 
309     std::cout << coulombLaw(q1,q2,r) << std::endl;
310     std::cout << coulombLaw(q1,q2,cgs_length) << std::endl;
311 
312     return 0;
313 }
314 */
315