moddef-cpp
Idiomatic C++ runtime for ModDef
Loading...
Searching...
No Matches
value.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
11#ifndef MODDEF_VALUE_HPP
12#define MODDEF_VALUE_HPP
13
14#include <cstdint>
15#include <optional>
16#include <string_view>
17
18#include "moddef/str.hpp"
19
20extern "C" {
21#include "moddef/codec.h"
22}
23
24namespace moddef {
25
26enum class ValueKind : std::uint8_t {
27 F64 = MD_VAL_F64,
28 U64 = MD_VAL_U64,
29 I64 = MD_VAL_I64,
30 Bool = MD_VAL_BOOL,
31 Flags = MD_VAL_FLAGS,
32 Fields = MD_VAL_FIELDS,
33 DateTime = MD_VAL_DATETIME,
34 Unavailable = MD_VAL_UNAVAILABLE,
35};
36
38class Value {
39 public:
40 Value() = default;
41 explicit Value(md_value_t v) noexcept : v_(v) {}
42
43 ValueKind kind() const noexcept { return static_cast<ValueKind>(v_.kind); }
44 const md_value_t& raw() const noexcept { return v_; }
45
46 bool is_unavailable() const noexcept { return v_.kind == MD_VAL_UNAVAILABLE; }
47
48 // §8.4 sentinel meaning (empty unless is_unavailable()).
49 std::string_view unavailable_meaning() const noexcept {
50 return is_unavailable() ? from_c(v_.na_meaning) : std::string_view{};
51 }
52
53 std::optional<double> as_f64() const noexcept {
54 switch (v_.kind) {
55 case MD_VAL_F64: return v_.v.f64;
56 case MD_VAL_U64: return static_cast<double>(v_.v.u64);
57 case MD_VAL_I64: return static_cast<double>(v_.v.i64);
58 case MD_VAL_BOOL: return v_.v.b ? 1.0 : 0.0;
59 default: return std::nullopt;
60 }
61 }
62
63 std::optional<std::int64_t> as_i64() const noexcept {
64 switch (v_.kind) {
65 case MD_VAL_I64: return v_.v.i64;
66 case MD_VAL_U64: return static_cast<std::int64_t>(v_.v.u64);
67 case MD_VAL_BOOL: return v_.v.b ? 1 : 0;
68 case MD_VAL_DATETIME: return v_.v.epoch;
69 default: return std::nullopt;
70 }
71 }
72
73 std::optional<std::uint64_t> as_u64() const noexcept {
74 if (v_.kind == MD_VAL_U64) return v_.v.u64;
75 return std::nullopt;
76 }
77
78 std::optional<bool> as_bool() const noexcept {
79 if (v_.kind == MD_VAL_BOOL) return v_.v.b;
80 return std::nullopt;
81 }
82
83 // Raw mask (§13.2 flags) or packed window (§13 fields).
84 std::optional<std::uint64_t> as_bits() const noexcept {
85 if (v_.kind == MD_VAL_FLAGS || v_.kind == MD_VAL_FIELDS) return v_.v.bits;
86 return std::nullopt;
87 }
88
89 // Epoch seconds/millis per the point's encoding (§8.5).
90 std::optional<std::int64_t> as_datetime() const noexcept {
91 if (v_.kind == MD_VAL_DATETIME) return v_.v.epoch;
92 return std::nullopt;
93 }
94
95 private:
96 md_value_t v_{};
97};
98
99} // namespace moddef
100
101#endif // MODDEF_VALUE_HPP
A decoded point value: a typed view over the C core's md_value_t.
Definition value.hpp:38
Value(md_value_t v) noexcept
Definition value.hpp:41
ValueKind kind() const noexcept
Definition value.hpp:43
const md_value_t & raw() const noexcept
Definition value.hpp:44
bool is_unavailable() const noexcept
Definition value.hpp:46
Value()=default
std::optional< std::uint64_t > as_bits() const noexcept
Definition value.hpp:84
std::optional< bool > as_bool() const noexcept
Definition value.hpp:78
std::optional< std::uint64_t > as_u64() const noexcept
Definition value.hpp:73
std::optional< std::int64_t > as_i64() const noexcept
Definition value.hpp:63
std::optional< double > as_f64() const noexcept
Definition value.hpp:53
std::optional< std::int64_t > as_datetime() const noexcept
Definition value.hpp:90
std::string_view unavailable_meaning() const noexcept
Definition value.hpp:49
Definition device.hpp:28
ValueKind
Definition value.hpp:26
Error from_c(md_err_t e) noexcept
Definition error.hpp:46
std::string_view <-> md_str_t bridging.