From a0beeb48b104542de1fd4700d31f0cbf6441596b Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Thu, 10 Sep 2020 21:17:43 +0200 Subject: [PATCH] feat: custom systems examples added --- docs/CMakeLists.txt | 1 + docs/examples.rst | 1 + docs/examples/custom_systems.rst | 7 ++ example/CMakeLists.txt | 1 + example/custom_systems.cpp | 108 +++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 docs/examples/custom_systems.rst create mode 100644 example/custom_systems.cpp diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt index 57134341..045667e9 100644 --- a/docs/CMakeLists.txt +++ b/docs/CMakeLists.txt @@ -88,6 +88,7 @@ add_custom_command(OUTPUT "${SPHINX_INDEX_FILE}" "${CMAKE_CURRENT_SOURCE_DIR}/examples/capacitor_time_curve.rst" "${CMAKE_CURRENT_SOURCE_DIR}/examples/clcpp_response.rst" "${CMAKE_CURRENT_SOURCE_DIR}/examples/conversion_factor.rst" + "${CMAKE_CURRENT_SOURCE_DIR}/examples/custom_systems.rst" "${CMAKE_CURRENT_SOURCE_DIR}/examples/foot_pound_second.rst" "${CMAKE_CURRENT_SOURCE_DIR}/examples/glide_computer.rst" "${CMAKE_CURRENT_SOURCE_DIR}/examples/hello_units.rst" diff --git a/docs/examples.rst b/docs/examples.rst index c1774d85..ee75bc5c 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -11,6 +11,7 @@ Examples examples/measurement examples/linear_algebra examples/foot_pound_second + examples/custom_systems examples/glide_computer examples/capacitor_time_curve diff --git a/docs/examples/custom_systems.rst b/docs/examples/custom_systems.rst new file mode 100644 index 00000000..a4e93583 --- /dev/null +++ b/docs/examples/custom_systems.rst @@ -0,0 +1,7 @@ +custom_systems +============== + +.. literalinclude:: ../../example/custom_systems.cpp + :caption: custom_systems.cpp + :start-at: #include + :linenos: diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index afe0cd50..76a99d84 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -31,6 +31,7 @@ add_example(box_example) add_example(capacitor_time_curve) add_example(clcpp_response) add_example(conversion_factor) +add_example(custom_systems) add_example(experimental_angle) add_example(foot_pound_second) add_example(kalman_filter-alpha_beta_filter_example2) diff --git a/example/custom_systems.cpp b/example/custom_systems.cpp new file mode 100644 index 00000000..f6d4337d --- /dev/null +++ b/example/custom_systems.cpp @@ -0,0 +1,108 @@ +// The MIT License (MIT) +// +// Copyright (c) 2018 Mateusz Pusz +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include +#include +#include + +using namespace units; + +namespace fps { + +struct foot : named_unit {}; +struct yard : named_scaled_unit {}; + +struct dim_length : base_dimension<"L", foot> {}; + +template +using length = quantity; + +} // namespace fps + +namespace si { + +struct metre : named_unit {}; +struct kilometre : prefixed_unit {}; + +struct dim_length : base_dimension<"L", metre> {}; + +template +using length = quantity; + +namespace fps { + +struct foot : named_scaled_unit {}; +struct yard : named_scaled_unit {}; + +struct dim_length : base_dimension<"L", foot> {}; + +template +using length = quantity; + +} // namespace fps +} // namespace si + +void conversions() +{ + // constexpr auto fps_yard = fps::length(1.); + // std::cout << quantity_cast(fps_yard) << "\n"; + + constexpr auto si_fps_yard = si::fps::length(1.); + std::cout << quantity_cast(si_fps_yard) << "\n"; +} + +void unknown_dimensions() +{ + constexpr auto fps_yard = fps::length(1.); + constexpr auto fps_area = quantity_cast(fps_yard * fps_yard); + std::cout << fps_yard << "\n"; + std::cout << fps_area << "\n"; + + constexpr auto si_fps_yard = si::fps::length(1.); + constexpr auto si_fps_area = quantity_cast(si_fps_yard * si_fps_yard); + std::cout << si_fps_yard << "\n"; + std::cout << si_fps_area << "\n"; +} + +std::ostream& operator<<(std::ostream& os, const ratio& r) +{ + return os << "ratio{" << r.num << ", " << r.den << ", " << r.exp << "}"; +} + +std::ostream& operator<<(std::ostream& os, const Unit auto& u) +{ + using unit_type = std::remove_cvref_t; + return os << unit_type::ratio << " x " << unit_type::reference::symbol.standard(); +} + +void what_is_your_ratio() +{ + std::cout << "fps: " << fps::yard() << "\n"; + std::cout << "si::fps: " << si::fps::yard() << "\n"; +} + +int main() +{ + conversions(); + unknown_dimensions(); + what_is_your_ratio(); +}