diff --git a/example/alternative_namespaces/acceleration.h b/example/alternative_namespaces/acceleration.h new file mode 100644 index 00000000..3f104517 --- /dev/null +++ b/example/alternative_namespaces/acceleration.h @@ -0,0 +1,19 @@ + +#pragma once + +#include + +namespace units{ +namespace experimental{ + +namespace acceleration { + +template +using m_per_s2 = units::si::acceleration; + +template +constexpr m_per_s2 g{static_cast(9.80665)}; + +} // namespace acceleration + +}} // units::experimental diff --git a/example/alternative_namespaces/box_example.cpp b/example/alternative_namespaces/box_example.cpp index 85f69008..55836e8e 100644 --- a/example/alternative_namespaces/box_example.cpp +++ b/example/alternative_namespaces/box_example.cpp @@ -1,94 +1,49 @@ -#include -#include -#include -#include -#include -#include -#include + +#include "./length.h" +#include "./acceleration.h" +#include "./density.h" +#include "./force.h" +#include "./mass.h" +#include "./time.h" +#include "./volume.h" + #include -namespace { +using namespace units::experimental; -namespace length { - -template -using m = units::si::length; - -template -using mm = units::si::length; - -} // namespace length - -namespace acceleration { - -template -using mps2 = units::si::acceleration; - -template -constexpr mps2<> g{static_cast(9.80665)}; - -} // namespace acceleration - -namespace force { - -template -using N = units::si::force; - -} - -namespace mass { - -template -using kg = units::si::mass; - -} - -namespace density { - -template -using kgpm3 = units::si::density; - -} - -namespace volume { - -template -using m3 = units::si::volume; - -} -} // namespace struct Box { - static constexpr density::kgpm3<> air_density{1.225}; + + static constexpr auto air_density = density::kg_per_m3<>{1.225}; length::m<> length; length::m<> width; length::m<> height; + constexpr Box(const length::m<>& l, const length::m<>& w, const length::m<>& h) : length{l}, width{w}, height{h} {} + struct contents { - density::kgpm3<> density = air_density; + density::kg_per_m3<> density = air_density; } contents; - Box(const length::m<>& l, const length::m<>& w, const length::m<>& h) : length{l}, width{w}, height{h} {} - - force::N<> filled_weight() const + constexpr force::N<> filled_weight() const { const volume::m3<> volume = length * width * height; const mass::kg<> mass = contents.density * volume; return mass * acceleration::g<>; } - length::m<> fill_level(const mass::kg<>& measured_mass) const + constexpr length::m<> fill_level(const mass::kg<>& measured_mass) const { return height * (measured_mass * acceleration::g<>) / filled_weight(); } - volume::m3<> spare_capacity(const mass::kg<>& measured_mass) const + constexpr volume::m3<> spare_capacity(const mass::kg<>& measured_mass) const { return (height - fill_level(measured_mass)) * width * length; } - void set_contents_density(const density::kgpm3<>& density_in) + constexpr void set_contents_density(const density::kg_per_m3<>& density_in) { assert(density_in > air_density); contents.density = density_in; @@ -99,6 +54,7 @@ struct Box { #include using namespace units::si::literals; + int main() { auto box = Box{1000.0q_mm, 500.0q_mm, 200.0q_mm}; @@ -107,7 +63,7 @@ int main() auto fill_time = 200.0q_s; // time since starting fill auto measured_mass = 20.0q_kg; // measured mass at fill_time - std::cout << "mpusz/units box example...\n"; + std::cout << "mpusz/units box example ( using experimental alternative syntax for defining quantities) ...\n"; std::cout << "fill height at " << fill_time << " = " << box.fill_level(measured_mass) << " (" << (box.fill_level(measured_mass) / box.height) * 100 << "% full)\n"; std::cout << "spare_capacity at " << fill_time << " = " << box.spare_capacity(measured_mass) << '\n'; diff --git a/example/alternative_namespaces/density.h b/example/alternative_namespaces/density.h new file mode 100644 index 00000000..5a9dc60b --- /dev/null +++ b/example/alternative_namespaces/density.h @@ -0,0 +1,19 @@ + + +#pragma once + +#include + +namespace units{ +namespace experimental{ + +namespace density { + +template +using kg_per_m3 = units::si::density; + +} + +}} // units::experimental + + diff --git a/example/alternative_namespaces/force.h b/example/alternative_namespaces/force.h new file mode 100644 index 00000000..a2cefd4d --- /dev/null +++ b/example/alternative_namespaces/force.h @@ -0,0 +1,16 @@ + +#pragma once + +#include + +namespace units{ +namespace experimental{ + +namespace force { + +template +using N = units::si::force; + +} + +}} // units::experimental diff --git a/example/alternative_namespaces/length.h b/example/alternative_namespaces/length.h new file mode 100644 index 00000000..c3fb3a87 --- /dev/null +++ b/example/alternative_namespaces/length.h @@ -0,0 +1,22 @@ + + +#pragma once + +#include + +namespace units{ +namespace experimental{ + +namespace length { + +template +using m = units::si::length; + +template +using mm = units::si::length; + +} // namespace length + +}} // units::experimental + + diff --git a/example/alternative_namespaces/mass.h b/example/alternative_namespaces/mass.h new file mode 100644 index 00000000..39c93c74 --- /dev/null +++ b/example/alternative_namespaces/mass.h @@ -0,0 +1,19 @@ + + +#pragma once + +#include + +namespace units{ +namespace experimental{ + +namespace mass { + +template +using kg = units::si::mass; + +} + +}} // units::experimental + + diff --git a/example/alternative_namespaces/volume.h b/example/alternative_namespaces/volume.h new file mode 100644 index 00000000..a6fa2c39 --- /dev/null +++ b/example/alternative_namespaces/volume.h @@ -0,0 +1,19 @@ + + +#pragma once + +#include + +namespace units{ +namespace experimental{ + +namespace volume { + +template +using m3 = units::si::volume; + +} + +}} // units::experimental + +