autoprefixes.cpp 4.7 KB

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