]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/units/example/autoprefixes.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / units / example / autoprefixes.cpp
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 /**
12 \file
13
14 \brief Example of using autoprefixes.
15
16 \details
17 Example of using engineering (10^3) and binary (2^10) autoprefixes.
18
19 Output:
20 @verbatim
21 autoprefixes.cpp
22 using native typeof
23 Linking...
24 Embedding manifest...
25 Autorun "j:\Cpp\Misc\debug\autoprefixes.exe"
26 2.345 m
27 2.345 km
28 5.49902 MJ
29 5.49902 megajoule
30 2.048 kb
31 2 Kib
32 2345.6
33 23456
34 2345.6
35 23456
36 m
37 meter
38 0
39 1
40 @endverbatim
41 //[autoprefixes_output
42
43 //] [/autoprefixes_output
44
45 **/
46
47 #include <iostream>
48
49 #include <boost/units/io.hpp>
50 #include <boost/units/pow.hpp>
51 #include <boost/units/systems/si.hpp>
52 #include <boost/units/systems/si/io.hpp>
53 #include <boost/units/quantity.hpp>
54
55 struct byte_base_unit : boost::units::base_unit<byte_base_unit, boost::units::dimensionless_type, 3>
56 {
57 static const char* name() { return("byte"); }
58 static const char* symbol() { return("b"); }
59 };
60
61 struct thing_base_unit : boost::units::base_unit<thing_base_unit, boost::units::dimensionless_type, 4>
62 {
63 static const char* name() { return("thing"); }
64 static const char* symbol() { return(""); }
65 };
66
67 struct euro_base_unit : boost::units::base_unit<euro_base_unit, boost::units::dimensionless_type, 5>
68 {
69 static const char* name() { return("EUR"); }
70 static const char* symbol() { return("\80"); }
71 };
72
73 int main()
74 {
75 using std::cout;
76 using std::endl;
77
78 using namespace boost::units;
79 using namespace boost::units::si;
80
81 //[autoprefixes_snippet_1
82 using boost::units::binary_prefix;
83 using boost::units::engineering_prefix;
84 using boost::units::no_prefix;
85
86 quantity<length> l = 2.345 * meters; // A quantity of length, in units of meters.
87 cout << engineering_prefix << l << endl; // Outputs "2.345 m".
88 l = 1000.0 * l; // Increase it by 1000, so expect a k prefix.
89 // Note that a double 1000.0 is required - an integer will fail to compile.
90 cout << engineering_prefix << l << endl; // Output autoprefixed with k to "2.345 km".
91
92 quantity<energy> e = kilograms * pow<2>(l / seconds); // A quantity of energy.
93 cout << engineering_prefix << e << endl; // 5.49902 MJ
94 cout << name_format << engineering_prefix << e << endl; // 5.49902 megaJoule
95 //] [/autoprefixes_snippet_1]
96
97 //[autoprefixes_snippet_2
98 // Don't forget that the units name or symbol format specification is persistent.
99 cout << symbol_format << endl; // Resets the format to the default symbol format.
100
101 quantity<byte_base_unit::unit_type> b = 2048. * byte_base_unit::unit_type();
102 cout << engineering_prefix << b << endl; // 2.048 kb
103 cout << symbol_format << binary_prefix << b << endl; // "2 Kib"
104 //] [/autoprefixes_snippet_2]
105
106 // Note that scalar dimensionless values are *not* prefixed automatically by the engineering_prefix or binary_prefix iostream manipulators.
107 //[autoprefixes_snippet_3
108 const double s1 = 2345.6;
109 const long x1 = 23456;
110 cout << engineering_prefix << s1 << endl; // 2345.6
111 cout << engineering_prefix << x1 << endl; // 23456
112
113 cout << binary_prefix << s1 << endl; // 2345.6
114 cout << binary_prefix << x1 << endl; // 23456
115 //] [/autoprefixes_snippet_3]
116
117 //[autoprefixes_snippet_4
118 const length L; // A unit of length (but not a quantity of length).
119 cout << L << endl; // Default length unit is meter,
120 // but default is symbol format so output is just "m".
121 cout << name_format << L << endl; // default length name is "meter".
122 //] [/autoprefixes_snippet_4]
123
124 //[autoprefixes_snippet_5
125 no_prefix(cout); // Clear any prefix flag.
126 cout << no_prefix << endl; // Clear any prefix flag using `no_prefix` manipulator.
127 //] [/autoprefixes_snippet_5]
128
129 //[autoprefixes_snippet_6
130 cout << boost::units::get_autoprefix(cout) << endl; // 8 is `autoprefix_binary` from `enum autoprefix_mode`.
131 cout << boost::units::get_format(cout) << endl; // 1 is `name_fmt` from `enum format_mode`.
132 //] [/autoprefixes_snippet_6]
133
134
135 quantity<thing_base_unit::unit_type> t = 2048. * thing_base_unit::unit_type();
136 cout << name_format << engineering_prefix << t << endl; // 2.048 kilothing
137 cout << symbol_format << engineering_prefix << t << endl; // 2.048 k
138
139 cout << binary_prefix << t << endl; // "2 Ki"
140
141 quantity<euro_base_unit::unit_type> ce = 2048. * euro_base_unit::unit_type();
142 cout << name_format << engineering_prefix << ce << endl; // 2.048 kiloEUR
143 cout << symbol_format << engineering_prefix << ce << endl; // 2.048 k\80
144
145
146 return 0;
147 } // int main()
148