Crunch
A Message Definition Language for Getting Things Right
Loading...
Searching...
No Matches
crunch_scalar.hpp
1#pragma once
2
3#include <array>
4#include <crunch/validators/crunch_validators.hpp>
5#include <cstdint>
6#include <optional>
7
8namespace Crunch::fields {
9
21template <class ScalarType, typename... Validators>
22 requires(Validator<Validators, ScalarType> && ...) &&
23 (sizeof...(Validators) > 0) && std::regular<ScalarType>
24class Scalar {
25 public:
26 using ValueType = ScalarType;
27
28 constexpr Scalar() = default;
29 // cppcheck-suppress noExplicitConstructor
30 // TODO: Fixing this is a decent sized refactor
31 // and frankly its probably always fine to implicit convert.
32 // But I should come back to this and fix it at some point.
33 constexpr Scalar(ScalarType v) : value_(v) {}
34
35 constexpr std::optional<Error> set(ScalarType v) noexcept {
36 if (auto err = Validate(v); err) {
37 return err;
38 }
39 value_ = v;
40 return std::nullopt;
41 }
42
43 constexpr void set_without_validation(ScalarType v) noexcept { value_ = v; }
44
45 [[nodiscard]] constexpr ScalarType get() const noexcept { return value_; }
46
47 constexpr void clear() noexcept { value_ = {}; }
48
52 [[nodiscard]] constexpr bool operator==(
53 const Scalar& other) const noexcept {
54 return value_ == other.value_;
55 }
56
62 [[nodiscard]] constexpr auto Validate() const noexcept
63 -> std::optional<Error> {
64 return Validate(value_);
65 }
66
75 [[nodiscard]] static constexpr auto Validate(ScalarType v,
76 FieldId id = 0) noexcept
77 -> std::optional<Error> {
78 for (const auto& result : {Validators::Check(v, id)...}) {
79 if (result.has_value()) {
80 return result;
81 }
82 }
83 return std::nullopt;
84 }
85
86 private:
87 ScalarType value_{};
88};
89
90template <typename T>
91struct is_scalar : std::false_type {};
92
93template <class ScalarType, typename... Validators>
94struct is_scalar<Scalar<ScalarType, Validators...>> : std::true_type {};
95
96template <typename T>
97inline constexpr bool is_scalar_v = is_scalar<T>::value;
98
99template <typename... Validators>
100using Int32 = Scalar<int32_t, Validators...>;
101
102template <typename... Validators>
103using Int16 = Scalar<int16_t, Validators...>;
104
105template <typename... Validators>
106using Int8 = Scalar<int8_t, Validators...>;
107
108template <typename... Validators>
109using UInt32 = Scalar<uint32_t, Validators...>;
110
111template <typename... Validators>
112using UInt16 = Scalar<uint16_t, Validators...>;
113
114template <typename... Validators>
115using UInt8 = Scalar<uint8_t, Validators...>;
116
117template <typename... Validators>
118using Float32 = Scalar<float, Validators...>;
119
120template <typename... Validators>
121using Float64 = Scalar<double, Validators...>;
122
123template <typename... Validators>
124using Bool = Scalar<bool, Validators...>;
125
126} // namespace Crunch::fields
A validated, typed message field.
Definition: crunch_scalar.hpp:24
static constexpr auto Validate(ScalarType v, FieldId id=0) noexcept -> std::optional< Error >
Validates a value against all validators without setting it.
Definition: crunch_scalar.hpp:75
constexpr bool operator==(const Scalar &other) const noexcept
Checks if two fields are equal.
Definition: crunch_scalar.hpp:52
constexpr auto Validate() const noexcept -> std::optional< Error >
Validates the current value of the field against all validators.
Definition: crunch_scalar.hpp:62
int32_t FieldId
Unique identifier for a field within a Crunch message.
Definition: crunch_types.hpp:11
Represents an error occurred during Crunch operations.
Definition: crunch_types.hpp:75
Definition: crunch_scalar.hpp:91