diff --git a/README.md b/README.md index 6b7c0bb9..65240c62 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ ```cpp #include +#include #include #include @@ -23,7 +24,7 @@ int main() { using namespace si::literals; Velocity auto v1 = avg_speed(220km, 2h); - Velocity auto v2 = avg_speed(si::length(140), si::time(2)); + Velocity auto v2 = avg_speed(si::length(140), si::time(2)); Velocity auto v3 = quantity_cast(v2); Velocity auto v4 = quantity_cast(v3); @@ -152,6 +153,7 @@ fashion: int main() { using namespace si::literals; + using namespace us::literals; Velocity auto v1 = avg_speed(220km, 2h); Velocity auto v2 = avg_speed(140mi, 2h); diff --git a/doc/DESIGN.md b/doc/DESIGN.md index 2d543005..9dd42c69 100644 --- a/doc/DESIGN.md +++ b/doc/DESIGN.md @@ -150,8 +150,6 @@ struct kilo : units::prefix> {}; struct metre : named_unit {}; struct centimetre : prefixed_unit {}; struct kilometre : prefixed_unit {}; -struct yard : named_scaled_unit, metre> {}; -struct mile : named_scaled_unit, yard> {}; // time struct second : named_unit {}; @@ -160,7 +158,17 @@ struct hour : named_scaled_unit, second> {}; // velocity struct metre_per_second : unit {}; struct kilometre_per_hour : deduced_unit {}; -struct mile_per_hour : deduced_unit {}; + +} + +namespace units::us { + +// length +struct yard : named_scaled_unit, si::metre> {}; +struct mile : named_scaled_unit, yard> {}; + +// velocity +struct mile_per_hour : deduced_unit {}; } ``` diff --git a/example/avg_velocity.cpp b/example/avg_velocity.cpp index 49d65993..5c1bfae5 100644 --- a/example/avg_velocity.cpp +++ b/example/avg_velocity.cpp @@ -20,8 +20,9 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#include #include +#include +#include #include namespace { @@ -96,7 +97,7 @@ void example() // Customary Units (int) { - using namespace units::si::literals; + using namespace units::us::literals; constexpr Length AUTO distance = 140mi; // constructed from a UDL constexpr si::time duration(2); // constructed from a value @@ -112,7 +113,7 @@ void example() // Customary Units (double) { - using namespace units::si::literals; + using namespace units::us::literals; constexpr Length AUTO distance = 140.mi; // constructed from a UDL constexpr si::time duration(2); // constructed from a value diff --git a/example/hello_units.cpp b/example/hello_units.cpp index 2a933e59..3ca1dde8 100644 --- a/example/hello_units.cpp +++ b/example/hello_units.cpp @@ -21,6 +21,7 @@ // SOFTWARE. #include +#include #include #include @@ -35,7 +36,7 @@ int main() { using namespace si::literals; Velocity AUTO v1 = avg_speed(220km, 2h); - Velocity AUTO v2 = avg_speed(si::length(140), si::time(2)); + Velocity AUTO v2 = avg_speed(si::length(140), si::time(2)); Velocity AUTO v3 = quantity_cast(v2); Velocity AUTO v4 = quantity_cast(v3); diff --git a/src/include/units/physical/si/area.h b/src/include/units/physical/si/area.h index 4a53594d..fcfd83a8 100644 --- a/src/include/units/physical/si/area.h +++ b/src/include/units/physical/si/area.h @@ -34,7 +34,6 @@ struct dim_area : physical::dim_area {}; struct square_millimetre : deduced_unit {}; struct square_centimetre : deduced_unit {}; struct square_kilometre : deduced_unit {}; -struct square_foot : deduced_unit {}; template using area = quantity; @@ -57,10 +56,6 @@ constexpr auto operator"" sq_cm(long double l) { return area(l); } constexpr auto operator"" sq_km(long double l) { return area(l); } -// sq_ft -constexpr auto operator"" sq_ft(unsigned long long l) { return area(l); } -constexpr auto operator"" sq_ft(long double l) { return area(l); } - } // namespace literals } // namespace units::si diff --git a/src/include/units/physical/si/length.h b/src/include/units/physical/si/length.h index 6bf17294..92ecb960 100644 --- a/src/include/units/physical/si/length.h +++ b/src/include/units/physical/si/length.h @@ -58,30 +58,4 @@ constexpr auto operator"" km(long double l) { return length, metre> {}; -struct foot : named_scaled_unit, yard> {}; -struct inch : named_scaled_unit, foot> {}; -struct mile : named_scaled_unit, yard> {}; - -inline namespace literals { - -// yd -constexpr auto operator"" yd(unsigned long long l) { return length(l); } -constexpr auto operator"" yd(long double l) { return length(l); } - -// ft -constexpr auto operator"" ft(unsigned long long l) { return length(l); } -constexpr auto operator"" ft(long double l) { return length(l); } - -// in -constexpr auto operator"" in(unsigned long long l) { return length(l); } -constexpr auto operator"" in(long double l) { return length(l); } - -// mi -constexpr auto operator"" mi(unsigned long long l) { return length(l); } -constexpr auto operator"" mi(long double l) { return length(l); } - -} // namespace literals - } // namespace units::si diff --git a/src/include/units/physical/si/velocity.h b/src/include/units/physical/si/velocity.h index bcd168bd..f064d61c 100644 --- a/src/include/units/physical/si/velocity.h +++ b/src/include/units/physical/si/velocity.h @@ -33,7 +33,6 @@ struct metre_per_second : unit {}; struct dim_velocity : physical::dim_velocity {}; struct kilometre_per_hour : deduced_unit {}; -struct mile_per_hour : deduced_unit {}; template using velocity = quantity; @@ -48,10 +47,6 @@ constexpr auto operator"" mps(long double l) { return velocity(l); } constexpr auto operator"" kmph(long double l) { return velocity(l); } -// mph -constexpr auto operator"" mph(unsigned long long l) { return velocity(l); } -constexpr auto operator"" mph(long double l) { return velocity(l); } - } // namespace literals } // namespace units::si diff --git a/src/include/units/physical/si/volume.h b/src/include/units/physical/si/volume.h index be80331a..adef97bd 100644 --- a/src/include/units/physical/si/volume.h +++ b/src/include/units/physical/si/volume.h @@ -34,7 +34,6 @@ struct dim_volume : physical::dim_volume {} struct cubic_millimetre : deduced_unit {}; struct cubic_centimetre : deduced_unit {}; struct cubic_kilometre : deduced_unit {}; -struct cubic_foot : deduced_unit {}; template using volume = quantity; @@ -57,10 +56,6 @@ constexpr auto operator""cub_m(long double l) { return volume(l); } constexpr auto operator""cub_km(long double l) { return volume(l); } -// cub_ft -constexpr auto operator""cub_ft(unsigned long long l) { return volume(l); } -constexpr auto operator""cub_ft(long double l) { return volume(l); } - } // namespace literals } // namespace units::si diff --git a/src/include/units/physical/us/area.h b/src/include/units/physical/us/area.h new file mode 100644 index 00000000..0d723af3 --- /dev/null +++ b/src/include/units/physical/us/area.h @@ -0,0 +1,40 @@ +// 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. + +#pragma once + +#include +#include + +namespace units::us { + +struct square_foot : deduced_unit {}; + +inline namespace literals { + +// sq_ft +constexpr auto operator"" sq_ft(unsigned long long l) { return si::area(l); } +constexpr auto operator"" sq_ft(long double l) { return si::area(l); } + +} // namespace literals + +} // namespace units::us diff --git a/src/include/units/physical/us/length.h b/src/include/units/physical/us/length.h new file mode 100644 index 00000000..c2ac6509 --- /dev/null +++ b/src/include/units/physical/us/length.h @@ -0,0 +1,54 @@ +// 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. + +#pragma once + +#include + +namespace units::us { + +struct yard : named_scaled_unit, si::metre> {}; +struct foot : named_scaled_unit, yard> {}; +struct inch : named_scaled_unit, foot> {}; +struct mile : named_scaled_unit, yard> {}; + +inline namespace literals { + +// yd +constexpr auto operator"" yd(unsigned long long l) { return si::length(l); } +constexpr auto operator"" yd(long double l) { return si::length(l); } + +// ft +constexpr auto operator"" ft(unsigned long long l) { return si::length(l); } +constexpr auto operator"" ft(long double l) { return si::length(l); } + +// in +constexpr auto operator"" in(unsigned long long l) { return si::length(l); } +constexpr auto operator"" in(long double l) { return si::length(l); } + +// mi +constexpr auto operator"" mi(unsigned long long l) { return si::length(l); } +constexpr auto operator"" mi(long double l) { return si::length(l); } + +} // namespace literals + +} // namespace units::us diff --git a/src/include/units/physical/us/velocity.h b/src/include/units/physical/us/velocity.h new file mode 100644 index 00000000..ac5d686f --- /dev/null +++ b/src/include/units/physical/us/velocity.h @@ -0,0 +1,40 @@ +// 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. + +#pragma once + +#include +#include + +namespace units::us { + +struct mile_per_hour : deduced_unit {}; + +inline namespace literals { + +// mph +constexpr auto operator"" mph(unsigned long long l) { return si::velocity(l); } +constexpr auto operator"" mph(long double l) { return si::velocity(l); } + +} // namespace literals + +} // namespace units::us diff --git a/src/include/units/physical/us/volume.h b/src/include/units/physical/us/volume.h new file mode 100644 index 00000000..29898cc5 --- /dev/null +++ b/src/include/units/physical/us/volume.h @@ -0,0 +1,40 @@ +// 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. + +#pragma once + +#include +#include + +namespace units::us { + +struct cubic_foot : deduced_unit {}; + +inline namespace literals { + +// cub_ft +constexpr auto operator""cub_ft(unsigned long long l) { return si::volume(l); } +constexpr auto operator""cub_ft(long double l) { return si::volume(l); } + +} // namespace literals + +} // namespace units::us diff --git a/test/unit_test/runtime/fmt_units_test.cpp b/test/unit_test/runtime/fmt_units_test.cpp index 1e721528..72fb7a24 100644 --- a/test/unit_test/runtime/fmt_units_test.cpp +++ b/test/unit_test/runtime/fmt_units_test.cpp @@ -26,10 +26,14 @@ #include "units/physical/si/velocity.h" #include "units/physical/si/volume.h" #include "units/physical/si/surface_tension.h" +#include "units/physical/us/area.h" +#include "units/physical/us/velocity.h" +#include "units/physical/us/volume.h" #include "units/format.h" #include using namespace units::si; +using namespace units::us; TEST_CASE("fmt::format on synthesized unit symbols", "[text][fmt]") { diff --git a/test/unit_test/static/CMakeLists.txt b/test/unit_test/static/CMakeLists.txt index 790ee380..06aa6b1f 100644 --- a/test/unit_test/static/CMakeLists.txt +++ b/test/unit_test/static/CMakeLists.txt @@ -33,6 +33,7 @@ add_library(unit_tests_static si_cgs_test.cpp type_list_test.cpp unit_test.cpp + us_test.cpp ) target_link_libraries(unit_tests_static PRIVATE diff --git a/test/unit_test/static/si_test.cpp b/test/unit_test/static/si_test.cpp index 19a3cb8e..724064ab 100644 --- a/test/unit_test/static/si_test.cpp +++ b/test/unit_test/static/si_test.cpp @@ -59,13 +59,6 @@ static_assert(10km / 5km == 2); static_assert(100mm / 5cm == 2); static_assert(10km / 2 == 5km); -static_assert(1yd == 0.9144m); -static_assert(1yd == 3ft); -static_assert(1ft == 12in); -static_assert(1mi == 1760yd); - -static_assert(5in + 8cm == 207mm); - static_assert(millimetre::symbol == "mm"); static_assert(centimetre::symbol == "cm"); static_assert(kilometre::symbol == "km"); @@ -200,8 +193,6 @@ static_assert(1km / 1s == 1000mps); static_assert(1.0km / 1h == 1kmph); static_assert(1000.0m / 3600.0s == 1kmph); -static_assert(10.0mi / 2h == 5mph); - static_assert(2kmph * 2h == 4km); // static_assert(2kmph * 15min == 500m); // should not compile static_assert(2kmph * 15.0min == 500m); @@ -213,7 +204,6 @@ static_assert(quantity_cast(2000m) / 2kmph == 1h); static_assert(detail::unit_text() == "m/s"); static_assert(kilometre_per_hour::symbol == "km/h"); -static_assert(mile_per_hour::symbol == "mi/h"); // acceleration diff --git a/test/unit_test/static/us_test.cpp b/test/unit_test/static/us_test.cpp new file mode 100644 index 00000000..30bdd9dd --- /dev/null +++ b/test/unit_test/static/us_test.cpp @@ -0,0 +1,59 @@ +// 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 +#include +#include +#include +#include +#include +#include + +namespace { + +using namespace units; +using namespace units::si; +using namespace units::us; + +/* ************** BASE DIMENSIONS **************** */ + +// length + +static_assert(1yd == 0.9144m); +static_assert(1yd == 3ft); +static_assert(1ft == 12in); +static_assert(1mi == 1760yd); + +static_assert(5in + 8cm == 207mm); + + +/* ************** DERIVED DIMENSIONS IN TERMS OF BASE UNITS **************** */ + +// velocity + +static_assert(10.0mi / 2h == 5mph); + +static_assert(mile_per_hour::symbol == "mi/h"); + +} // namespace