Skip to content

Commit

Permalink
xo-unit: refactor: move promoter aux class to detail/ subdir
Browse files Browse the repository at this point in the history
  • Loading branch information
Rconybea committed Apr 5, 2024
1 parent c25930b commit eea10d8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
40 changes: 40 additions & 0 deletions include/xo/unit/detail/promoter.hpp
Original file line number Diff line number Diff line change
@@ -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 <typename Unit, typename Repr, bool Dimensionless = dimensionless_v<Unit> >
struct promoter;

template <typename Unit, typename Repr>
class quantity;

/* collapse dimensionless quantity to its repr_type> */
template <typename Unit, typename Repr>
struct promoter<Unit, Repr, /*Dimensionless*/ true> {
static constexpr Repr promote(Repr x) { return x; };
};

template <typename Unit, typename Repr>
struct promoter<Unit, Repr, /*Dimensionless*/ false> {
static constexpr quantity<Unit, Repr> promote(Repr x) { return quantity<Unit, Repr>(x); }
};
} /*namespace unit*/
} /*namespace xo*/

/** end promoter.hpp **/
29 changes: 7 additions & 22 deletions include/xo/unit/quantity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename Unit, typename Repr, bool Dimensionless = dimensionless_v<Unit> >
struct promoter;

// ----- quantity -----

/** @class quantity
Expand Down Expand Up @@ -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<U,R>::promote: x=" << x << ", R=" << reflect::Reflect::require<Repr>()->canonical_name() << std::endl;
return promoter<Unit, Repr>::promote(x);
}
static constexpr auto promote(Repr x);
///@}

/** @addtogroup quantity-traits **/
Expand Down Expand Up @@ -500,18 +491,12 @@ namespace xo {
Repr scale_ = 0;
}; /*quantity*/

// ----- promoter -----

/* collapse dimensionless quantity to its repr_type> */
template <typename Unit, typename Repr>
struct promoter<Unit, Repr, /*Dimensionless*/ true> {
static constexpr Repr promote(Repr x) { return x; };
};

template <typename Unit, typename Repr>
struct promoter<Unit, Repr, /*Dimensionless*/ false> {
static constexpr quantity<Unit, Repr> promote(Repr x) { return quantity<Unit, Repr>(x); }
};
constexpr auto
quantity<Unit, Repr>::promote(Repr x) {
//std::cerr << "quantity<U,R>::promote: x=" << x << ", R=" << reflect::Reflect::require<Repr>()->canonical_name() << std::endl;
return promoter<Unit, Repr>::promote(x);
}

// ----- operator+ -----

Expand Down

0 comments on commit eea10d8

Please sign in to comment.