qtty-cpp 0.4.5
Header-only C++ wrapper for qtty
Loading...
Searching...
No Matches
velocity.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2// Copyright (C) 2026 Vallés Puig, Ramon
3
4#pragma once
5
6#include "../ffi_core.hpp"
7#include "length.hpp"
8#include "time.hpp"
9
10namespace qtty {
11
12// ============================================================================
13// Compound Units: Velocity
14// ============================================================================
15// Velocity is a derived quantity representing length divided by time.
16// Compound units are fully supported through the FFI layer via
17// qtty_derived_make() and qtty_derived_convert(), which independently convert
18// numerator and denominator units to produce the correct result.
19//
20// Design: CompoundTag<NumeratorTag, DenominatorTag> encodes a quotient of
21// two unit types. The type system tracks the unit relationship at compile
22// time, while the Rust FFI layer handles conversions at runtime.
23//
24// Conversions between different compound units (e.g., m/s to km/h) are
25// fully supported via the .to<>() method, just like simple unit conversions.
26
27// Velocity type alias using compound units
28// This is a template alias, not a concrete type. Instantiate with specific
29// length and time units, e.g., Velocity<Meter, Second>.
30template <typename LengthUnit, typename TimeUnit>
32
33// ============================================================================
34// Division Operator: Create Compound Units
35// ============================================================================
36// Dividing a length quantity by a time quantity produces a velocity.
37// The resulting type encodes both the numerator and denominator units,
38// allowing type-safe operations on velocities while maintaining dimensional
39// correctness (e.g., you can't add m/s to m/s² by accident).
40//
41// Example:
42// Meter distance(100.0);
43// Second time(20.0);
44// auto velocity = distance / time; // Type: MeterPerSecond
45// std::cout << velocity.value(); // Prints: 5.0
46
47// Division operator to create velocity from length and time
48template <typename LengthTag, typename TimeTag>
53
54// ============================================================================
55// UnitTraits for Compound Units
56// ============================================================================
57// Compound tags expose numerator_unit_id() and denominator_unit_id() instead
58// of a single unit_id(). The Quantity::to<>() template detects compound tags
59// via is_compound_v<> and dispatches to qtty_derived_convert() accordingly.
60
61// Generic template: derives unit IDs from the component tags
62template <typename NumeratorTag, typename DenominatorTag>
66 static constexpr std::string_view symbol() { return ""; }
67};
68
69// Specializations for common velocity types: provide human-readable symbols
71 static constexpr UnitId numerator_unit_id() { return UNIT_ID_METER; }
72 static constexpr UnitId denominator_unit_id() { return UNIT_ID_SECOND; }
73 static constexpr std::string_view symbol() { return "m/s"; }
74};
75
77 static constexpr UnitId numerator_unit_id() { return UNIT_ID_KILOMETER; }
78 static constexpr UnitId denominator_unit_id() { return UNIT_ID_HOUR; }
79 static constexpr std::string_view symbol() { return "km/h"; }
80};
81
82template <> struct UnitTraits<CompoundTag<MeterTag, HourTag>> {
83 static constexpr UnitId numerator_unit_id() { return UNIT_ID_METER; }
84 static constexpr UnitId denominator_unit_id() { return UNIT_ID_HOUR; }
85 static constexpr std::string_view symbol() { return "m/h"; }
86};
87
89 static constexpr UnitId numerator_unit_id() { return UNIT_ID_KILOMETER; }
90 static constexpr UnitId denominator_unit_id() { return UNIT_ID_SECOND; }
91 static constexpr std::string_view symbol() { return "km/s"; }
92};
93
95 static constexpr UnitId numerator_unit_id() { return UNIT_ID_METER; }
96 static constexpr UnitId denominator_unit_id() { return UNIT_ID_MINUTE; }
97 static constexpr std::string_view symbol() { return "m/min"; }
98};
99
101 static constexpr UnitId numerator_unit_id() { return UNIT_ID_KILOMETER; }
102 static constexpr UnitId denominator_unit_id() { return UNIT_ID_MINUTE; }
103 static constexpr std::string_view symbol() { return "km/min"; }
104};
105
106// ============================================================================
107// Common Velocity Type Aliases
108// ============================================================================
109// Pre-defined aliases for commonly used velocity combinations.
110// These improve readability but are purely convenience types—they're just
111// specific instantiations of Quantity<CompoundTag<...>>.
112
119
120} // namespace qtty
constexpr bool is_angular_v
Definition angles.hpp:49
Quantity< CompoundTag< LengthTag, TimeTag > > operator/(const Quantity< LengthTag > &length, const Quantity< TimeTag > &time)
Definition velocity.hpp:49
static constexpr std::string_view symbol()
Definition velocity.hpp:79
static constexpr std::string_view symbol()
Definition velocity.hpp:85
static constexpr std::string_view symbol()
Definition velocity.hpp:97
static constexpr std::string_view symbol()
Definition velocity.hpp:73