• 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 // $Id: lambda.cpp 27 2008-06-16 14:50:58Z maehne $
12 
13 ////////////////////////////////////////////////////////////////////////
14 ///
15 /// \file lambda.cpp
16 ///
17 /// \brief Example demonstrating the usage of Boost.Units' quantity,
18 ///        unit, and absolute types in functors created with the
19 ///        Boost.Lambda library and stored in Boost.Function objects.
20 ///
21 /// \author Torsten Maehne
22 /// \date   2008-06-04
23 ///
24 /// A mechanical, electrical, geometrical, and thermal example
25 /// demonstrate how to use Boost.Units' quantity, unit, and absolute
26 /// types in lambda expressions. The resulting functors can be stored
27 /// in boost::function objects. It is also shown how to work around a
28 /// limitation of Boost.Lambda's bind() to help it to find the correct
29 /// overloaded function by specifying its signature with a
30 /// static_cast.
31 ///
32 ////////////////////////////////////////////////////////////////////////
33 
34 #include <iostream>
35 #include <boost/function.hpp>
36 #include <boost/units/io.hpp>
37 #include <boost/units/cmath.hpp>
38 #include <boost/units/pow.hpp>
39 #include <boost/units/systems/si.hpp>
40 #include <boost/units/absolute.hpp>
41 
42 // Include boost/units/lambda.hpp instead of boost/lambda/lambda.hpp
43 // for a convenient usage of Boost.Units' quantity, unit, and absolute
44 // types in lambda expressions. The header augments Boost.Lambda's
45 // return type detuction system to recognize the new types so that not
46 // for each arithmetic operation the return type needs to be
47 // explicitely specified.
48 #include <boost/units/lambda.hpp>
49 
50 #include <boost/lambda/bind.hpp>
51 
52 static const double pi = 3.14159265358979323846;
53 
54 //[lambda_snippet_1
55 
main(int argc,char ** argv)56 int main(int argc, char **argv) {
57 
58    using namespace std;
59    namespace bl = boost::lambda;
60    namespace bu = boost::units;
61    namespace si = boost::units::si;
62 
63 
64    ////////////////////////////////////////////////////////////////////////
65    // Mechanical example: linear accelerated movement
66    ////////////////////////////////////////////////////////////////////////
67 
68    // Initial condition variables for acceleration, speed, and displacement
69    bu::quantity<si::acceleration> a = 2.0 * si::meters_per_second_squared;
70    bu::quantity<si::velocity> v = 1.0 * si::meters_per_second;
71    bu::quantity<si::length> s0 = 0.5 * si::meter;
72 
73    // Displacement over time
74    boost::function<bu::quantity<si::length> (bu::quantity<si::time>) >
75        s = 0.5 * bl::var(a) * bl::_1 * bl::_1
76            + bl::var(v) * bl::_1
77            + bl::var(s0);
78 
79    cout << "Linear accelerated movement:" << endl
80         << "a = " << a << ", v = " << v << ", s0 = " << s0 << endl
81         << "s(1.0 * si::second) = " << s(1.0 * si::second) << endl
82         << endl;
83 
84    // Change initial conditions
85    a = 1.0 * si::meters_per_second_squared;
86    v = 2.0 * si::meters_per_second;
87    s0 = -1.5 * si::meter;
88 
89    cout << "a = " << a << ", v = " << v << ", s0 = " << s0 << endl
90         << "s(1.0 * si::second) = " << s(1.0 * si::second) << endl
91         << endl;
92 
93 
94    ////////////////////////////////////////////////////////////////////////
95    // Electrical example: oscillating current
96    ////////////////////////////////////////////////////////////////////////
97 
98    // Constants for the current amplitude, frequency, and offset current
99    const bu::quantity<si::current> iamp = 1.5 * si::ampere;
100    const bu::quantity<si::frequency> f = 1.0e3 * si::hertz;
101    const bu::quantity<si::current> i0 = 0.5 * si::ampere;
102 
103    // The invocation of the sin function needs to be postponed using
104    // bind to specify the oscillation function. A lengthy static_cast
105    // to the function pointer referencing boost::units::sin() is needed
106    // to avoid an "unresolved overloaded function type" error.
107    boost::function<bu::quantity<si::current> (bu::quantity<si::time>) >
108        i = iamp
109            * bl::bind(static_cast<bu::dimensionless_quantity<si::system, double>::type (*)(const bu::quantity<si::plane_angle>&)>(bu::sin),
110                       2.0 * pi * si::radian * f * bl::_1)
111            + i0;
112 
113    cout << "Oscillating current:" << endl
114         << "iamp = " << iamp << ", f = " << f << ", i0 = " << i0 << endl
115         << "i(1.25e-3 * si::second) = " << i(1.25e-3 * si::second) << endl
116         << endl;
117 
118 
119    ////////////////////////////////////////////////////////////////////////
120    // Geometric example: area calculation for a square
121    ////////////////////////////////////////////////////////////////////////
122 
123    // Length constant
124    const bu::quantity<si::length> l = 1.5 * si::meter;
125 
126    // Again an ugly static_cast is needed to bind pow<2> to the first
127    // function argument.
128    boost::function<bu::quantity<si::area> (bu::quantity<si::length>) >
129        A = bl::bind(static_cast<bu::quantity<si::area> (*)(const bu::quantity<si::length>&)>(bu::pow<2>),
130                     bl::_1);
131 
132    cout << "Area of a square:" << endl
133         << "A(" << l <<") = " << A(l) << endl << endl;
134 
135 
136    ////////////////////////////////////////////////////////////////////////
137    // Thermal example: temperature difference of two absolute temperatures
138    ////////////////////////////////////////////////////////////////////////
139 
140    // Absolute temperature constants
141    const bu::quantity<bu::absolute<si::temperature> >
142        Tref = 273.15 * bu::absolute<si::temperature>();
143    const bu::quantity<bu::absolute<si::temperature> >
144        Tamb = 300.00 * bu::absolute<si::temperature>();
145 
146    boost::function<bu::quantity<si::temperature> (bu::quantity<bu::absolute<si::temperature> >,
147                                                   bu::quantity<bu::absolute<si::temperature> >)>
148        dT = bl::_2 - bl::_1;
149 
150    cout << "Temperature difference of two absolute temperatures:" << endl
151         << "dT(" << Tref << ", " << Tamb << ") = " << dT(Tref, Tamb) << endl
152         << endl;
153 
154 
155    return 0;
156 }
157 //]
158