qtty-cpp
Header-only C++ wrapper for qtty
Loading...
Searching...
No Matches
velocity.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include "../ffi_core.hpp"
4
#include "
length.hpp
"
5
#include "
time.hpp
"
6
7
namespace
qtty
{
8
9
// ============================================================================
10
// Compound Units: Velocity
11
// ============================================================================
12
// Velocity is a derived quantity representing length divided by time.
13
// Unlike base dimensions (length, time, mass, etc.), compound units are not
14
// directly supported by the C FFI layer. Instead, we use C++ template
15
// metaprogramming to encode the compound relationship in the type system.
16
//
17
// Design: CompoundTag<NumeratorTag, DenominatorTag> encodes a quotient of
18
// two unit types. The division operator (/) creates instances by dividing
19
// the raw values, while the type system tracks the unit relationship.
20
//
21
// Limitation: Conversions between different compound units (e.g., m/s to km/h)
22
// are not automatically supported. Users must manually convert numerator and
23
// denominator separately, then recombine.
24
25
// Template for compound units (e.g., velocity = length/time)
26
template
<
typename
NumeratorTag,
typename
DenominatorTag>
27
struct
CompoundTag
{};
28
29
// Velocity type alias using compound units
30
// This is a template alias, not a concrete type. Instantiate with specific
31
// length and time units, e.g., Velocity<Meter, Second>.
32
template
<
typename
LengthUnit,
typename
TimeUnit>
33
using
Velocity
=
Quantity<CompoundTag<typename LengthUnit::unit_tag, typename TimeUnit::unit_tag>
>;
34
35
// Note: The C API doesn't have explicit velocity unit IDs
36
// We create velocity by dividing length by time, operating on raw values
37
// No unit conversion is available for compound units through the C API
38
39
// ============================================================================
40
// Division Operator: Create Compound Units
41
// ============================================================================
42
// Dividing a length quantity by a time quantity produces a velocity.
43
// The resulting type encodes both the numerator and denominator units,
44
// allowing type-safe operations on velocities while maintaining dimensional
45
// correctness (e.g., you can't add m/s to m/s² by accident).
46
//
47
// Example:
48
// Meter distance(100.0);
49
// Second time(20.0);
50
// auto velocity = distance / time; // Type: MeterPerSecond
51
// std::cout << velocity.value(); // Prints: 5.0
52
53
// Division operator to create velocity from length and time
54
template
<
typename
LengthTag,
typename
TimeTag>
55
Quantity<CompoundTag<LengthTag, TimeTag>
>
operator/
(
56
const
Quantity<LengthTag>
&
length
,
57
const
Quantity<TimeTag>
&
time
)
58
{
59
return
Quantity<CompoundTag<LengthTag, TimeTag>
>(
length
.
value
() /
time
.
value
());
60
}
61
62
// ============================================================================
63
// Common Velocity Type Aliases
64
// ============================================================================
65
// Pre-defined aliases for commonly used velocity combinations.
66
// These improve readability but are purely convenience types—they're just
67
// specific instantiations of Quantity<CompoundTag<...>>.
68
69
// Common velocity type aliases (constructed from division, not convertible)
70
using
MeterPerSecond
=
Quantity<CompoundTag<MeterTag, SecondTag>
>;
71
using
KilometerPerHour
=
Quantity<CompoundTag<KilometerTag, HourTag>
>;
72
73
}
// namespace qtty
qtty::Quantity
Definition
ffi_core.hpp:135
qtty::Quantity::value
constexpr double value() const
Definition
ffi_core.hpp:152
length.hpp
qtty
Definition
ffi_core.hpp:16
qtty::operator/
Quantity< CompoundTag< LengthTag, TimeTag > > operator/(const Quantity< LengthTag > &length, const Quantity< TimeTag > &time)
Definition
velocity.hpp:55
qtty::CompoundTag
Definition
velocity.hpp:27
time.hpp
include
qtty
units
velocity.hpp
Generated by
1.9.8