diff --git a/src/include/units/data/bitrate.h b/src/include/units/data/bitrate.h new file mode 100644 index 00000000..711e0fff --- /dev/null +++ b/src/include/units/data/bitrate.h @@ -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. + +#pragma once + +#include +#include +#include +#include + +namespace units::data { + +struct bit_per_second : unit {}; +struct dim_bitrate : derived_dimension, exp> {}; + +struct kibibit_per_second : deduced_unit {}; +struct mebibit_per_second : deduced_unit {}; +struct gibibit_per_second : deduced_unit {}; +struct tebibit_per_second : deduced_unit {}; +struct pebibit_per_second : deduced_unit {}; + +template +concept Bitrate = QuantityOf; + +template +using bitrate = quantity; + +inline namespace literals { + +// bits +constexpr auto operator""_bps(unsigned long long l) { return bitrate(l); } +constexpr auto operator""_Kibps(unsigned long long l) { return bitrate(l); } +constexpr auto operator""_Mibps(unsigned long long l) { return bitrate(l); } +constexpr auto operator""_Gibps(unsigned long long l) { return bitrate(l); } +constexpr auto operator""_Tibps(unsigned long long l) { return bitrate(l); } +constexpr auto operator""_Pibps(unsigned long long l) { return bitrate(l); } + +} // namespace literals + +} // namespace units::data diff --git a/src/include/units/data/information.h b/src/include/units/data/information.h new file mode 100644 index 00000000..4d28f47b --- /dev/null +++ b/src/include/units/data/information.h @@ -0,0 +1,74 @@ +// 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 +#include +#include + +namespace units::data { + +struct bit : named_unit {}; +struct kibibit : prefixed_unit {}; +struct mebibit : prefixed_unit {}; +struct gibibit : prefixed_unit {}; +struct tebibit : prefixed_unit {}; +struct pebibit : prefixed_unit {}; + +struct byte : named_scaled_unit, bit> {}; +struct kibibyte : prefixed_unit {}; +struct mebibyte : prefixed_unit {}; +struct gibibyte : prefixed_unit {}; +struct tebibyte : prefixed_unit {}; +struct pebibyte : prefixed_unit {}; + +struct dim_information : base_dimension<"information", bit> {}; + +template +concept Information = QuantityOf; + +template +using information = quantity; + +inline namespace literals { + +// bits +constexpr auto operator""b(unsigned long long l) { return information(l); } +constexpr auto operator""Kib(unsigned long long l) { return information(l); } +constexpr auto operator""Mib(unsigned long long l) { return information(l); } +constexpr auto operator""Gib(unsigned long long l) { return information(l); } +constexpr auto operator""Tib(unsigned long long l) { return information(l); } +constexpr auto operator""Pib(unsigned long long l) { return information(l); } + +// bytes +constexpr auto operator""B(unsigned long long l) { return information(l); } +constexpr auto operator""KiB(unsigned long long l) { return information(l); } +constexpr auto operator""MiB(unsigned long long l) { return information(l); } +constexpr auto operator""GiB(unsigned long long l) { return information(l); } +constexpr auto operator""TiB(unsigned long long l) { return information(l); } +constexpr auto operator""PiB(unsigned long long l) { return information(l); } + +} // namespace literals + +} // namespace units::data diff --git a/src/include/units/data/prefixes.h b/src/include/units/data/prefixes.h new file mode 100644 index 00000000..827b39fa --- /dev/null +++ b/src/include/units/data/prefixes.h @@ -0,0 +1,38 @@ +// 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::data { + +struct prefix : prefix_type {}; + +struct kibi : units::prefix> {}; +struct mebi : units::prefix> {}; +struct gibi : units::prefix> {}; +struct tebi : units::prefix> {}; +struct pebi : units::prefix> {}; + +} // namespace units::si diff --git a/src/include/units/dimension_op.h b/src/include/units/dimension_op.h index 294de4b4..5a58c02f 100644 --- a/src/include/units/dimension_op.h +++ b/src/include/units/dimension_op.h @@ -199,7 +199,7 @@ struct dimension_pow_impl>, N> { template struct dimension_pow_impl { - using type = dimension_pow_impl, N>; + using type = dimension_pow_impl, N>::type; }; template diff --git a/src/include/units/physical/dimensions.h b/src/include/units/physical/dimensions.h index 89a18f53..86004125 100644 --- a/src/include/units/physical/dimensions.h +++ b/src/include/units/physical/dimensions.h @@ -29,7 +29,7 @@ namespace units { -inline namespace physical { +namespace physical { template typename DimTemplate> concept DimensionOf = (Dimension || BaseDimension) && is_derived_from_instantiation; @@ -39,148 +39,128 @@ concept QuantityOf = Quantity && is_derived_from_instantiation struct dim_length : base_dimension<"length", U> {}; -template -concept Length = QuantityOf; - -// mass template struct dim_mass : base_dimension<"mass", U> {}; -template -concept Mass = QuantityOf; - -// time template struct dim_time : base_dimension<"time", U> {}; -template -concept Time = QuantityOf; - -// current template struct dim_current : base_dimension<"current", U> {}; -template -concept Current = QuantityOf; - -// temperature template struct dim_temperature : base_dimension<"temperature", U> {}; -template -concept Temperature = QuantityOf; - -// substance template struct dim_substance : base_dimension<"substance", U> {}; -template -concept Substance = QuantityOf; - -// luminous intensity template struct dim_luminous_intensity : base_dimension<"luminous intensity", U> {}; -template -concept LuminousIntensity = QuantityOf; - // ------------------------ derived dimensions ----------------------------- -// frequency template T> struct dim_frequency : derived_dimension> {}; -template -concept Frequency = QuantityOf; - -// area template L> struct dim_area : derived_dimension> {}; -template -concept Area = QuantityOf; - -// volume template L> struct dim_volume : derived_dimension> {}; -template -concept Volume = QuantityOf; - -// velocity template L, DimensionOf T> struct dim_velocity : derived_dimension, exp> {}; -template -concept Velocity = QuantityOf; - -// acceleration template L, DimensionOf T> struct dim_acceleration : derived_dimension, exp> {}; -template -concept Acceleration = QuantityOf; - -// force template M, DimensionOf A> struct dim_force : derived_dimension, exp> {}; -template -concept Force = QuantityOf; - -// energy template F, DimensionOf L> struct dim_energy : derived_dimension, exp> {}; -template -concept Energy = QuantityOf; - -// power template E, DimensionOf T> struct dim_power : derived_dimension, exp> {}; -template -concept Power = QuantityOf; - -// voltage template P, DimensionOf C> struct dim_voltage : derived_dimension, exp> {}; -template -concept Voltage = QuantityOf; - -// electric charge template T, DimensionOf C> struct dim_electric_charge : derived_dimension, exp> {}; -template -concept ElectricCharge = QuantityOf; - -// capacitance template C, DimensionOf V> struct dim_capacitance : derived_dimension, exp> {}; -template -concept Capacitance = QuantityOf; - -// surface tension template F, DimensionOf L> struct dim_surface_tension : derived_dimension, exp> {}; -template -concept SurfaceTension = QuantityOf; - -// pressure template F, DimensionOf A> struct dim_pressure : derived_dimension, exp> {}; -template -concept Pressure = QuantityOf; - } // namespace physical +template +concept Length = physical::QuantityOf; + +template +concept Mass = physical::QuantityOf; + +template +concept Time = physical::QuantityOf; + +template +concept Current = physical::QuantityOf; + +template +concept Temperature = physical::QuantityOf; + +template +concept Substance = physical::QuantityOf; + +template +concept LuminousIntensity = physical::QuantityOf; + +template +concept Frequency = physical::QuantityOf; + +template +concept Area = physical::QuantityOf; + +template +concept Volume = physical::QuantityOf; + +template +concept Velocity = physical::QuantityOf; + +template +concept Acceleration = physical::QuantityOf; + +template +concept Force = physical::QuantityOf; + +template +concept Energy = physical::QuantityOf; + +template +concept Power = physical::QuantityOf; + +template +concept Voltage = physical::QuantityOf; + +template +concept ElectricCharge = physical::QuantityOf; + +template +concept Capacitance = physical::QuantityOf; + +template +concept SurfaceTension = physical::QuantityOf; + +template +concept Pressure = physical::QuantityOf; + } // namespace units diff --git a/test/unit_test/static/CMakeLists.txt b/test/unit_test/static/CMakeLists.txt index b448503c..2447b832 100644 --- a/test/unit_test/static/CMakeLists.txt +++ b/test/unit_test/static/CMakeLists.txt @@ -22,7 +22,8 @@ add_library(unit_tests_static # cgs_test.cpp -# custom_unit_test.cpp + custom_unit_test.cpp + data_test.cpp dimension_op_test.cpp # fixed_string_test.cpp math_test.cpp diff --git a/test/unit_test/static/custom_unit_test.cpp b/test/unit_test/static/custom_unit_test.cpp index 2df947e8..54c0c056 100644 --- a/test/unit_test/static/custom_unit_test.cpp +++ b/test/unit_test/static/custom_unit_test.cpp @@ -20,70 +20,37 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#include -#include +#include +#include #include -/* ************** DERIVED DIMENSIONS THAT INCLUDE UNITS WITH SPECIAL NAMES **************** */ - namespace { - struct base_dim_digital_information : units::base_dimension<"digital information", "b"> {}; +using namespace units; +using namespace units::si; - struct digital_information : units::derived_dimension> {}; +// power spectral density +struct sq_volt_per_hertz : unit {}; +struct dim_power_spectral_density : derived_dimension, units::exp> {}; - template - concept DigitalInformation = units::QuantityOf; +template +using power_spectral_density = quantity; - struct data_prefix : units::prefix_type {}; +// amplitude spectral density +struct volt_per_sqrt_hertz : unit {}; +struct dim_amplitude_spectral_density : derived_dimension, units::exp> {}; - struct kibi : units::prefix, "Ki"> {}; - - struct bit : units::named_coherent_derived_unit {}; - struct kilobit : units::prefixed_derived_unit {}; - - struct byte : units::named_scaled_derived_unit, data_prefix> {}; - struct kilobyte : units::prefixed_derived_unit {}; - - inline namespace literals { - - constexpr auto operator""_b(unsigned long long l) { return units::quantity(l); } - constexpr auto operator""_Kib(unsigned long long l) { return units::quantity(l); } - constexpr auto operator""_B(unsigned long long l) { return units::quantity(l); } - constexpr auto operator""_KiB(unsigned long long l) { return units::quantity(l); } - - } -} - -namespace { - - static_assert(1_B == 8_b); - static_assert(1024_b == 1_Kib); - static_assert(1024_B == 1_KiB); - static_assert(8 * 1024_b == 1_KiB); - static_assert(8 * 1_Kib == 1_KiB); +template +using amplitude_spectral_density = quantity; } namespace { - using namespace units; +static_assert(std::is_same_v, dim_amplitude_spectral_density>); +static_assert(std::is_same_v, dim_power_spectral_density>); - // power spectral density - struct power_spectral_density : derived_dimension, units::exp> {}; - struct sq_volt_per_hertz : coherent_derived_unit {}; - - // amplitude spectral density - struct amplitude_spectral_density : derived_dimension, units::exp> {}; - struct volt_per_sqrt_hertz : coherent_derived_unit {}; -} - -namespace { - - static_assert(std::is_same_v, amplitude_spectral_density>); - static_assert(std::is_same_v, power_spectral_density>); - - static_assert(std::is_same_v(quantity(4))), decltype(quantity(16))>); - static_assert(std::is_same_v(16))), decltype(quantity(4))>); +static_assert(std::is_same_v(amplitude_spectral_density(4))), decltype(power_spectral_density(16))>); +static_assert(std::is_same_v(16))), decltype(amplitude_spectral_density(4))>); } diff --git a/test/unit_test/static/data_test.cpp b/test/unit_test/static/data_test.cpp new file mode 100644 index 00000000..14b483b2 --- /dev/null +++ b/test/unit_test/static/data_test.cpp @@ -0,0 +1,48 @@ +// 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 + +/* ************** DERIVED DIMENSIONS THAT INCLUDE UNITS WITH SPECIAL NAMES **************** */ + +namespace { + +using namespace units::data; + +// information + +static_assert(1B == 8b); +static_assert(1024b == 1Kib); +static_assert(1024B == 1KiB); +static_assert(8 * 1024b == 1KiB); +static_assert(8 * 1Kib == 1KiB); + +static_assert(1Kib == 1024b); +static_assert(1Mib == 1024Kib); +static_assert(1Gib == 1024Mib); +static_assert(1Tib == 1024Gib); +static_assert(1Pib == 1024Tib); + +// bitrate + +}