]>
git.proxmox.com Git - ceph.git/blob - 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
4 // Copyright (C) 2003-2008 Matthias Christian Schabel
5 // Copyright (C) 2008 Steven Watanabe
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)
14 \brief Example of using autoprefixes.
17 Example of using engineering (10^3) and binary (2^10) autoprefixes.
25 Autorun "j:\Cpp\Misc\debug\autoprefixes.exe"
41 //[autoprefixes_output
43 //] [/autoprefixes_output
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>
55 struct byte_base_unit
: boost::units::base_unit
<byte_base_unit
, boost::units::dimensionless_type
, 3>
57 static const char* name() { return("byte"); }
58 static const char* symbol() { return("b"); }
61 struct thing_base_unit
: boost::units::base_unit
<thing_base_unit
, boost::units::dimensionless_type
, 4>
63 static const char* name() { return("thing"); }
64 static const char* symbol() { return(""); }
67 struct euro_base_unit
: boost::units::base_unit
<euro_base_unit
, boost::units::dimensionless_type
, 5>
69 static const char* name() { return("EUR"); }
70 static const char* symbol() { return("\80"); }
78 using namespace boost::units
;
79 using namespace boost::units::si
;
81 //[autoprefixes_snippet_1
82 using boost::units::binary_prefix
;
83 using boost::units::engineering_prefix
;
84 using boost::units::no_prefix
;
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".
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]
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.
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]
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
113 cout
<< binary_prefix
<< s1
<< endl
; // 2345.6
114 cout
<< binary_prefix
<< x1
<< endl
; // 23456
115 //] [/autoprefixes_snippet_3]
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]
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]
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]
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
139 cout
<< binary_prefix
<< t
<< endl
; // "2 Ki"
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