diff --git a/docs/default_val.md b/docs/default_val.md index e6b61fdd..7801dfb6 100644 --- a/docs/default_val.md +++ b/docs/default_val.md @@ -25,7 +25,7 @@ std::string s = p.last_name.value(); API convenience: -- .get(), .value(), operator()() — access the underlying value (const and non-const overloads). +- .get(), .value(), operator()(), operator*() — access the underlying value (const and non-const overloads). - set(...) — assign underlying value. ## JSON behaviour diff --git a/docs/flatten_structs.md b/docs/flatten_structs.md index 8544f638..6eb6bd7d 100644 --- a/docs/flatten_structs.md +++ b/docs/flatten_structs.md @@ -68,3 +68,6 @@ This flattens all fields into a single JSON object: {"firstName":"Homer","lastName":"Simpson","age":45,"salary":60000.0} ``` +## API convenience + +`rfl::Flatten` behaves like a thin wrapper around the underlying type. You can access the underlying value using `.get()`, `.value()`, `operator()()`, or `operator*()` (const and non-const overloads). diff --git a/docs/generic.md b/docs/generic.md index 8af89a30..99005d1b 100644 --- a/docs/generic.md +++ b/docs/generic.md @@ -55,7 +55,7 @@ The resulting JSON strings looks as follows: `rfl::Generic` contains some convenience methods that allow you to handle parsed data: -You can retrieve the underlying `std::variant` using `.get()`. This then allows you to handle allow possible seven cases using the [visitor pattern](https://en.cppreference.com/w/cpp/utility/variant/visit). +You can retrieve the underlying `std::variant` using `.get()`, `.value()`, `operator()()`, or `operator*()`. This then allows you to handle all possible seven cases using the [visitor pattern](https://en.cppreference.com/w/cpp/utility/variant/visit). If you have a guess what the types of a particular field might be, you can use any of the seven convenience methods `.to_array()`, `.to_bool()`, `.to_double()`, `.to_int()`, `.to_null()`, `.to_object()`, `.to_string()`. Each of these methods will return an `rfl::Result<...>` with the corresponding type, if the `rfl::Generic` does indeed contain such a type. diff --git a/docs/json_schema.md b/docs/json_schema.md index f87e2e42..6332fd38 100644 --- a/docs/json_schema.md +++ b/docs/json_schema.md @@ -139,6 +139,8 @@ const std::string json_schema = rfl::json::to_schema(rfl::json::pretty); } ``` +`rfl::Description` behaves like a thin wrapper around the underlying type. Much like `rfl::Field`, you can access the underlying value using `.get()`, `.value()`, `operator()()`, or `operator*()` (const and non-const overloads). + You also add a description to the entire JSON schema: ```cpp diff --git a/docs/named_tuple.md b/docs/named_tuple.md index 7a90f2f1..64da742c 100644 --- a/docs/named_tuple.md +++ b/docs/named_tuple.md @@ -107,6 +107,10 @@ person.apply([](const auto& f) { auto field_name = f.name(); const auto& value = f.value(); }); + +### `rfl::Field` API convenience + +`rfl::Field` behaves like a thin wrapper around the underlying type. You can access the underlying value using `.get()`, `.value()`, `operator()()`, or `operator*()` (const and non-const overloads). ``` ### Monadic operations: `.transform` and `.and_then` diff --git a/docs/number_systems.md b/docs/number_systems.md index 89c58687..c3931052 100644 --- a/docs/number_systems.md +++ b/docs/number_systems.md @@ -25,6 +25,6 @@ This results in the following JSON: 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 access the contained value using `.value()`, `.get()`, `operator()()`, or `operator*()` (const and non-const overloads). You can produce the string representation using `.str()`. diff --git a/docs/rfl_skip.md b/docs/rfl_skip.md index 2e53bb5b..64515f88 100644 --- a/docs/rfl_skip.md +++ b/docs/rfl_skip.md @@ -50,6 +50,7 @@ You can access the underlying value in the field `town` using any of the followi person.town(); person.town.get(); person.town.value(); +*person.town; ``` You can assign the underlying field just like any other field: diff --git a/docs/supported_formats/xml.md b/docs/supported_formats/xml.md index 533f60f5..cf1ddf41 100644 --- a/docs/supported_formats/xml.md +++ b/docs/supported_formats/xml.md @@ -137,6 +137,8 @@ This will result in the following XML string: Note that only boolean values, string values, integral values or floating point values can be represented as attributes. +`rfl::Attribute` behaves like a thin wrapper around the underlying type. Much like `rfl::Field`, you can access the underlying value using `.get()`, `.value()`, `operator()()`, or `operator*()` (const and non-const overloads). + There also is a special field name called `xml_content` to be used when you want a value directly inserted into the content. Again, only boolean values, string values, integral values or floating point values can be represented this way: diff --git a/docs/timestamps.md b/docs/timestamps.md index 1dcae34a..26b73241 100644 --- a/docs/timestamps.md +++ b/docs/timestamps.md @@ -27,8 +27,7 @@ const auto person1 = Person{.birthday = "1970-01-01"}; const auto person2 = Person{.birthday = std::tm{...}}; ``` -You can access the underlying `std::tm` struct using the `.tm()` method and you can generate -the string representation using the `.str()` method. +You can access the underlying `std::tm` struct using the `.tm()`, `.get()`, `.value()`, `operator()()`, or `operator*()` method (const and non-const overloads). You can generate the string representation using the `.str()` method. ```cpp const std::tm birthday = person1.birthday.tm(); diff --git a/docs/validating_numbers.md b/docs/validating_numbers.md index dc99b904..c39c9d73 100644 --- a/docs/validating_numbers.md +++ b/docs/validating_numbers.md @@ -10,7 +10,7 @@ using IntGreaterThan10 = rfl::Validator>; When you then use the type `IntGreaterThan10` inside you `rfl::Field`, the condition will be automatically validated. -The underlying value can be retrieved using the `.value()` method. +The underlying value can be retrieved using the `.get()`, `.value()`, `operator()()`, or `operator*()` method (const and non-const overloads). The current conditions are currently supported by reflect-cpp: diff --git a/include/rfl/Attribute.hpp b/include/rfl/Attribute.hpp index 1af50846..37d3b315 100644 --- a/include/rfl/Attribute.hpp +++ b/include/rfl/Attribute.hpp @@ -53,13 +53,22 @@ struct Attribute { ~Attribute() = default; /// Returns the underlying object. - const Type& get() const { return value_; } + const Type& get() const noexcept { return value_; } /// Returns the underlying object. - Type& operator()() { return value_; } + Type& get() noexcept { return value_; } /// Returns the underlying object. - const Type& operator()() const { return value_; } + Type& operator*() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator*() const noexcept { return value_; } + + /// Returns the underlying object. + Type& operator()() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator()() const noexcept { return value_; } /// Assigns the underlying object. auto& operator=(const Type& _value) { @@ -120,10 +129,10 @@ struct Attribute { void set(Type&& _value) { value_ = std::move(_value); } /// Returns the underlying object. - Type& value() { return value_; } + Type& value() noexcept { return value_; } /// Returns the underlying object. - const Type& value() const { return value_; } + const Type& value() const noexcept { return value_; } /// The underlying value. Type value_; diff --git a/include/rfl/Binary.hpp b/include/rfl/Binary.hpp index 6c5550d4..f44bc5d5 100644 --- a/include/rfl/Binary.hpp +++ b/include/rfl/Binary.hpp @@ -54,13 +54,22 @@ struct Binary { ~Binary() = default; /// Returns the underlying object. - const Type& get() const { return value_; } + const Type& get() const noexcept { return value_; } /// Returns the underlying object. - Type& operator()() { return value_; } + Type& get() noexcept { return value_; } /// Returns the underlying object. - const Type& operator()() const { return value_; } + Type& operator*() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator*() const noexcept { return value_; } + + /// Returns the underlying object. + Type& operator()() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator()() const noexcept { return value_; } /// Assigns the underlying object. auto& operator=(const Type& _value) { @@ -113,10 +122,10 @@ struct Binary { std::string str() const { return reflection(); } /// Returns the underlying object. - Type& value() { return value_; } + Type& value() noexcept { return value_; } /// Returns the underlying object. - const Type& value() const { return value_; } + const Type& value() const noexcept { return value_; } /// The underlying value. Type value_; diff --git a/include/rfl/DefaultVal.hpp b/include/rfl/DefaultVal.hpp index db43ebe4..ca700851 100644 --- a/include/rfl/DefaultVal.hpp +++ b/include/rfl/DefaultVal.hpp @@ -51,13 +51,22 @@ struct DefaultVal { ~DefaultVal() = default; /// Returns the underlying object. - const Type& get() const { return value_; } + const Type& get() const noexcept { return value_; } /// Returns the underlying object. - Type& operator()() { return value_; } + Type& get() noexcept { return value_; } /// Returns the underlying object. - const Type& operator()() const { return value_; } + Type& operator*() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator*() const noexcept { return value_; } + + /// Returns the underlying object. + Type& operator()() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator()() const noexcept { return value_; } /// Assigns the underlying object. auto& operator=(const Type& _value) { @@ -114,10 +123,10 @@ struct DefaultVal { void set(Type&& _value) { value_ = std::move(_value); } /// Returns the underlying object. - Type& value() { return value_; } + Type& value() noexcept { return value_; } /// Returns the underlying object. - const Type& value() const { return value_; } + const Type& value() const noexcept { return value_; } /// The underlying value. Type value_; diff --git a/include/rfl/Description.hpp b/include/rfl/Description.hpp index cc33560e..1bdcbbc6 100644 --- a/include/rfl/Description.hpp +++ b/include/rfl/Description.hpp @@ -66,13 +66,22 @@ struct Description { constexpr static const internal::StringLiteral description_ = _description; /// Returns the underlying object. - const Type& get() const { return value_; } + const Type& get() const noexcept { return value_; } /// Returns the underlying object. - Type& operator()() { return value_; } + Type& get() noexcept { return value_; } /// Returns the underlying object. - const Type& operator()() const { return value_; } + Type& operator*() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator*() const noexcept { return value_; } + + /// Returns the underlying object. + Type& operator()() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator()() const noexcept { return value_; } /// Assigns the underlying object. auto& operator=(const Type& _value) { @@ -132,10 +141,10 @@ struct Description { void set(Type&& _value) { value_ = std::move(_value); } /// Returns the underlying object. - Type& value() { return value_; } + Type& value() noexcept { return value_; } /// Returns the underlying object. - const Type& value() const { return value_; } + const Type& value() const noexcept { return value_; } /// The underlying value. Type value_; diff --git a/include/rfl/Field.hpp b/include/rfl/Field.hpp index 2f436dad..9175fd1f 100644 --- a/include/rfl/Field.hpp +++ b/include/rfl/Field.hpp @@ -59,16 +59,25 @@ struct Field { constexpr static const internal::StringLiteral name_ = _name; /// Returns the underlying object. - const Type& get() const { return value_; } + const Type& get() const noexcept { return value_; } + + /// Returns the underlying object. + Type& get() noexcept { return value_; } + + /// Returns the underlying object. + Type& operator*() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator*() const noexcept { return value_; } /// The name of the field. constexpr static std::string_view name() { return name_.string_view(); } /// Returns the underlying object. - Type& operator()() { return value_; } + Type& operator()() noexcept { return value_; } /// Returns the underlying object. - const Type& operator()() const { return value_; } + const Type& operator()() const noexcept { return value_; } /// Assigns the underlying object. auto& operator=(const Type& _value) { @@ -125,10 +134,10 @@ struct Field { void set(Type&& _value) { value_ = std::move(_value); } /// Returns the underlying object. - Type& value() { return value_; } + Type& value() noexcept { return value_; } /// Returns the underlying object. - const Type& value() const { return value_; } + const Type& value() const noexcept { return value_; } /// The underlying value. Type value_; diff --git a/include/rfl/Flatten.hpp b/include/rfl/Flatten.hpp index 579cb7eb..90bec71b 100644 --- a/include/rfl/Flatten.hpp +++ b/include/rfl/Flatten.hpp @@ -42,16 +42,28 @@ struct Flatten { ~Flatten() = default; /// Returns the underlying object. - Type& get() { return value_; } + Type& get() noexcept { return value_; } /// Returns the underlying object. - const Type& get() const { return value_; } + const Type& get() const noexcept { return value_; } /// Returns the underlying object. - Type& operator()() { return value_; } + Type& operator*() noexcept { return value_; } /// Returns the underlying object. - const Type& operator()() const { return value_; } + const Type& operator*() const noexcept { return value_; } + + /// Returns the underlying object. + Type& operator()() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator()() const noexcept { return value_; } + + /// Returns the underlying object. + Type& value() noexcept { return value_; } + + /// Returns the underlying object. + const Type& value() const noexcept { return value_; } /// Assigns the underlying object. Flatten& operator=(const T& _value) { diff --git a/include/rfl/Generic.hpp b/include/rfl/Generic.hpp index 3b1f97a6..ad652deb 100644 --- a/include/rfl/Generic.hpp +++ b/include/rfl/Generic.hpp @@ -51,7 +51,28 @@ class RFL_API Generic { ~Generic(); /// Returns the underlying object. - const VariantType& get() const { return value_; } + const VariantType& get() const noexcept { return value_; } + + /// Returns the underlying object. + VariantType& get() noexcept { return value_; } + + /// Returns the underlying object. + VariantType& operator*() noexcept { return value_; } + + /// Returns the underlying object. + const VariantType& operator*() const noexcept { return value_; } + + /// Returns the underlying object. + VariantType& operator()() noexcept { return value_; } + + /// Returns the underlying object. + const VariantType& operator()() const noexcept { return value_; } + + /// Returns the underlying object. + VariantType& value() noexcept { return value_; } + + /// Returns the underlying object. + const VariantType& value() const noexcept { return value_; } /// Whether the object contains the null value. bool is_null() const noexcept; diff --git a/include/rfl/Hex.hpp b/include/rfl/Hex.hpp index 678697f2..fb8a48fe 100644 --- a/include/rfl/Hex.hpp +++ b/include/rfl/Hex.hpp @@ -52,13 +52,22 @@ struct Hex { ~Hex() = default; /// Returns the underlying object. - const Type& get() const { return value_; } + const Type& get() const noexcept { return value_; } /// Returns the underlying object. - Type& operator()() { return value_; } + Type& get() noexcept { return value_; } /// Returns the underlying object. - const Type& operator()() const { return value_; } + Type& operator*() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator*() const noexcept { return value_; } + + /// Returns the underlying object. + Type& operator()() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator()() const noexcept { return value_; } /// Assigns the underlying object. auto& operator=(const Type& _value) { @@ -115,10 +124,10 @@ struct Hex { std::string str() const { return reflection(); } /// Returns the underlying object. - Type& value() { return value_; } + Type& value() noexcept { return value_; } /// Returns the underlying object. - const Type& value() const { return value_; } + const Type& value() const noexcept { return value_; } /// The underlying value. Type value_; diff --git a/include/rfl/Oct.hpp b/include/rfl/Oct.hpp index cbe2467f..20e0c330 100644 --- a/include/rfl/Oct.hpp +++ b/include/rfl/Oct.hpp @@ -53,13 +53,22 @@ struct Oct { ~Oct() = default; /// Returns the underlying object. - const Type& get() const { return value_; } + const Type& get() const noexcept { return value_; } /// Returns the underlying object. - Type& operator()() { return value_; } + Type& get() noexcept { return value_; } /// Returns the underlying object. - const Type& operator()() const { return value_; } + Type& operator*() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator*() const noexcept { return value_; } + + /// Returns the underlying object. + Type& operator()() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator()() const noexcept { return value_; } /// Assigns the underlying object. auto& operator=(const Type& _value) { @@ -116,10 +125,10 @@ struct Oct { std::string str() const { return reflection(); } /// Returns the underlying object. - Type& value() { return value_; } + Type& value() noexcept { return value_; } /// Returns the underlying object. - const Type& value() const { return value_; } + const Type& value() const noexcept { return value_; } /// The underlying value. Type value_; diff --git a/include/rfl/Rename.hpp b/include/rfl/Rename.hpp index 99adc62a..c3bd79d9 100644 --- a/include/rfl/Rename.hpp +++ b/include/rfl/Rename.hpp @@ -62,13 +62,22 @@ struct Rename { constexpr static const internal::StringLiteral name_ = _name; /// Returns the underlying object. - const Type& get() const { return value_; } + const Type& get() const noexcept { return value_; } /// Returns the underlying object. - Type& operator()() { return value_; } + Type& get() noexcept { return value_; } /// Returns the underlying object. - const Type& operator()() const { return value_; } + Type& operator*() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator*() const noexcept { return value_; } + + /// Returns the underlying object. + Type& operator()() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator()() const noexcept { return value_; } /// Assigns the underlying object. auto& operator=(const Type& _value) { @@ -125,10 +134,10 @@ struct Rename { void set(Type&& _value) { value_ = std::move(_value); } /// Returns the underlying object. - Type& value() { return value_; } + Type& value() noexcept { return value_; } /// Returns the underlying object. - const Type& value() const { return value_; } + const Type& value() const noexcept { return value_; } /// The underlying value. Type value_; diff --git a/include/rfl/Timestamp.hpp b/include/rfl/Timestamp.hpp index 64468229..fe78e85f 100644 --- a/include/rfl/Timestamp.hpp +++ b/include/rfl/Timestamp.hpp @@ -71,6 +71,30 @@ class Timestamp { return from_string(_str); } + /// Returns the underlying object. + const std::tm& get() const noexcept { return tm_; } + + /// Returns the underlying object. + std::tm& get() noexcept { return tm_; } + + /// Returns the underlying object. + std::tm& operator*() noexcept { return tm_; } + + /// Returns the underlying object. + const std::tm& operator*() const noexcept { return tm_; } + + /// Returns the underlying object. + std::tm& operator()() noexcept { return tm_; } + + /// Returns the underlying object. + const std::tm& operator()() const noexcept { return tm_; } + + /// Returns the underlying object. + std::tm& value() noexcept { return tm_; } + + /// Returns the underlying object. + const std::tm& value() const noexcept { return tm_; } + /// Necessary for the serialization to work. ReflectionType reflection() const { char outstr[200]; diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp index cd7cd578..ffb5d08e 100644 --- a/include/rfl/Validator.hpp +++ b/include/rfl/Validator.hpp @@ -92,11 +92,29 @@ struct Validator { return value() == _other.value(); } + /// Returns the underlying object. + const T& get() const noexcept { return value_; } + + /// Returns the underlying object. + T& get() noexcept { return value_; } + + /// Returns the underlying object. + T& operator*() noexcept { return value_; } + + /// Returns the underlying object. + const T& operator*() const noexcept { return value_; } + + /// Returns the underlying object. + T& operator()() noexcept { return value_; } + + /// Returns the underlying object. + const T& operator()() const noexcept { return value_; } + /// Exposes the underlying value. - T& value() { return value_; } + T& value() noexcept { return value_; } /// Exposes the underlying value. - const T& value() const { return value_; } + const T& value() const noexcept { return value_; } /// Necessary for the serialization to work. const T& reflection() const { return value_; } diff --git a/include/rfl/internal/Skip.hpp b/include/rfl/internal/Skip.hpp index 843a167b..30cee133 100644 --- a/include/rfl/internal/Skip.hpp +++ b/include/rfl/internal/Skip.hpp @@ -61,16 +61,22 @@ class Skip { ~Skip() = default; /// Returns the underlying object. - Type& get() { return value_; } + Type& get() noexcept { return value_; } /// Returns the underlying object. - const Type& get() const { return value_; } + const Type& get() const noexcept { return value_; } /// Returns the underlying object. - Type& operator()() { return value_; } + Type& operator*() noexcept { return value_; } /// Returns the underlying object. - const Type& operator()() const { return value_; } + const Type& operator*() const noexcept { return value_; } + + /// Returns the underlying object. + Type& operator()() noexcept { return value_; } + + /// Returns the underlying object. + const Type& operator()() const noexcept { return value_; } /// Assigns the underlying object. auto& operator=(const Type& _value) { @@ -130,10 +136,10 @@ class Skip { void set(Type&& _value) { value_ = std::move(_value); } /// Returns the underlying object. - Type& value() { return value_; } + Type& value() noexcept { return value_; } /// Returns the underlying object. - const Type& value() const { return value_; } + const Type& value() const noexcept { return value_; } private: /// The underlying value