diff --git a/include/xo/unit/detail/promoter.hpp b/include/xo/unit/detail/promoter.hpp new file mode 100644 index 00000000..1dc34c78 --- /dev/null +++ b/include/xo/unit/detail/promoter.hpp @@ -0,0 +1,40 @@ +/** @file promoter.hpp + * + * Author: Roland Conybeare + **/ + +#pragma once + +#include "unit.hpp" + +namespace xo { + namespace unit { + /** @class promoter + * + * Auxiliary class driver for quantity::promote(). + * promoter has two specializations: + * 1. if Unit is dimensionless, @c promoter::promote() is the identity function. + * This has the effect of collapsing dimensionless quantities to their representation. + * 2. if Unit has at least one non-empty dimension, + * @c promoter::promote() builds an xo::unit::quantity instance + **/ + template > + struct promoter; + + template + class quantity; + + /* collapse dimensionless quantity to its repr_type> */ + template + struct promoter { + static constexpr Repr promote(Repr x) { return x; }; + }; + + template + struct promoter { + static constexpr quantity promote(Repr x) { return quantity(x); } + }; + } /*namespace unit*/ +} /*namespace xo*/ + +/** end promoter.hpp **/ diff --git a/include/xo/unit/quantity.hpp b/include/xo/unit/quantity.hpp index f8d5dd80..138565d1 100644 --- a/include/xo/unit/quantity.hpp +++ b/include/xo/unit/quantity.hpp @@ -4,18 +4,12 @@ #include "quantity_concept.hpp" #include "unit.hpp" +#include "detail/promoter.hpp" //#include "xo/reflect/Reflect.hpp" //#include "xo/indentlog/scope.hpp" namespace xo { namespace unit { - /** @class promoter - * - * Aux class assister for quantity::promote() - **/ - template > - struct promoter; - // ----- quantity ----- /** @class quantity @@ -72,10 +66,7 @@ namespace xo { static constexpr quantity unit_quantity() { return quantity(1); } /** @brief promote representation to quantity. Same as multiplying by Unit **/ - static constexpr auto promote(Repr x) { - //std::cerr << "quantity::promote: x=" << x << ", R=" << reflect::Reflect::require()->canonical_name() << std::endl; - return promoter::promote(x); - } + static constexpr auto promote(Repr x); ///@} /** @addtogroup quantity-traits **/ @@ -500,18 +491,12 @@ namespace xo { Repr scale_ = 0; }; /*quantity*/ - // ----- promoter ----- - - /* collapse dimensionless quantity to its repr_type> */ template - struct promoter { - static constexpr Repr promote(Repr x) { return x; }; - }; - - template - struct promoter { - static constexpr quantity promote(Repr x) { return quantity(x); } - }; + constexpr auto + quantity::promote(Repr x) { + //std::cerr << "quantity::promote: x=" << x << ", R=" << reflect::Reflect::require()->canonical_name() << std::endl; + return promoter::promote(x); + } // ----- operator+ -----