dimval 0.2.0
Modern C++23 header-only library of dimensional values (units, measures, ranges)
Loading...
Searching...
No Matches
define.hpp File Reference

Macros that expand into a CRTP unit/measure struct and a runtime auto-registration for it. More...

#include <dimval/base.hpp>
#include <dimval/measure.hpp>
#include <dimval/registry.hpp>
#include <dimval/unit.hpp>
#include <commons/color.hpp>
#include <commons/icon.hpp>
#include <commons/icons.hpp>
#include <optional>
Include dependency graph for define.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define DIMVAL_DEFINE_UNIT(Tag, Id, Symbol, ShortName, LongName, Kind, Factor, Icon, Color, ...)
 Define a unit struct in ::dimval and auto-register its descriptor at static-init time.
 
#define DIMVAL_DEFINE_MEASURE(Tag, BaseUnit, Id, Name, Icon, Color, ...)
 Define a measure struct in ::dimval and auto-register its descriptor.
 

Detailed Description

Macros that expand into a CRTP unit/measure struct and a runtime auto-registration for it.

Each unit/measure is its own struct that derives from UnitBase / MeasureBase and publishes its metadata as static constexpr members. The macro arguments fill in those members; optional fields fall through to the defaults declared on UnitBase / MeasureBase unless overridden via VA_ARGS.

The Icon argument is a comms::Icon — prefer a catalog constant such as comms::Icons::mdi::movie_roll (from <commons/icons.hpp>), or build one from any Iconify set with the compile-time validated comms::Icon::from("set:name"). The Color argument is a comms::Color — use a Material UI shade such as comms::Colors::mui::blue[400]. Passing ::std::nullopt for either means "unset / inherit".

Usage from outside the library (custom user units):

DIMVAL_DEFINE_UNIT(Frame, "frame", "frm", "frm", "frame", "count", 1.0,
::comms::Icons::mdi::movie_roll,
::comms::Colors::mui::blue[400])
DIMVAL_DEFINE_MEASURE(FrameCount, ::dimval::Frame,
"frame_count", "Frame count",
::comms::Icons::mdi::movie_roll,
::comms::Colors::mui::blue[400])
#define DIMVAL_DEFINE_MEASURE(Tag, BaseUnit, Id, Name, Icon, Color,...)
Define a measure struct in ::dimval and auto-register its descriptor.
Definition define.hpp:102
#define DIMVAL_DEFINE_UNIT(Tag, Id, Symbol, ShortName, LongName, Kind, Factor, Icon, Color,...)
Define a unit struct in ::dimval and auto-register its descriptor at static-init time.
Definition define.hpp:72
Umbrella header.

Optional fields (offset, formatter, default_precision, no_space_before_symbol) can be supplied as additional macro arguments after the required ones — each one is a static constexpr member declaration that shadows the inherited default:

DIMVAL_DEFINE_UNIT(Celsius, "degC", "°C", "°C", "degree Celsius",
"temperature", 1.0,
::comms::Icons::mdi::thermometer_lines,
::comms::Colors::mui::orange[400],
static constexpr double offset = 273.15;
static constexpr int default_precision = 1;)

All macros open namespace dimval internally. The structs they produce therefore live in ::dimval.

Macro Definition Documentation

◆ DIMVAL_DEFINE_MEASURE

#define DIMVAL_DEFINE_MEASURE (   Tag,
  BaseUnit,
  Id,
  Name,
  Icon,
  Color,
  ... 
)
Value:
namespace dimval { \
struct Tag : ::dimval::MeasureBase<Tag, BaseUnit> { \
static constexpr ::std::string_view id = Id; \
static constexpr ::std::string_view name = Name; \
static constexpr ::dimval::detail::icon_field_t icon = Icon; \
static constexpr ::dimval::detail::color_field_t color = Color; \
__VA_ARGS__ \
}; \
using DIMVAL_DETAIL_CAT(Tag, Value) = ::dimval::MeasureValue<Tag>; \
using DIMVAL_DETAIL_CAT(Tag, ValueUnique) = ::std::unique_ptr<DIMVAL_DETAIL_CAT(Tag, Value)>; \
using DIMVAL_DETAIL_CAT(Tag, ValueShared) = ::std::shared_ptr<DIMVAL_DETAIL_CAT(Tag, Value)>; \
using DIMVAL_DETAIL_CAT(Tag, RangeValue) = ::dimval::MeasureRangeValue<Tag>; \
} \
namespace dimval::detail { \
[[maybe_unused]] inline const int DIMVAL_DETAIL_CAT(_dimval_measure_reg_, Tag) = \
::dimval::detail::register_measure_tag<::dimval::Tag>(); \
}
Closed/open interval over a MeasureValue<M,T>.
Definition range.hpp:196
CRTP base for measure tags.
Definition base.hpp:63
A value carrying both a measure tag and a unit tag.
Definition measure.hpp:31

Define a measure struct in ::dimval and auto-register its descriptor.

BaseUnit is the underlying unit type (must be UnitLike). Icon (a comms::Icon) and Color (a comms::Color) shadow the unit's defaults; pass ::std::nullopt to inherit from the base unit. __VA_ARGS__ is an optional list of static constexpr member declarations that shadow the defaults declared on MeasureBase (e.g. default_precision, formatter).

◆ DIMVAL_DEFINE_UNIT

#define DIMVAL_DEFINE_UNIT (   Tag,
  Id,
  Symbol,
  ShortName,
  LongName,
  Kind,
  Factor,
  Icon,
  Color,
  ... 
)
Value:
namespace dimval { \
struct Tag : ::dimval::UnitBase<Tag> { \
static constexpr ::std::string_view id = Id; \
static constexpr ::std::string_view symbol = Symbol; \
static constexpr ::std::string_view short_name = ShortName; \
static constexpr ::std::string_view long_name = LongName; \
static constexpr ::std::string_view kind = Kind; \
static constexpr double factor = Factor; \
static constexpr ::dimval::detail::icon_field_t icon = Icon; \
static constexpr ::dimval::detail::color_field_t color = Color; \
__VA_ARGS__ \
}; \
using DIMVAL_DETAIL_CAT(Tag, Value) = ::dimval::UnitValue<Tag>; \
using DIMVAL_DETAIL_CAT(Tag, ValueUnique) = ::std::unique_ptr<DIMVAL_DETAIL_CAT(Tag, Value)>; \
using DIMVAL_DETAIL_CAT(Tag, ValueShared) = ::std::shared_ptr<DIMVAL_DETAIL_CAT(Tag, Value)>; \
using DIMVAL_DETAIL_CAT(Tag, RangeValue) = ::dimval::UnitRangeValue<Tag>; \
} \
namespace dimval::detail { \
[[maybe_unused]] inline const int DIMVAL_DETAIL_CAT(_dimval_unit_reg_, Tag) = \
::dimval::detail::register_unit_tag<::dimval::Tag>(); \
}
Closed/open interval over a UnitValue<U,T>.
Definition range.hpp:45
CRTP base for unit tags.
Definition base.hpp:30
A value paired at the type level with a unit tag.
Definition unit.hpp:36

Define a unit struct in ::dimval and auto-register its descriptor at static-init time.

Tag is the unqualified identifier (also used to name the auto-registrar). Id, Symbol, ShortName, LongName, Kind are string literals; Factor is a double; Icon is a comms::Icon (e.g. a ::comms::Icons::mdi::* catalog constant) and Color is a comms::Color (e.g. ::comms::Colors::mui::blue[400]) — pass ::std::nullopt to leave unset. __VA_ARGS__ is an optional list of static constexpr member declarations that shadow the defaults declared on UnitBase.