diff --git a/example/alternative_namespaces/CMakeLists.txt b/example/alternative_namespaces/CMakeLists.txt index 9d5d3286..6f3bc3d7 100644 --- a/example/alternative_namespaces/CMakeLists.txt +++ b/example/alternative_namespaces/CMakeLists.txt @@ -29,3 +29,4 @@ add_example(box_example) add_example(capacitor_time_curve) add_example(clcpp_response) add_example(conversion_factor) +add_example(timer) diff --git a/example/alternative_namespaces/area.h b/example/alternative_namespaces/area.h new file mode 100644 index 00000000..610542e7 --- /dev/null +++ b/example/alternative_namespaces/area.h @@ -0,0 +1,14 @@ + +#pragma once + +#include + +namespace area { + +template +using m2 = units::si::area; + +template +using fm2 = units::si::area; + +} // namespace area diff --git a/example/alternative_namespaces/box_example.cpp b/example/alternative_namespaces/box_example.cpp index 55836e8e..d18b25bd 100644 --- a/example/alternative_namespaces/box_example.cpp +++ b/example/alternative_namespaces/box_example.cpp @@ -11,7 +11,6 @@ using namespace units::experimental; - struct Box { static constexpr auto air_density = density::kg_per_m3<>{1.225}; diff --git a/example/alternative_namespaces/capacitor_time_curve.cpp b/example/alternative_namespaces/capacitor_time_curve.cpp index 9ef501d7..9577a98f 100644 --- a/example/alternative_namespaces/capacitor_time_curve.cpp +++ b/example/alternative_namespaces/capacitor_time_curve.cpp @@ -23,31 +23,11 @@ #include #include #include -#include +#include "./voltage.h" #include #include -namespace { -namespace voltage { - -template -using V = units::si::voltage; - -template -using mV = units::si::voltage; - -template -using uV = units::si::voltage; - -template -using nV = units::si::voltage; - -template -using pV = units::si::voltage; - -} // namespace voltage -} // namespace - +using namespace units::experimental; using namespace units::si::literals; int main() diff --git a/example/alternative_namespaces/clcpp_response.cpp b/example/alternative_namespaces/clcpp_response.cpp index 94a26faa..567b3100 100644 --- a/example/alternative_namespaces/clcpp_response.cpp +++ b/example/alternative_namespaces/clcpp_response.cpp @@ -15,127 +15,30 @@ along with this program. If not, see http://www.gnu.org/licenses./ */ -#include -#include -#include -#include -#include -#include -#include -#include -#include #include +#include -namespace { -namespace length { - -template -using m = units::si::length; - -template -using mm = units::si::length; - -template -using fm = units::si::length; - -template -using km = units::si::length; - -template -using AU = units::si::length; - -template -using in = units::si::length; - -template -using angstrom = units::si::length; - -template -using ch = units::si::length; - -template -using fathom = units::si::length; - -template -using fathom_us = units::si::length; - -template -using ft = units::si::length; - -template -using ft_us = units::si::length; - -template -using ly = units::si::length; - -template -using mi = units::si::length; - -template -using mi_naut = units::si::length; - -template -using pc = units::si::length; - -template -using pica_comp = units::si::length; - -template -using pica_prn = units::si::length; - -template -using point_comp = units::si::length; - -template -using point_prn = units::si::length; - -template -using rd = units::si::length; - -template -using yd = units::si::length; - -} // namespace length - -namespace time { - -template -using s = units::si::time; - -template -using min = units::si::time; - -template -using h = units::si::time; - -} // namespace time - -namespace area { - -template -using m2 = units::si::area; - -template -using fm2 = units::si::area; - -} // namespace area -} // namespace - +#include "./length.h" +#include "./volume.h" +#include "./time.h" +#include "./area.h" +#include "./units_str.h" using namespace units::si::literals; using namespace units::international; +using namespace units::experimental; void simple_quantities() { using distance = length::m<>; - using time = time::s<>; + using q_time = q_time::s<>; constexpr distance km = 1.0q_km; constexpr distance miles = 1.0q_mi; - constexpr time sec = 1q_s; - constexpr time min = 1q_min; - constexpr time hr = 1q_h; + constexpr q_time sec = 1q_s; + constexpr q_time min = 1q_min; + constexpr q_time hr = 1q_h; std::cout << "A physical quantities library can choose the simple\n"; std::cout << "option to provide output using a single type for each base unit:\n\n"; @@ -151,11 +54,9 @@ void quantities_with_typed_units() constexpr length::km<> km = 1.0q_km; constexpr length::mi<> miles = 1.0q_mi; - std::cout.precision(6); - - constexpr time::s<> sec = 1q_s; - constexpr time::min<> min = 1q_min; - constexpr time::h<> hr = 1q_h; + constexpr q_time::s<> sec = 1q_s; + constexpr q_time::min<> min = 1q_min; + constexpr q_time::h<> hr = 1q_h; std::cout << "A more flexible option is to provide separate types for each unit,\n\n"; std::cout << km << '\n'; @@ -192,16 +93,14 @@ void quantities_with_typed_units() void calcs_comparison() { - std::cout.precision(20); std::cout << "\nA distinct unit for each type is efficient and accurate\n" "when adding two values of the same very big\n" "or very small type:\n\n"; - length::fm L1A = 2q_fm; - length::fm L2A = 3q_fm; + length::fm L1A = 2.q_fm; + length::fm L2A = 3.q_fm; length::fm LrA = L1A + L2A; - - std::cout << L1A << " + " << L2A << " = " << LrA << "\n\n"; + fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n",L1A,L2A,LrA); std::cout << "The single unit method must convert large\n" "or small values in other units to the base unit.\n" @@ -210,23 +109,22 @@ void calcs_comparison() length::m L1B = L1A; length::m L2B = L2A; length::m LrB = L1B + L2B; - - std::cout << L1B << " + " << L2B << " = " << LrB << "\n\n"; + fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n",L1B,L2B,LrB); std::cout << "In multiplication and division:\n\n"; area::fm2 ArA = L1A * L2A; - std::cout << L1A << " * " << L2A << " = " << ArA << "\n\n"; + fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n",L1A,L2A,ArA); std::cout << "similar problems arise\n\n"; area::m2 ArB = L1B * L2B; - std::cout << L1B << " * " << L2B << "\n = " << ArB << '\n'; + fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n",L1B,L2B,ArB); } int main() { - std::cout << "This demo was originally posted on com.lang.c++.moderated in 2006\n"; + std::cout << "This demo was originally posted on comp.lang.c++.moderated in 2006\n"; std::cout << "http://compgroups.net/comp.lang.c++.moderated/dimensional-analysis-units/51712\n"; std::cout << "Here converted to use mpusz/units library.\n\n"; diff --git a/example/alternative_namespaces/conversion_factor.cpp b/example/alternative_namespaces/conversion_factor.cpp index 259dfa38..28812a6d 100644 --- a/example/alternative_namespaces/conversion_factor.cpp +++ b/example/alternative_namespaces/conversion_factor.cpp @@ -15,7 +15,9 @@ along with this program. If not, see http://www.gnu.org/licenses./ */ -#include + +#include "./units_str.h" +#include "./length.h" #include /* @@ -36,29 +38,10 @@ inline constexpr std::common_type_t return target{source{1}}.count(); } -// get at the units text of the quantity, without its numeric value -inline auto constexpr units_str(const units::Quantity AUTO& q) -{ - typedef std::remove_cvref_t qtype; - return units::detail::unit_text(); -} - -} // namespace - -namespace { - -namespace length { - -template -using m = units::si::length; - -template -using mm = units::si::length; - -} // namespace length } // namespace using namespace units::si::literals; +using namespace units::experimental; int main() { diff --git a/example/alternative_namespaces/length.h b/example/alternative_namespaces/length.h index c3fb3a87..7410a0be 100644 --- a/example/alternative_namespaces/length.h +++ b/example/alternative_namespaces/length.h @@ -3,6 +3,11 @@ #pragma once #include +#include +#include +#include +#include +#include namespace units{ namespace experimental{ @@ -15,6 +20,67 @@ using m = units::si::length; template using mm = units::si::length; +template +using fm = units::si::length; + +template +using km = units::si::length; + +template +using AU = units::si::length; + +template +using in = units::si::length; + +template +using angstrom = units::si::length; + +template +using ch = units::si::length; + +template +using fathom = units::si::length; + +template +using fathom_us = units::si::length; + +template +using ft = units::si::length; + +template +using ft_us = units::si::length; + +template +using ly = units::si::length; + +template +using mi = units::si::length; + +template +using mi_naut = units::si::length; + +template +using pc = units::si::length; + +template +using pica_comp = units::si::length; + +template +using pica_prn = units::si::length; + +template +using point_comp = units::si::length; + +template +using point_prn = units::si::length; + +template +using rd = units::si::length; + +template +using yd = units::si::length; + + } // namespace length }} // units::experimental diff --git a/example/alternative_namespaces/time.h b/example/alternative_namespaces/time.h new file mode 100644 index 00000000..a72418aa --- /dev/null +++ b/example/alternative_namespaces/time.h @@ -0,0 +1,21 @@ + +#pragma once + +#include + +// named qtime due to conflict with time_t time(time_t*) +namespace q_time { + +template +using s = units::si::time; + +template +using ms = units::si::time; + +template +using min = units::si::time; + +template +using h = units::si::time; + +} // namespace time diff --git a/example/alternative_namespaces/timer.cpp b/example/alternative_namespaces/timer.cpp new file mode 100644 index 00000000..c2a90c1f --- /dev/null +++ b/example/alternative_namespaces/timer.cpp @@ -0,0 +1,35 @@ + +#include +#include "./timer.h" + +/* + simple timer, useful for perf timing etc +*/ + +using namespace units::experimental; +using namespace units::si::literals; + +int main() +{ + std::cout << "Simple timer using mpusz/units ...\n"; + + auto const period = 0.5q_s; + auto const duration = 10 * period; + + timer t; + + auto const start_time = t(); + + std::cout << "Started at " << start_time <<'\n'; + + auto prev = start_time; + for (auto now = t(); (now - start_time) < duration; now = t() ) { + if ( (now - prev ) >= period ){ + prev = now; + std::cout << "tick (" << now << ")\n";; + } + } + t.stop(); + + std::cout << "finished at " << t() << '\n'; +} diff --git a/example/alternative_namespaces/timer.h b/example/alternative_namespaces/timer.h new file mode 100644 index 00000000..73cb1799 --- /dev/null +++ b/example/alternative_namespaces/timer.h @@ -0,0 +1,64 @@ +#pragma once + +/* + Copyright (c) 2003-2020 Andy Little. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see http://www.gnu.org/licenses./ + */ + +#include "./time.h" + +#include + +namespace units{ namespace experimental{ + + class timer{ + + public: + timer(): running(true) + { + start_time = std::clock(); + } + + void restart() + { + running = true; + start_time = std::clock(); + } + + void stop() + { + if (running){ + stop_time = std::clock(); + running = false; + } + } + + q_time::ms<> operator ()()const + { + std::clock_t const wanted = running ? std::clock() : stop_time; + static constexpr auto divider = 1000.0 / CLOCKS_PER_SEC; + + return q_time::ms<>( + difftime(wanted , start_time) + ) * divider; + } + bool is_running() const {return running;} + bool is_stopped() const {return !running;} + private: + bool running; + std::clock_t start_time,stop_time; + }; + +} }//units::experimental diff --git a/example/alternative_namespaces/units_str.h b/example/alternative_namespaces/units_str.h new file mode 100644 index 00000000..3b0bfacd --- /dev/null +++ b/example/alternative_namespaces/units_str.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include +#include +// get at the units text of the quantity, without its numeric value +inline auto constexpr units_str(const units::Quantity AUTO& q) +{ + typedef std::remove_cvref_t qtype; + return units::detail::unit_text(); +} diff --git a/example/alternative_namespaces/voltage.h b/example/alternative_namespaces/voltage.h new file mode 100644 index 00000000..a0807e50 --- /dev/null +++ b/example/alternative_namespaces/voltage.h @@ -0,0 +1,31 @@ + + +#pragma once + +#include + +namespace units{ +namespace experimental{ + +namespace voltage { + +template +using V = units::si::voltage; + +template +using mV = units::si::voltage; + +template +using uV = units::si::voltage; + +template +using nV = units::si::voltage; + +template +using pV = units::si::voltage; + +} // namespace voltage + +}} // units::experimental + + diff --git a/example/clcpp_response.cpp b/example/clcpp_response.cpp index 6e5e86c4..d5951497 100644 --- a/example/clcpp_response.cpp +++ b/example/clcpp_response.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include namespace { @@ -105,36 +106,33 @@ void calcs_comparison() { using namespace units::si; - std::cout.precision(20); std::cout << "\nA distinct unit for each type is efficient and accurate\n" "when adding two values of the same very big\n" "or very small type:\n\n"; - Length AUTO L1A = 2.q_fm; - Length AUTO L2A = 3.q_fm; - Length AUTO LrA = L1A + L2A; - - std::cout << L1A << " + " << L2A << " = " << LrA << "\n\n"; + length L1A = 2.q_fm; + length L2A = 3.q_fm; + length LrA = L1A + L2A; + fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n",L1A,L2A,LrA); std::cout << "The single unit method must convert large\n" "or small values in other units to the base unit.\n" "This is both inefficient and inaccurate\n\n"; - length L1B = L1A; - length L2B = L2A; - length LrB = L1B + L2B; - - std::cout << L1B << " + " << L2B << " = " << LrB << "\n\n"; + length L1B = L1A; + length L2B = L2A; + length LrB = L1B + L2B; + fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n",L1B,L2B,LrB); std::cout << "In multiplication and division:\n\n"; - Area AUTO ArA = L1A * L2A; - std::cout << L1A << " * " << L2A << " = " << ArA << "\n\n"; + area ArA = L1A * L2A; + fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n",L1A,L2A,ArA); std::cout << "similar problems arise\n\n"; - Area AUTO ArB = L1B * L2B; - std::cout << L1B << " * " << L2B << "\n = " << ArB << '\n'; + area ArB = L1B * L2B; + fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n",L1B,L2B,ArB); } } // namespace