diff --git a/README.md b/README.md index fa2fa3ed..71c63432 100644 --- a/README.md +++ b/README.md @@ -469,12 +469,15 @@ reflect-cpp supports the following containers from the C++ standard library: In addition, it supports the following custom containers: +- `rfl::Binary`: Used to express numbers in binary format. - `rfl::Box`: Similar to `std::unique_ptr`, but (almost) guaranteed to never be null. - `rfl::Bytestring`: An alias for `std::basic_string`. Supported by BSON, CBOR, flexbuffers and msgpack. - `rfl::Generic`: A catch-all type that can represent (almost) anything. +- `rfl::Hex`: Used to express numbers in hex format. - `rfl::Literal`: An explicitly enumerated string. - `rfl::NamedTuple`: Similar to `std::tuple`, but with named fields that can be retrieved via their name at compile time. - `rfl::Object`: A map-like type representing a object with field names that are unknown at compile time. +- `rfl::Oct`: Used to express numbers in octal format. - `rfl::Ref`: Similar to `std::shared_ptr`, but (almost) guaranteed to never be null. - `rfl::Result`: Allows for exception-free programming. - `rfl::TaggedUnion`: Similar to `std::variant`, but with explicit tags that make parsing more efficient. diff --git a/docs/README.md b/docs/README.md index 6fbc15cf..a115311f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -34,6 +34,8 @@ 1.15) [rfl::Bytestring](https://github.com/getml/reflect-cpp/blob/main/docs/bytestring.md) - Describes how reflect-cpp handles binary strings for formats that support them. +1.16) [rfl::Binary, rfl::Hex and rfl::Oct](https://github.com/getml/reflect-cpp/blob/main/docs/number_systems.md)- For expressing numbers in different formats. + ## 2) Validation 2.1) [Regex patterns](https://github.com/getml/reflect-cpp/blob/main/docs/patterns.md) - For requiring that strings follow used-defined regex patterns. diff --git a/docs/number_systems.md b/docs/number_systems.md new file mode 100644 index 00000000..89c58687 --- /dev/null +++ b/docs/number_systems.md @@ -0,0 +1,30 @@ +# `rfl::Binary`, `rfl::Hex` and `rfl::Oct` + +`rfl::Binary`, `rfl::Hex` and `rfl::Oct` can be used to express numbers +in binary, hex and octal format. + +```cpp +struct ExampleStruct { + rfl::Binary binary; + rfl::Hex hex; + rfl::Oct oct; +}; + +const auto example = ExampleStruct{ + .binary = 30, + .hex = 30, + .oct = 30}; +``` + +This results in the following JSON: + +```json +{"binary":"00011110","hex":"1e","oct":"36"} +``` + +Note that the contained type must be integral for `rfl::Hex` and `rfl::Oct`. For `rfl::Binary`, it must be unsigned. Moreover, +the number of digits for `rfl::Binary` will be determined by the bitsize of the type. + +You can access the contained value using `.value()`, `.get()` or simply `operator()`. + +You can produce the string representation using `.str()`. diff --git a/include/rfl.hpp b/include/rfl.hpp index 4806e59e..d6ad0745 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -11,6 +11,7 @@ #include "rfl/AllOf.hpp" #include "rfl/AnyOf.hpp" #include "rfl/Attribute.hpp" +#include "rfl/Binary.hpp" #include "rfl/Box.hpp" #include "rfl/Bytestring.hpp" #include "rfl/Description.hpp" @@ -18,11 +19,13 @@ #include "rfl/Field.hpp" #include "rfl/Flatten.hpp" #include "rfl/Generic.hpp" +#include "rfl/Hex.hpp" #include "rfl/Literal.hpp" #include "rfl/NamedTuple.hpp" #include "rfl/NoFieldNames.hpp" #include "rfl/NoOptionals.hpp" #include "rfl/Object.hpp" +#include "rfl/Oct.hpp" #include "rfl/OneOf.hpp" #include "rfl/Pattern.hpp" #include "rfl/PatternValidator.hpp" diff --git a/include/rfl/Binary.hpp b/include/rfl/Binary.hpp new file mode 100644 index 00000000..1b0aee2c --- /dev/null +++ b/include/rfl/Binary.hpp @@ -0,0 +1,127 @@ +#ifndef RFL_BINARY_HPP_ +#define RFL_BINARY_HPP_ + +#include +#include +#include +#include +#include +#include +#include + +namespace rfl { + +/// Used to define a field in the NamedTuple. +template +requires std::is_unsigned_v +struct Binary { + /// The underlying type. + using Type = T; + + using ReflectionType = std::string; + + using Bitset = std::bitset; + + Binary() : value_(0) {} + + Binary(const Type& _value) : value_(_value) {} + + Binary(Binary&& _other) noexcept = default; + + Binary(const Binary& _other) = default; + + template + Binary(const Binary& _other) : value_(_other.get()) {} + + template + Binary(Binary&& _other) : value_(_other.get()) {} + + template , + bool>::type = true> + Binary(const U& _value) : value_(_value) {} + + template , + bool>::type = true> + Binary(U&& _value) noexcept : value_(std::forward(_value)) {} + + template , + bool>::type = true> + Binary(const Binary& _other) : value_(_other.value()) {} + + Binary(const std::string& _str) + : value_(static_cast(Bitset{_str}.to_ullong())) {} + + ~Binary() = default; + + /// Returns the underlying object. + const Type& get() const { return value_; } + + /// Returns the underlying object. + Type& operator()() { return value_; } + + /// Returns the underlying object. + const Type& operator()() const { return value_; } + + /// Assigns the underlying object. + auto& operator=(const Type& _value) { + value_ = _value; + return *this; + } + + /// Assigns the underlying object. + template , + bool>::type = true> + auto& operator=(const U& _value) { + value_ = _value; + return *this; + } + + /// Assigns the underlying object. + Binary& operator=(const Binary& _other) = default; + + /// Assigns the underlying object. + Binary& operator=(Binary&& _other) = default; + + /// Assigns the underlying object. + template + auto& operator=(const Binary& _other) { + value_ = _other.get(); + return *this; + } + + /// Assigns the underlying object. + template + auto& operator=(const std::string& _str) { + value_ = static_cast(Bitset{_str}.to_ullong()); + return *this; + } + + /// Assigns the underlying object. + template + auto& operator=(Binary&& _other) { + value_ = std::forward(_other.value_); + return *this; + } + + /// Necessary for the automated parsing to work. + std::string reflection() const { return Bitset{value_}.to_string(); } + + /// Assigns the underlying object. + void set(const Type& _value) { value_ = _value; } + + /// Returns the underlying value as a string, alias for .reflection(). + std::string str() const { return reflection(); } + + /// Returns the underlying object. + Type& value() { return value_; } + + /// Returns the underlying object. + const Type& value() const { return value_; } + + /// The underlying value. + Type value_; +}; + +} // namespace rfl + +#endif // RFL_FIELD_HPP_ diff --git a/include/rfl/Hex.hpp b/include/rfl/Hex.hpp new file mode 100644 index 00000000..42811e17 --- /dev/null +++ b/include/rfl/Hex.hpp @@ -0,0 +1,129 @@ +#ifndef RFL_HEX_HPP_ +#define RFL_HEX_HPP_ + +#include +#include +#include +#include +#include +#include + +namespace rfl { + +/// Used to define a field in the NamedTuple. +template +requires std::is_integral_v +struct Hex { + /// The underlying type. + using Type = T; + + using ReflectionType = std::string; + + Hex() : value_(0) {} + + Hex(const Type& _value) : value_(_value) {} + + Hex(Hex&& _other) noexcept = default; + + Hex(const Hex& _other) = default; + + template + Hex(const Hex& _other) : value_(_other.get()) {} + + template + Hex(Hex&& _other) : value_(_other.get()) {} + + template , + bool>::type = true> + Hex(const U& _value) : value_(_value) {} + + template , + bool>::type = true> + Hex(U&& _value) noexcept : value_(std::forward(_value)) {} + + template , + bool>::type = true> + Hex(const Hex& _other) : value_(_other.value()) {} + + Hex(const std::string& _str) { + std::istringstream(_str) >> std::hex >> value_; + } + + ~Hex() = default; + + /// Returns the underlying object. + const Type& get() const { return value_; } + + /// Returns the underlying object. + Type& operator()() { return value_; } + + /// Returns the underlying object. + const Type& operator()() const { return value_; } + + /// Assigns the underlying object. + auto& operator=(const Type& _value) { + value_ = _value; + return *this; + } + + /// Assigns the underlying object. + template , + bool>::type = true> + auto& operator=(const U& _value) { + value_ = _value; + return *this; + } + + /// Assigns the underlying object. + Hex& operator=(const Hex& _other) = default; + + /// Assigns the underlying object. + Hex& operator=(Hex&& _other) = default; + + /// Assigns the underlying object. + template + auto& operator=(const Hex& _other) { + value_ = _other.get(); + return *this; + } + + /// Assigns the underlying object. + template + auto& operator=(const std::string& _str) { + std::istringstream(_str) >> std::hex >> value_; + return *this; + } + + /// Assigns the underlying object. + template + auto& operator=(Hex&& _other) { + value_ = std::forward(_other.value_); + return *this; + } + + /// Necessary for the automated parsing to work. + std::string reflection() const { + std::stringstream stream; + stream << std::hex << value_; + return stream.str(); + } + + /// Assigns the underlying object. + void set(const Type& _value) { value_ = _value; } + + /// Returns the underlying value as a string, alias for .reflection(). + std::string str() const { return reflection(); } + + /// Returns the underlying object. + Type& value() { return value_; } + + /// Returns the underlying object. + const Type& value() const { return value_; } + + /// The underlying value. + Type value_; +}; + +} // namespace rfl + +#endif // RFL_FIELD_HPP_ diff --git a/include/rfl/Oct.hpp b/include/rfl/Oct.hpp new file mode 100644 index 00000000..b466df0e --- /dev/null +++ b/include/rfl/Oct.hpp @@ -0,0 +1,130 @@ +#ifndef RFL_OCT_HPP_ +#define RFL_OCT_HPP_ + +#include +#include +#include +#include +#include +#include +#include + +namespace rfl { + +/// Used to define a field in the NamedTuple. +template +requires std::is_integral_v +struct Oct { + /// The underlying type. + using Type = T; + + using ReflectionType = std::string; + + Oct() : value_(0) {} + + Oct(const Type& _value) : value_(_value) {} + + Oct(Oct&& _other) noexcept = default; + + Oct(const Oct& _other) = default; + + template + Oct(const Oct& _other) : value_(_other.get()) {} + + template + Oct(Oct&& _other) : value_(_other.get()) {} + + template , + bool>::type = true> + Oct(const U& _value) : value_(_value) {} + + template , + bool>::type = true> + Oct(U&& _value) noexcept : value_(std::forward(_value)) {} + + template , + bool>::type = true> + Oct(const Oct& _other) : value_(_other.value()) {} + + Oct(const std::string& _str) { + std::istringstream(_str) >> std::oct >> value_; + } + + ~Oct() = default; + + /// Returns the underlying object. + const Type& get() const { return value_; } + + /// Returns the underlying object. + Type& operator()() { return value_; } + + /// Returns the underlying object. + const Type& operator()() const { return value_; } + + /// Assigns the underlying object. + auto& operator=(const Type& _value) { + value_ = _value; + return *this; + } + + /// Assigns the underlying object. + template , + bool>::type = true> + auto& operator=(const U& _value) { + value_ = _value; + return *this; + } + + /// Assigns the underlying object. + Oct& operator=(const Oct& _other) = default; + + /// Assigns the underlying object. + Oct& operator=(Oct&& _other) = default; + + /// Assigns the underlying object. + template + auto& operator=(const Oct& _other) { + value_ = _other.get(); + return *this; + } + + /// Assigns the underlying object. + template + auto& operator=(const std::string& _str) { + std::istringstream(_str) >> std::oct >> value_; + return *this; + } + + /// Assigns the underlying object. + template + auto& operator=(Oct&& _other) { + value_ = std::forward(_other.value_); + return *this; + } + + /// Necessary for the automated parsing to work. + std::string reflection() const { + std::stringstream stream; + stream << std::oct << value_; + return stream.str(); + } + + /// Assigns the underlying object. + void set(const Type& _value) { value_ = _value; } + + /// Returns the underlying value as a string, alias for .reflection(). + std::string str() const { return reflection(); } + + /// Returns the underlying object. + Type& value() { return value_; } + + /// Returns the underlying object. + const Type& value() const { return value_; } + + /// The underlying value. + Type value_; +}; + +} // namespace rfl + +#endif // RFL_FIELD_HPP_ diff --git a/tests/json/test_binary.cpp b/tests/json/test_binary.cpp new file mode 100644 index 00000000..9cabed7e --- /dev/null +++ b/tests/json/test_binary.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_binary { + +struct TestStruct { + rfl::Binary binary; +}; + +TEST(json, test_binary) { + const auto test = TestStruct{.binary = 30}; + write_and_read(test, R"({"binary":"00011110"})"); +} +} // namespace test_binary diff --git a/tests/json/test_hex.cpp b/tests/json/test_hex.cpp new file mode 100644 index 00000000..3f4a480e --- /dev/null +++ b/tests/json/test_hex.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_hex { + +struct TestStruct { + rfl::Hex hex; +}; + +TEST(json, test_hex) { + const auto test = TestStruct{.hex = 30}; + write_and_read(test, R"({"hex":"1e"})"); +} +} // namespace test_hex diff --git a/tests/json/test_oct.cpp b/tests/json/test_oct.cpp new file mode 100644 index 00000000..4c4a634d --- /dev/null +++ b/tests/json/test_oct.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_oct { + +struct TestStruct { + rfl::Oct oct; +}; + +TEST(json, test_oct) { + const auto test = TestStruct{.oct = 30}; + write_and_read(test, R"({"oct":"36"})"); +} +} // namespace test_oct