diff --git a/docs/examples/conversion_factor.rst b/docs/examples/conversion_factor.rst index e2709215..21bbefc5 100644 --- a/docs/examples/conversion_factor.rst +++ b/docs/examples/conversion_factor.rst @@ -1,7 +1,7 @@ conversion_factor ================= -.. literalinclude:: ../../example/references/conversion_factor.cpp +.. literalinclude:: ../../example/conversion_factor.cpp :caption: conversion_factor.cpp :start-at: #include :linenos: diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 2a789851..17fb0211 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -22,11 +22,17 @@ cmake_minimum_required(VERSION 3.2) -add_executable(hello_units hello_units.cpp) -target_link_libraries(hello_units PRIVATE mp-units::core-fmt mp-units::core-io mp-units::si mp-units::si-international) +# +# add_example(target ...) +# +function(add_example target) + add_executable(${target} ${target}.cpp) + target_link_libraries(${target} PRIVATE ${ARGN}) +endfunction() -add_executable(custom_systems custom_systems.cpp) -target_link_libraries(custom_systems PRIVATE mp-units::core-io mp-units::si) +add_example(conversion_factor mp-units::core-fmt mp-units::core-io mp-units::si) +add_example(custom_systems mp-units::core-io mp-units::si) +add_example(hello_units mp-units::core-fmt mp-units::core-io mp-units::si mp-units::si-international) if(NOT UNITS_LIBCXX) add_subdirectory(glide_computer) diff --git a/example/literals/conversion_factor.cpp b/example/conversion_factor.cpp similarity index 98% rename from example/literals/conversion_factor.cpp rename to example/conversion_factor.cpp index 7e370522..fa3b66f3 100644 --- a/example/literals/conversion_factor.cpp +++ b/example/conversion_factor.cpp @@ -46,7 +46,7 @@ int main() std::cout << "conversion factor in mp-units...\n\n"; - constexpr length lengthA = 2.0_q_m; + constexpr length lengthA(2.0); constexpr length lengthB = lengthA; std::cout << fmt::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB) diff --git a/example/literals/CMakeLists.txt b/example/literals/CMakeLists.txt index 765ec342..26d32f77 100644 --- a/example/literals/CMakeLists.txt +++ b/example/literals/CMakeLists.txt @@ -35,7 +35,6 @@ add_example(avg_speed mp-units::core-io mp-units::si mp-units::si-cgs mp-units:: add_example(box_example mp-units::core-fmt mp-units::si) add_example(capacitor_time_curve mp-units::core-io mp-units::si) add_example(clcpp_response mp-units::core-fmt mp-units::core-io mp-units::si mp-units::si-iau mp-units::si-imperial mp-units::si-international mp-units::si-typographic mp-units::si-us) -add_example(conversion_factor mp-units::core-fmt mp-units::core-io mp-units::si) add_example(experimental_angle mp-units::core-fmt mp-units::core-io mp-units::si) add_example(foot_pound_second mp-units::core-fmt mp-units::si-fps) add_example(kalman_filter-alpha_beta_filter_example2 mp-units::core-fmt mp-units::si) diff --git a/example/references/CMakeLists.txt b/example/references/CMakeLists.txt index 47b583fe..6e75a0e8 100644 --- a/example/references/CMakeLists.txt +++ b/example/references/CMakeLists.txt @@ -35,7 +35,6 @@ add_example(avg_speed mp-units::core-io mp-units::si mp-units::si-cgs mp-units:: add_example(box_example mp-units::core-fmt mp-units::si) add_example(capacitor_time_curve mp-units::core-io mp-units::si) add_example(clcpp_response mp-units::core-fmt mp-units::core-io mp-units::si mp-units::si-iau mp-units::si-imperial mp-units::si-international mp-units::si-typographic mp-units::si-us) -add_example(conversion_factor mp-units::core-fmt mp-units::core-io mp-units::si) add_example(experimental_angle mp-units::core-fmt mp-units::core-io mp-units::si) add_example(foot_pound_second mp-units::core-fmt mp-units::si-fps) add_example(kalman_filter-alpha_beta_filter_example2 mp-units::core-fmt mp-units::si) diff --git a/example/references/conversion_factor.cpp b/example/references/conversion_factor.cpp deleted file mode 100644 index ef6eb7fc..00000000 --- a/example/references/conversion_factor.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* - 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 -#include -#include -#include - -/* - get conversion factor from one dimensionally equivalent - quantity type to another -*/ - -namespace { - -template - requires units::equivalent -inline constexpr std::common_type_t conversion_factor(Target, Source) -{ - // get quantities looking like inputs but with Q::rep that doesn't have narrowing conversion - typedef std::common_type_t rep; - typedef units::quantity source; - typedef units::quantity target; - return target{source{1}}.number(); -} - -} // namespace - -int main() -{ - using namespace units::isq::si; - using namespace units::isq::si::references; - - std::cout << "conversion factor in mp-units...\n\n"; - - constexpr auto lengthA = 2.0 * m; - constexpr auto lengthB = quantity_cast(lengthA); - - std::cout << fmt::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB) - << "represent the same length in different units.\n\n"; - - std::cout << fmt::format("therefore ratio lengthA / lengthB == {}\n\n", lengthA / lengthB); - - std::cout << fmt::format("conversion factor from lengthA::unit of {:%q} to lengthB::unit of {:%q}:\n\n", lengthA, lengthB) - << fmt::format("lengthB.number( {} ) == lengthA.number( {} ) * conversion_factor( {} )\n", - lengthB.number(), lengthA.number(), conversion_factor(lengthB, lengthA)); -}