mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-05 10:16:19 +08:00
Added CBOR support; #53
This commit is contained in:
@@ -20,9 +20,10 @@ jobs:
|
||||
- name: Run test
|
||||
run: |
|
||||
g++ --version
|
||||
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build -j 4
|
||||
./build/tests/bson/reflect-cpp-bson-tests
|
||||
./build/tests/cbor/reflect-cpp-cbor-tests
|
||||
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
|
||||
./build/tests/json/reflect-cpp-json-tests
|
||||
./build/tests/xml/reflect-cpp-xml-tests
|
||||
@@ -52,9 +53,10 @@ jobs:
|
||||
CXX: clang++
|
||||
run: |
|
||||
clang --version
|
||||
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build -j 4
|
||||
./build/tests/bson/reflect-cpp-bson-tests
|
||||
./build/tests/cbor/reflect-cpp-cbor-tests
|
||||
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
|
||||
./build/tests/json/reflect-cpp-json-tests
|
||||
./build/tests/xml/reflect-cpp-xml-tests
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
|
||||
# Output files
|
||||
*.bson
|
||||
*.cbor
|
||||
*.json
|
||||
*.fb
|
||||
*.xml
|
||||
|
||||
+10
-1
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.15)
|
||||
|
||||
option(REFLECTCPP_BUILD_SHARED "Build shared library" OFF)
|
||||
option(REFLECTCPP_BSON "Enable BSON support" OFF)
|
||||
option(REFLECTCPP_CBOR "Enable CBOR support" OFF)
|
||||
option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" OFF)
|
||||
option(REFLECTCPP_XML "Enable XML support" OFF)
|
||||
option(REFLECTCPP_YAML "Enable YAML support" OFF)
|
||||
@@ -9,7 +10,7 @@ option(REFLECTCPP_YAML "Enable YAML support" OFF)
|
||||
option(REFLECTCPP_BUILD_TESTS "Build tests" OFF)
|
||||
|
||||
# enable vcpkg if require features other than JSON
|
||||
if (REFLECTCPP_BSON OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_YAML)
|
||||
if (REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_YAML)
|
||||
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file")
|
||||
endif ()
|
||||
|
||||
@@ -30,6 +31,14 @@ if (REFLECTCPP_BSON)
|
||||
target_link_libraries(reflectcpp PRIVATE $<IF:$<TARGET_EXISTS:mongo::bson_static>,mongo::bson_static,mongo::bson_shared>)
|
||||
endif ()
|
||||
|
||||
if (REFLECTCPP_CBOR)
|
||||
if (MSVC)
|
||||
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/tinycbor${CMAKE_STATIC_LIBRARY_SUFFIX}")
|
||||
else ()
|
||||
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/libtinycbor${CMAKE_STATIC_LIBRARY_SUFFIX}")
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (REFLECTCPP_FLEXBUFFERS)
|
||||
find_package(flatbuffers CONFIG REQUIRED)
|
||||
target_link_libraries(reflectcpp INTERFACE flatbuffers::flatbuffers)
|
||||
|
||||
@@ -32,10 +32,11 @@ The following table lists the serialization formats currently supported by refle
|
||||
| Format | Library | Version | License | Remarks |
|
||||
|--------------|------------------------------------------------------|--------------|------------| -----------------------------------------------------|
|
||||
| JSON | [yyjson](https://github.com/ibireme/yyjson) | >= 0.8.0 | MIT | out-of-the-box support, included in this repository |
|
||||
| BSON | [libbson](https://github.com/mongodb/libbson) | >= 1.25.1 | Apache 2.0 | |
|
||||
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | |
|
||||
| XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | |
|
||||
| YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | |
|
||||
| BSON | [libbson](https://github.com/mongodb/libbson) | >= 1.25.1 | Apache 2.0 | JSON-like binary format |
|
||||
| CBOR | [tinycbor](https://github.com/intel/tinycbor) | >= 0.6.0 | MIT | JSON-like binary format |
|
||||
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format |
|
||||
| XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | Textual format used in many legacy projects |
|
||||
| YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | Textual format with an emphasis on readability |
|
||||
|
||||
Support for more serialization formats is in development. Refer to the [issues](https://github.com/getml/reflect-cpp/issues) for details.
|
||||
|
||||
@@ -69,8 +70,7 @@ The resulting JSON string looks like this:
|
||||
{"first_name":"Homer","last_name":"Simpson","age":45}
|
||||
```
|
||||
|
||||
Or you can use another format, such as YAML. This will work for just about
|
||||
any example in the entire documentation or any supported format.
|
||||
Or you can use another format, such as YAML.
|
||||
|
||||
```cpp
|
||||
#include <rfl/yaml.hpp>
|
||||
@@ -89,6 +89,21 @@ last_name: Simpson
|
||||
age: 45
|
||||
```
|
||||
|
||||
This will work for just about any example in the entire documentation
|
||||
and any supported format:
|
||||
|
||||
```cpp
|
||||
rfl::bson::write(homer);
|
||||
rfl::cbor::write(homer);
|
||||
rfl::flexbuf::write(homer);
|
||||
rfl::xml::write(homer);
|
||||
|
||||
rfl::bson::read<Person>(bson_bytes);
|
||||
rfl::cbor::read<Person>(cbor_bytes);
|
||||
rfl::flexbuf::read<Person>(flexbuf_bytes);
|
||||
rfl::xml::read<Person>(xml_string);
|
||||
```
|
||||
|
||||
## More Comprehensive Example
|
||||
|
||||
```cpp
|
||||
@@ -450,6 +465,7 @@ To use reflect-cpp in your project:
|
||||
add_subdirectory(reflect-cpp) # Add this project as a subdirectory
|
||||
|
||||
set(REFLECTCPP_BSON ON) # Optional
|
||||
set(REFLECTCPP_CBOR ON) # Optional
|
||||
set(REFLECTCPP_FLEXBUFFERS ON) # Optional
|
||||
set(REFLECTCPP_XML ON) # Optional
|
||||
set(REFLECTCPP_YAML ON) # Optional
|
||||
@@ -496,7 +512,7 @@ git submodule update --init
|
||||
./vcpkg/bootstrap-vcpkg.bat # Windows
|
||||
# You may be prompted to install additional dependencies.
|
||||
|
||||
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build -j 4 # gcc, clang
|
||||
cmake --build build --config Release -j 4 # MSVC
|
||||
```
|
||||
@@ -504,6 +520,8 @@ cmake --build build --config Release -j 4 # MSVC
|
||||
To run the tests, do the following:
|
||||
|
||||
```
|
||||
./build/tests/bson/reflect-cpp-bson-tests
|
||||
./build/tests/cbor/reflect-cpp-cbor-tests
|
||||
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
|
||||
./build/tests/json/reflect-cpp-json-tests
|
||||
./build/tests/xml/reflect-cpp-xml-tests
|
||||
|
||||
+5
-3
@@ -58,11 +58,13 @@
|
||||
|
||||
5.2) [BSON](https://github.com/getml/reflect-cpp/blob/main/docs/bson.md)
|
||||
|
||||
5.3) [flexbuffers](https://github.com/getml/reflect-cpp/blob/main/docs/flexbuffers.md)
|
||||
5.3) [CBOR](https://github.com/getml/reflect-cpp/blob/main/docs/cbor.md)
|
||||
|
||||
5.4) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md)
|
||||
5.4) [flexbuffers](https://github.com/getml/reflect-cpp/blob/main/docs/flexbuffers.md)
|
||||
|
||||
5.5) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md)
|
||||
5.5) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md)
|
||||
|
||||
5.6) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md)
|
||||
|
||||
## 6) Advanced topics
|
||||
|
||||
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
# CBOR
|
||||
|
||||
For CBOR support, you must also include the header `<rfl/cbor.hpp>` and link to the tinycbor library (https://github.com/intel/tinycbor).
|
||||
|
||||
CBOR or Concise Binary Object Representation, is a JSON-like binary format with an emphasis on small binary sizes.
|
||||
|
||||
## Reading and writing
|
||||
|
||||
Suppose you have a struct like this:
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
```
|
||||
|
||||
A `Person` can be turned into a bytes vector like this:
|
||||
|
||||
```cpp
|
||||
const auto person = Person{...};
|
||||
const std::vector<char> bytes = rfl::cbor::write(person);
|
||||
```
|
||||
|
||||
You can parse bytes like this:
|
||||
|
||||
```cpp
|
||||
const rfl::Result<Person> result = rfl::cbor::read<Person>(bytes);
|
||||
```
|
||||
|
||||
## Loading and saving
|
||||
|
||||
You can also load and save to disc using a very similar syntax:
|
||||
|
||||
```cpp
|
||||
const rfl::Result<Person> result = rfl::cbor::load<Person>("/path/to/file.cbor");
|
||||
|
||||
const auto person = Person{...};
|
||||
rfl::cbor::save("/path/to/file.cbor", person);
|
||||
```
|
||||
|
||||
## Reading from and writing into streams
|
||||
|
||||
You can also read from and write into any `std::istream` and `std::ostream` respectively.
|
||||
|
||||
```cpp
|
||||
const rfl::Result<Person> result = rfl::cbor::read<Person>(my_istream);
|
||||
|
||||
const auto person = Person{...};
|
||||
rfl::cbor::write(person, my_ostream);
|
||||
```
|
||||
|
||||
Note that `std::cout` is also an ostream, so this works as well:
|
||||
|
||||
```cpp
|
||||
rfl::cbor::write(person, std::cout) << std::endl;
|
||||
```
|
||||
|
||||
(Since CBOR is a binary format, the readability of this will be limited, but it might be useful for debugging).
|
||||
|
||||
## Custom constructors
|
||||
|
||||
One of the great things about C++ is that it gives you control over
|
||||
when and how you code is compiled.
|
||||
|
||||
For large and complex systems of structs, it is often a good idea to split up
|
||||
your code into smaller compilation units. You can do so using custom constructors.
|
||||
|
||||
For the CBOR format, these must be a static function on your struct or class called
|
||||
`from_cbor` that take a `rfl::cbor::Reader::InputVarType` as input and return
|
||||
the class or the class wrapped in `rfl::Result`.
|
||||
|
||||
In your header file you can write something like this:
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
|
||||
using InputVarType = typename rfl::cbor::Reader::InputVarType;
|
||||
static rfl::Result<Person> from_cbor(const InputVarType& _obj);
|
||||
};
|
||||
```
|
||||
|
||||
And in your source file, you implement `from_cbor` as follows:
|
||||
|
||||
```cpp
|
||||
rfl::Result<Person> Person::from_cbor(const InputVarType& _obj) {
|
||||
const auto from_nt = [](auto&& _nt) {
|
||||
return rfl::from_named_tuple<Person>(std::move(_nt));
|
||||
};
|
||||
return rfl::cbor::read<rfl::named_tuple_t<Person>>(_obj)
|
||||
.transform(from_nt);
|
||||
}
|
||||
```
|
||||
|
||||
This will force the compiler to only compile the CBOR parsing when the source file is compiled.
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef RFL_CBOR_HPP_
|
||||
#define RFL_CBOR_HPP_
|
||||
|
||||
#include "cbor/Parser.hpp"
|
||||
#include "cbor/Reader.hpp"
|
||||
#include "cbor/Writer.hpp"
|
||||
#include "cbor/load.hpp"
|
||||
#include "cbor/read.hpp"
|
||||
#include "cbor/save.hpp"
|
||||
#include "cbor/write.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef RFL_CBOR_PARSER_HPP_
|
||||
#define RFL_CBOR_PARSER_HPP_
|
||||
|
||||
#include "../parsing/Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
/// CBOR requires us to explicitly set the number of fields in advance. Because
|
||||
/// of that, we require all of the fields and then set them to nullptr, if
|
||||
/// necessary.
|
||||
template <class... FieldTypes>
|
||||
requires AreReaderAndWriter<cbor::Reader, cbor::Writer,
|
||||
NamedTuple<FieldTypes...>>
|
||||
struct Parser<cbor::Reader, cbor::Writer, NamedTuple<FieldTypes...>>
|
||||
: public NamedTupleParser<cbor::Reader, cbor::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true, FieldTypes...> {
|
||||
};
|
||||
|
||||
template <class... Ts>
|
||||
requires AreReaderAndWriter<cbor::Reader, cbor::Writer, std::tuple<Ts...>>
|
||||
struct Parser<cbor::Reader, cbor::Writer, std::tuple<Ts...>>
|
||||
: public TupleParser<cbor::Reader, cbor::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true, Ts...> {
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
|
||||
template <class T>
|
||||
using Parser = parsing::Parser<Reader, Writer, T>;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,298 @@
|
||||
#ifndef RFL_CBOR_READER_HPP_
|
||||
#define RFL_CBOR_READER_HPP_
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <array>
|
||||
#include <concepts>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <source_location>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "../Box.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
|
||||
/// Please refer to https://intel.github.io/tinycbor/current/index.html
|
||||
struct Reader {
|
||||
struct CBORInputArray {
|
||||
CborValue* val_;
|
||||
};
|
||||
|
||||
struct CBORInputObject {
|
||||
CborValue* val_;
|
||||
};
|
||||
|
||||
struct CBORInputVar {
|
||||
CborValue* val_;
|
||||
};
|
||||
|
||||
using InputArrayType = CBORInputArray;
|
||||
using InputObjectType = CBORInputObject;
|
||||
using InputVarType = CBORInputVar;
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_cbor_obj(var);
|
||||
});
|
||||
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
CborValue val;
|
||||
auto buffer = std::vector<char>();
|
||||
auto err = cbor_value_enter_container(_obj.val_, &val);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
size_t length = 0;
|
||||
err = cbor_value_get_map_length(_obj.val_, &length);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (!cbor_value_is_text_string(&val)) {
|
||||
return Error("Expected the key to be a string value.");
|
||||
}
|
||||
err = get_string(&val, &buffer);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
if (_name == buffer.data()) {
|
||||
return to_input_var(&val);
|
||||
}
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
}
|
||||
return Error("No field named '" + _name + "' was found.");
|
||||
}
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return cbor_value_is_null(_var.val_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
if (!cbor_value_is_text_string(_var.val_)) {
|
||||
return Error("Could not cast to string.");
|
||||
}
|
||||
std::vector<char> buffer;
|
||||
const auto err = get_string(_var.val_, &buffer);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
return std::string(buffer.data());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!cbor_value_is_boolean(_var.val_)) {
|
||||
return rfl::Error("Could not cast to boolean.");
|
||||
}
|
||||
bool result = false;
|
||||
const auto err = cbor_value_get_boolean(_var.val_, &result);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
return result;
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (cbor_value_is_integer(_var.val_)) {
|
||||
std::int64_t result = 0;
|
||||
const auto err = cbor_value_get_int64(_var.val_, &result);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
return static_cast<T>(result);
|
||||
} else if (cbor_value_is_float(_var.val_)) {
|
||||
float result = 0.0;
|
||||
const auto err = cbor_value_get_float(_var.val_, &result);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
return static_cast<T>(result);
|
||||
} else if (cbor_value_is_double(_var.val_)) {
|
||||
double result = 0.0;
|
||||
const auto err = cbor_value_get_double(_var.val_, &result);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
return static_cast<T>(result);
|
||||
}
|
||||
return rfl::Error(
|
||||
"Could not cast to numeric value. The type must be integral, float "
|
||||
"or double.");
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!cbor_value_is_array(_var.val_)) {
|
||||
return Error("Could not cast to an array.");
|
||||
}
|
||||
return InputArrayType{_var.val_};
|
||||
}
|
||||
|
||||
template <size_t size, class FunctionType>
|
||||
std::array<std::optional<InputVarType>, size> to_fields_array(
|
||||
const FunctionType& _fct, const InputObjectType& _obj) const noexcept {
|
||||
std::array<std::optional<InputVarType>, size> f_arr;
|
||||
CborValue val;
|
||||
auto buffer = std::vector<char>();
|
||||
auto err = cbor_value_enter_container(_obj.val_, &val);
|
||||
if (err != CborNoError) {
|
||||
return f_arr;
|
||||
}
|
||||
size_t length = 0;
|
||||
err = cbor_value_get_map_length(_obj.val_, &length);
|
||||
if (err != CborNoError) {
|
||||
return f_arr;
|
||||
}
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (!cbor_value_is_text_string(&val)) {
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError) {
|
||||
return f_arr;
|
||||
}
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError) {
|
||||
return f_arr;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
err = get_string(&val, &buffer);
|
||||
if (err != CborNoError) {
|
||||
return f_arr;
|
||||
}
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError) {
|
||||
return f_arr;
|
||||
}
|
||||
const auto ix = _fct(std::string_view(buffer.data()));
|
||||
if (ix != -1) {
|
||||
f_arr[ix] = to_input_var(&val);
|
||||
}
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError) {
|
||||
return f_arr;
|
||||
}
|
||||
}
|
||||
return f_arr;
|
||||
}
|
||||
|
||||
rfl::Result<std::vector<std::pair<std::string, InputVarType>>> to_map(
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
std::vector<std::pair<std::string, InputVarType>> map;
|
||||
CborValue val;
|
||||
auto buffer = std::vector<char>();
|
||||
auto err = cbor_value_enter_container(_obj.val_, &val);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
size_t length = 0;
|
||||
cbor_value_get_map_length(_obj.val_, &length);
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (!cbor_value_is_text_string(&val)) {
|
||||
return Error("Expected the key to be a string value.");
|
||||
}
|
||||
err = get_string(&val, &buffer);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
map.emplace_back(
|
||||
std::make_pair(std::string(buffer.data()), to_input_var(&val)));
|
||||
cbor_value_advance(&val);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!cbor_value_is_map(_var.val_)) {
|
||||
return Error("Could not cast to an object.");
|
||||
}
|
||||
return InputObjectType{_var.val_};
|
||||
}
|
||||
|
||||
std::vector<InputVarType> to_vec(const InputArrayType& _arr) const noexcept {
|
||||
std::vector<InputVarType> vec;
|
||||
CborValue val;
|
||||
auto buffer = std::vector<char>();
|
||||
auto err = cbor_value_enter_container(_arr.val_, &val);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return vec;
|
||||
}
|
||||
size_t length = 0;
|
||||
err = cbor_value_get_array_length(_arr.val_, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return vec;
|
||||
}
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
vec.emplace_back(to_input_var(&val));
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return vec;
|
||||
}
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
const InputVarType& _var) const noexcept {
|
||||
try {
|
||||
return T::from_cbor_obj(_var);
|
||||
} catch (std::exception& e) {
|
||||
return rfl::Error(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
CborError get_string(const CborValue* _ptr,
|
||||
std::vector<char>* _buffer) const noexcept {
|
||||
size_t length = 0;
|
||||
auto err = cbor_value_get_string_length(_ptr, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return err;
|
||||
}
|
||||
_buffer->resize(length + 1);
|
||||
(*_buffer)[length] = '\0';
|
||||
return cbor_value_copy_text_string(_ptr, _buffer->data(), &length, NULL);
|
||||
}
|
||||
|
||||
InputVarType to_input_var(CborValue* _ptr) const noexcept {
|
||||
values_->emplace_back(rfl::Box<CborValue>::make(*_ptr));
|
||||
auto* last_value = values_->back().get();
|
||||
return InputVarType{last_value};
|
||||
}
|
||||
|
||||
private:
|
||||
/// Contains the values inside the object.
|
||||
rfl::Box<std::vector<rfl::Box<CborValue>>> values_;
|
||||
};
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
@@ -0,0 +1,163 @@
|
||||
#ifndef RFL_CBOR_WRITER_HPP_
|
||||
#define RFL_CBOR_WRITER_HPP_
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "../Box.hpp"
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
|
||||
class Writer {
|
||||
public:
|
||||
struct CBOROutputArray {
|
||||
CborEncoder* encoder_;
|
||||
CborEncoder* parent_;
|
||||
};
|
||||
|
||||
struct CBOROutputObject {
|
||||
CborEncoder* encoder_;
|
||||
CborEncoder* parent_;
|
||||
};
|
||||
|
||||
struct CBOROutputVar {};
|
||||
|
||||
using OutputArrayType = CBOROutputArray;
|
||||
using OutputObjectType = CBOROutputObject;
|
||||
using OutputVarType = CBOROutputVar;
|
||||
|
||||
Writer(CborEncoder* _encoder) : encoder_(_encoder) {}
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array(_size, encoder_);
|
||||
}
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object(_size, encoder_);
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
cbor_encode_null(encoder_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return new_value(_var, encoder_);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.c_str(), _name.size());
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.c_str(), _name.size());
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_value(_var, _parent->encoder_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string& _name, const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.c_str(), _name.size());
|
||||
return new_value(_var, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
cbor_encode_null(_parent->encoder_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(const std::string& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.c_str(), _name.size());
|
||||
cbor_encode_null(_parent->encoder_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
cbor_encoder_close_container(_arr->parent_, _arr->encoder_);
|
||||
}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
cbor_encoder_close_container(_obj->parent_, _obj->encoder_);
|
||||
}
|
||||
|
||||
private:
|
||||
OutputArrayType new_array(const size_t _size,
|
||||
CborEncoder* _parent) const noexcept {
|
||||
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
|
||||
cbor_encoder_create_array(_parent, subencoders_->back().get(), _size);
|
||||
return OutputArrayType{subencoders_->back().get(), _parent};
|
||||
}
|
||||
|
||||
OutputObjectType new_object(const size_t _size,
|
||||
CborEncoder* _parent) const noexcept {
|
||||
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
|
||||
cbor_encoder_create_map(_parent, subencoders_->back().get(), _size);
|
||||
return OutputObjectType{subencoders_->back().get(), _parent};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType new_value(const T& _var, CborEncoder* _parent) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
cbor_encode_text_string(_parent, _var.c_str(), _var.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
cbor_encode_boolean(_parent, _var);
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
cbor_encode_double(_parent, static_cast<double>(_var));
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
cbor_encode_int(_parent, static_cast<std::int64_t>(_var));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
private:
|
||||
/// The underlying TinyCBOR encoder.
|
||||
CborEncoder* const encoder_;
|
||||
|
||||
/// Contain all of the subobjects and subarrays.
|
||||
const rfl::Box<std::vector<rfl::Box<CborEncoder>>> subencoders_;
|
||||
};
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif // CBOR_PARSER_HPP_
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef RFL_CBOR_LOAD_HPP_
|
||||
#define RFL_CBOR_LOAD_HPP_
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../io/load_bytes.hpp"
|
||||
#include "read.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
|
||||
template <class T>
|
||||
Result<T> load(const std::string& _fname) {
|
||||
const auto read_bytes = [](const auto& _bytes) { return read<T>(_bytes); };
|
||||
return rfl::io::load_bytes(_fname).and_then(read_bytes);
|
||||
}
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,56 @@
|
||||
#ifndef RFL_CBOR_READ_HPP_
|
||||
#define RFL_CBOR_READ_HPP_
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
|
||||
using InputObjectType = typename Reader::InputObjectType;
|
||||
using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a CBOR var.
|
||||
template <class T>
|
||||
auto read(const InputVarType& _obj) {
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _obj);
|
||||
}
|
||||
|
||||
/// Parses an object from CBOR using reflection.
|
||||
template <class T>
|
||||
Result<internal::wrap_in_rfl_array_t<T>> read(const char* _bytes,
|
||||
const size_t _size) {
|
||||
CborParser parser;
|
||||
CborValue value;
|
||||
cbor_parser_init(reinterpret_cast<const uint8_t*>(_bytes), _size, 0, &parser,
|
||||
&value);
|
||||
auto doc = InputVarType{&value};
|
||||
auto result = read<T>(doc);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Parses an object from CBOR using reflection.
|
||||
template <class T>
|
||||
auto read(const std::vector<char>& _bytes) {
|
||||
return read<T>(_bytes.data(), _bytes.size());
|
||||
}
|
||||
|
||||
/// Parses an object from a stream.
|
||||
template <class T>
|
||||
auto read(std::istream& _stream) {
|
||||
std::istreambuf_iterator<char> begin(_stream), end;
|
||||
auto bytes = std::vector<char>(begin, end);
|
||||
return read<T>(bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef RFL_CBOR_SAVE_HPP_
|
||||
#define RFL_CBOR_SAVE_HPP_
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../io/save_bytes.hpp"
|
||||
#include "write.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
|
||||
template <class T>
|
||||
Result<Nothing> save(const std::string& _fname, const T& _obj) {
|
||||
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write(_obj, _stream);
|
||||
};
|
||||
return rfl::io::save_bytes(_fname, _obj, write_func);
|
||||
}
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,57 @@
|
||||
#ifndef RFL_CBOR_WRITE_HPP_
|
||||
#define RFL_CBOR_WRITE_HPP_
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
|
||||
template <class T>
|
||||
void write_into_buffer(const T& _obj, CborEncoder* _encoder,
|
||||
std::vector<char>* _buffer) noexcept {
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
cbor_encoder_init(_encoder, reinterpret_cast<uint8_t*>(_buffer->data()),
|
||||
_buffer->size(), 0);
|
||||
const auto writer = Writer(_encoder);
|
||||
Parser<T>::write(writer, _obj, typename ParentType::Root{});
|
||||
}
|
||||
|
||||
/// Returns CBOR bytes.
|
||||
template <class T>
|
||||
std::vector<char> write(const T& _obj) noexcept {
|
||||
std::vector<char> buffer(4096);
|
||||
CborEncoder encoder;
|
||||
write_into_buffer(_obj, &encoder, &buffer);
|
||||
const auto total_bytes_needed =
|
||||
buffer.size() + cbor_encoder_get_extra_bytes_needed(&encoder);
|
||||
if (total_bytes_needed != buffer.size()) {
|
||||
buffer.resize(total_bytes_needed);
|
||||
write_into_buffer(_obj, &encoder, &buffer);
|
||||
}
|
||||
const auto length = cbor_encoder_get_buffer_size(
|
||||
&encoder, reinterpret_cast<uint8_t*>(buffer.data()));
|
||||
buffer.resize(length);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// Writes a CBOR into an ostream.
|
||||
template <class T>
|
||||
std::ostream& write(const T& _obj, std::ostream& _stream) noexcept {
|
||||
auto buffer = write(_obj);
|
||||
_stream.write(buffer.data(), buffer.size());
|
||||
return _stream;
|
||||
}
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -21,7 +21,8 @@
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class W, bool _ignore_empty_containers, class... FieldTypes>
|
||||
template <class R, class W, bool _ignore_empty_containers, bool _all_required,
|
||||
class... FieldTypes>
|
||||
requires AreReaderAndWriter<R, W, NamedTuple<FieldTypes...>>
|
||||
struct NamedTupleParser {
|
||||
using InputObjectType = typename R::InputObjectType;
|
||||
@@ -78,7 +79,8 @@ struct NamedTupleParser {
|
||||
const auto& f = std::get<i>(_fields_arr);
|
||||
|
||||
if (!f) {
|
||||
if constexpr (is_required<ValueType, _ignore_empty_containers>()) {
|
||||
if constexpr (_all_required ||
|
||||
is_required<ValueType, _ignore_empty_containers>()) {
|
||||
const auto key = FieldType::name_.str();
|
||||
return collect_errors<i + 1>(
|
||||
_r, _fields_arr,
|
||||
@@ -134,7 +136,8 @@ struct NamedTupleParser {
|
||||
const auto& f = std::get<_i>(_fields_arr);
|
||||
|
||||
if (!f) {
|
||||
if constexpr (is_required<ValueType, _ignore_empty_containers>()) {
|
||||
if constexpr (_all_required ||
|
||||
is_required<ValueType, _ignore_empty_containers>()) {
|
||||
const auto key = FieldType::name_.str();
|
||||
_errors.emplace_back(Error("Field named '" + key + "' not found."));
|
||||
}
|
||||
@@ -167,7 +170,8 @@ struct NamedTupleParser {
|
||||
const auto& value = rfl::get<_i>(_tup);
|
||||
const auto name = FieldType::name_.str();
|
||||
const auto new_parent = typename ParentType::Object{name, _ptr};
|
||||
if constexpr (!is_required<ValueType, _ignore_empty_containers>()) {
|
||||
if constexpr (!_all_required &&
|
||||
!is_required<ValueType, _ignore_empty_containers>()) {
|
||||
if (!is_empty(value)) {
|
||||
if constexpr (internal::is_attribute_v<ValueType>) {
|
||||
Parser<R, W, ValueType>::write(_w, value,
|
||||
|
||||
@@ -11,7 +11,7 @@ template <class R, class W, class... FieldTypes>
|
||||
requires AreReaderAndWriter<R, W, NamedTuple<FieldTypes...>>
|
||||
struct Parser<R, W, NamedTuple<FieldTypes...>>
|
||||
: public NamedTupleParser<R, W, /*_ignore_empty_containers=*/false,
|
||||
FieldTypes...> {
|
||||
/*_all_required=*/false, FieldTypes...> {
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -10,7 +10,8 @@ namespace parsing {
|
||||
template <class R, class W, class... Ts>
|
||||
requires AreReaderAndWriter<R, W, std::tuple<Ts...>>
|
||||
struct Parser<R, W, std::tuple<Ts...>>
|
||||
: public TupleParser<R, W, /*_ignore_empty_containers=*/false, Ts...> {
|
||||
: public TupleParser<R, W, /*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/false, Ts...> {
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -11,8 +11,9 @@
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class W, bool _ignore_empty_containers, class... Ts>
|
||||
requires AreReaderAndWriter<R, W, std::tuple<Ts...>>
|
||||
template <class R, class W, bool _ignore_empty_containers, bool _all_required,
|
||||
class... Ts>
|
||||
requires AreReaderAndWriter<R, W, std::tuple<Ts...>>
|
||||
struct TupleParser {
|
||||
public:
|
||||
using InputArrayType = typename R::InputArrayType;
|
||||
@@ -82,7 +83,8 @@ struct TupleParser {
|
||||
using ResultType = Result<NewFieldType>;
|
||||
|
||||
if (_i >= _vec.size()) {
|
||||
if constexpr (is_required<NewFieldType, _ignore_empty_containers>()) {
|
||||
if constexpr (_all_required ||
|
||||
is_required<NewFieldType, _ignore_empty_containers>()) {
|
||||
return ResultType(Error("Array is of length " +
|
||||
std::to_string(_vec.size()) + ", but field " +
|
||||
std::to_string(_i + 1) + " is required."));
|
||||
|
||||
@@ -20,7 +20,7 @@ requires AreReaderAndWriter<xml::Reader, xml::Writer, NamedTuple<FieldTypes...>>
|
||||
struct Parser<xml::Reader, xml::Writer, NamedTuple<FieldTypes...>>
|
||||
: public NamedTupleParser<xml::Reader, xml::Writer,
|
||||
/*_ignore_empty_containers=*/true,
|
||||
FieldTypes...> {
|
||||
/*_all_required=*/false, FieldTypes...> {
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -12,6 +12,10 @@ if (REFLECTCPP_BSON)
|
||||
add_subdirectory(bson)
|
||||
endif()
|
||||
|
||||
if (REFLECTCPP_CBOR)
|
||||
add_subdirectory(cbor)
|
||||
endif()
|
||||
|
||||
if (REFLECTCPP_FLEXBUFFERS)
|
||||
add_subdirectory(flexbuffers)
|
||||
endif()
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
project(reflect-cpp-cbor-tests)
|
||||
|
||||
file(GLOB_RECURSE SOURCES "*.cpp")
|
||||
|
||||
add_executable(reflect-cpp-cbor-tests ${SOURCES})
|
||||
|
||||
target_include_directories(reflect-cpp-cbor-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/tinycbor")
|
||||
target_link_libraries(reflect-cpp-cbor-tests PRIVATE reflectcpp)
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "test_array.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <rfl/cbor.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
// Make sure things still compile when
|
||||
// rfl.hpp is included after rfl/cbor.hpp.
|
||||
#include <rfl.hpp>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_array {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::array<Person, 3>> children = nullptr;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto bart = Person{.first_name = "Bart"};
|
||||
|
||||
auto lisa = Person{.first_name = "Lisa"};
|
||||
|
||||
auto maggie = Person{.first_name = "Maggie"};
|
||||
|
||||
const auto homer = Person{
|
||||
.first_name = "Homer",
|
||||
.children = std::make_unique<std::array<Person, 3>>(std::array<Person, 3>{
|
||||
std::move(bart), std::move(lisa), std::move(maggie)})};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_array
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_array{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "test_box.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_box {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
using Tag = rfl::Literal<"Leaf">;
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
using Tag = rfl::Literal<"Node">;
|
||||
rfl::Rename<"criticalValue", double> critical_value;
|
||||
rfl::Box<DecisionTree> lesser;
|
||||
rfl::Box<DecisionTree> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
|
||||
auto node = DecisionTree::Node{
|
||||
.critical_value = 10.0,
|
||||
.lesser = rfl::make_box<DecisionTree>(DecisionTree{leaf1}),
|
||||
.greater = rfl::make_box<DecisionTree>(DecisionTree{leaf2})};
|
||||
|
||||
const DecisionTree tree{.leaf_or_node = std::move(node)};
|
||||
|
||||
write_and_read(tree);
|
||||
|
||||
}
|
||||
} // namespace test_box
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_box{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "test_custom_class1.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_custom_class1 {
|
||||
|
||||
struct Person {
|
||||
struct PersonImpl {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
using ReflectionType = PersonImpl;
|
||||
|
||||
Person(const PersonImpl& _impl) : impl(_impl) {}
|
||||
|
||||
Person(const std::string& _first_name)
|
||||
: impl(PersonImpl{.first_name = _first_name}) {}
|
||||
|
||||
const ReflectionType& reflection() const { return impl; };
|
||||
|
||||
private:
|
||||
PersonImpl impl;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person("Bart");
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_custom_class1
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_custom_class1{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
#include "test_custom_class3.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_custom_class3 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name, const std::string& _last_name,
|
||||
const int _age)
|
||||
: first_name_(_first_name), last_name_(_last_name), age_(_age) {}
|
||||
|
||||
const auto& first_name() const { return first_name_; }
|
||||
|
||||
const auto& last_name() const { return last_name_; }
|
||||
|
||||
auto age() const { return age_; }
|
||||
|
||||
private:
|
||||
std::string first_name_;
|
||||
std::string last_name_;
|
||||
int age_;
|
||||
};
|
||||
|
||||
struct PersonImpl {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
int age;
|
||||
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{.first_name = _p.first_name(),
|
||||
.last_name = _p.last_name(),
|
||||
.age = _p.age()};
|
||||
}
|
||||
|
||||
Person to_class() const { return Person(first_name(), last_name(), age); }
|
||||
};
|
||||
} // namespace test_custom_class3
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct Parser<ReaderType, WriterType, test_custom_class3::Person>
|
||||
: public CustomParser<ReaderType, WriterType, test_custom_class3::Person,
|
||||
test_custom_class3::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
namespace test_custom_class3 {
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person("Bart", "Simpson", 10);
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
|
||||
} // namespace test_custom_class3
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_custom_class3{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
#include "test_custom_class4.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_custom_class4 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name,
|
||||
const rfl::Box<std::string>& _last_name, int _age)
|
||||
: first_name_(_first_name),
|
||||
last_name_(rfl::make_box<std::string>(*_last_name)),
|
||||
age_(_age) {}
|
||||
|
||||
const auto& first_name() const { return first_name_; }
|
||||
|
||||
const auto& last_name() const { return last_name_; }
|
||||
|
||||
auto age() const { return age_; }
|
||||
|
||||
private:
|
||||
std::string first_name_;
|
||||
rfl::Box<std::string> last_name_;
|
||||
int age_;
|
||||
};
|
||||
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{.first_name = _p.first_name(),
|
||||
.last_name = rfl::make_box<std::string>(*_p.last_name()),
|
||||
.age = _p.age()};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace test_custom_class4
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct Parser<ReaderType, WriterType, test_custom_class4::Person>
|
||||
: public CustomParser<ReaderType, WriterType, test_custom_class4::Person,
|
||||
test_custom_class4::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
namespace test_custom_class4 {
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = test_custom_class4::Person(
|
||||
"Bart", rfl::make_box<std::string>("Simpson"), 10);
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_custom_class4
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_custom_class4{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "test_default_values.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_default_values {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = "Bart"};
|
||||
const auto lisa = Person{.first_name = "Lisa"};
|
||||
const auto maggie = Person{.first_name = "Maggie"};
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_default_values
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_default_values{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "test_deque.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_deque {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::deque<Person>> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto children = std::make_unique<std::deque<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
|
||||
}
|
||||
} // namespace test_deque
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_deque{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "test_enum.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_enum {
|
||||
|
||||
enum class Color { red, green, blue, yellow };
|
||||
|
||||
struct Circle {
|
||||
float radius;
|
||||
Color color;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto circle = Circle{.radius = 2.0, .color = Color::green};
|
||||
|
||||
write_and_read(circle);
|
||||
}
|
||||
|
||||
} // namespace test_enum
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_enum {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "test_field_variant.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_field_variant {
|
||||
|
||||
struct Circle {
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
double width;
|
||||
};
|
||||
|
||||
using Shapes = rfl::Variant<rfl::Field<"circle", Circle>,
|
||||
rfl::Field<"rectangle", Rectangle>,
|
||||
rfl::Field<"square", rfl::Box<Square>>>;
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const Shapes r =
|
||||
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
|
||||
|
||||
write_and_read(r);
|
||||
}
|
||||
} // namespace test_field_variant
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_field_variant{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "test_enum.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flag_enum {
|
||||
|
||||
enum class Color {
|
||||
red = 256,
|
||||
green = 512,
|
||||
blue = 1024,
|
||||
yellow = 2048,
|
||||
orange = (256 | 2048) // red + yellow = orange
|
||||
};
|
||||
|
||||
inline Color operator|(Color c1, Color c2) {
|
||||
return static_cast<Color>(static_cast<int>(c1) | static_cast<int>(c2));
|
||||
}
|
||||
|
||||
struct Circle {
|
||||
float radius;
|
||||
Color color;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto circle =
|
||||
Circle{.radius = 2.0, .color = Color::blue | Color::orange};
|
||||
|
||||
write_and_read(circle);
|
||||
}
|
||||
|
||||
} // namespace test_flag_enum
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_flag_enum {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "test_enum.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flag_enum_with_int {
|
||||
|
||||
enum class Color {
|
||||
red = 256,
|
||||
green = 512,
|
||||
blue = 1024,
|
||||
yellow = 2048,
|
||||
orange = (256 | 2048) // red + yellow = orange
|
||||
};
|
||||
|
||||
inline Color operator|(Color c1, Color c2) {
|
||||
return static_cast<Color>(static_cast<int>(c1) | static_cast<int>(c2));
|
||||
}
|
||||
|
||||
struct Circle {
|
||||
float radius;
|
||||
Color color;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto circle = Circle{.radius = 2.0, .color = static_cast<Color>(10000)};
|
||||
|
||||
write_and_read(circle);
|
||||
}
|
||||
|
||||
} // namespace test_flag_enum_with_int
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_flag_enum_with_int {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#include "test_flatten.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flatten {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
};
|
||||
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Field<"employer", rfl::Box<std::string>> employer;
|
||||
rfl::Field<"salary", float> salary;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto employee = Employee{
|
||||
.person = Person{.first_name = "Homer",
|
||||
.last_name = rfl::make_box<std::string>("Simpson"),
|
||||
.age = 45},
|
||||
.employer = rfl::make_box<std::string>("Mr. Burns"),
|
||||
.salary = 60000.0};
|
||||
|
||||
write_and_read(employee);
|
||||
}
|
||||
} // namespace test_flatten
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_flatten{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "test_flatten_anonymous.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flatten_anonymous {
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
rfl::Box<std::string> last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Box<std::string> employer;
|
||||
float salary;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto employee = Employee{
|
||||
.person = Person{.first_name = "Homer",
|
||||
.last_name = rfl::make_box<std::string>("Simpson"),
|
||||
.age = 45},
|
||||
.employer = rfl::make_box<std::string>("Mr. Burns"),
|
||||
.salary = 60000.0};
|
||||
|
||||
write_and_read(employee);
|
||||
}
|
||||
|
||||
} // namespace test_flatten_anonymous
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_flatten_anonymous{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "test_forward_list.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_forward_list {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::forward_list<Person>> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto children = std::make_unique<std::forward_list<Person>>();
|
||||
children->emplace_front(Person{.first_name = "Maggie"});
|
||||
children->emplace_front(Person{.first_name = "Lisa"});
|
||||
children->emplace_front(Person{.first_name = "Bart"});
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
|
||||
}
|
||||
} // namespace test_forward_list
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_forward_list{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#include "test_literal.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_literal {
|
||||
|
||||
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
|
||||
using LastName = rfl::Literal<"Simpson">;
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", FirstName> first_name;
|
||||
rfl::Rename<"lastName", LastName> last_name;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_literal
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_literal{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "test_literal_map.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_literal_map {
|
||||
|
||||
using FieldName = rfl::Literal<"firstName", "lastName">;
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
std::map<FieldName, std::unique_ptr<std::string>> homer;
|
||||
homer.insert(std::make_pair(FieldName::make<"firstName">(),
|
||||
std::make_unique<std::string>("Homer")));
|
||||
homer.insert(std::make_pair(FieldName::make<"lastName">(),
|
||||
std::make_unique<std::string>("Simpson")));
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_literal_map
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_literal_map{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "test_map.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_map {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::map<std::string, Person> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto children = std::map<std::string, Person>();
|
||||
children.insert(std::make_pair("child1", Person{.first_name = "Bart"}));
|
||||
children.insert(std::make_pair("child2", Person{.first_name = "Lisa"}));
|
||||
children.insert(std::make_pair("child3", Person{.first_name = "Maggie"}));
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_map
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_map{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "test_map_with_key_validation.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_map_with_key_validation {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::map<rfl::AlphaNumeric, Person>> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto children = std::make_unique<std::map<rfl::AlphaNumeric, Person>>();
|
||||
|
||||
children->insert(std::make_pair("Bart", Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair("Lisa", Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair("Maggie", Person{.first_name = "Maggie"}));
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_map_with_key_validation
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_map_with_key_validation{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
#include "test_monster_example.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_monster_example {
|
||||
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
|
||||
struct Weapon {
|
||||
std::string name;
|
||||
short damage;
|
||||
};
|
||||
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
|
||||
struct Vec3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
struct Monster {
|
||||
Vec3 pos;
|
||||
short mana = 150;
|
||||
short hp = 100;
|
||||
std::string name;
|
||||
bool friendly = false;
|
||||
std::vector<int> inventory;
|
||||
Color color = Color::make<"Blue">();
|
||||
std::vector<Weapon> weapons;
|
||||
Equipment equipped;
|
||||
std::vector<Vec3> path;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto sword = Weapon{.name = "Sword", .damage = 3};
|
||||
const auto axe = Weapon{.name = "Axe", .damage = 5};
|
||||
|
||||
const auto weapons = std::vector<Weapon>({sword, axe});
|
||||
|
||||
const auto position = Vec3{1.0f, 2.0f, 3.0f};
|
||||
|
||||
const auto inventory = std::vector<int>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
|
||||
|
||||
const auto orc = Monster{.pos = position,
|
||||
.mana = 150,
|
||||
.hp = 80,
|
||||
.name = "MyMonster",
|
||||
.inventory = inventory,
|
||||
.color = Color::make<"Red">(),
|
||||
.weapons = weapons,
|
||||
.equipped = rfl::make_field<"weapon">(axe)};
|
||||
|
||||
write_and_read(orc);
|
||||
}
|
||||
} // namespace test_monster_example
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_monster_example{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
#include "test_readme_example.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_readme_example {
|
||||
|
||||
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::string town = "Springfield";
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> child;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 10,
|
||||
.email = "bart@simpson.com"};
|
||||
|
||||
const auto lisa = Person{.first_name = "Lisa",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 8,
|
||||
.email = "lisa@simpson.com"};
|
||||
|
||||
const auto maggie = Person{.first_name = "Maggie",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 0,
|
||||
.email = "maggie@simpson.com"};
|
||||
|
||||
const auto homer = Person{.first_name = "Homer",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 45,
|
||||
.email = "homer@simpson.com",
|
||||
.child = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_readme_example
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_readme_example{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "test_readme_example.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_readme_example2 {
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_readme_example2
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_readme_example2 {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "test_ref.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_ref {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
using Tag = rfl::Literal<"Leaf">;
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
using Tag = rfl::Literal<"Node">;
|
||||
rfl::Rename<"criticalValue", double> critical_value;
|
||||
rfl::Ref<DecisionTree> lesser;
|
||||
rfl::Ref<DecisionTree> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
const auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
|
||||
auto node = DecisionTree::Node{
|
||||
.critical_value = 10.0,
|
||||
.lesser = rfl::make_ref<DecisionTree>(DecisionTree{leaf1}),
|
||||
.greater = rfl::make_ref<DecisionTree>(DecisionTree{leaf2})};
|
||||
|
||||
const DecisionTree tree{.leaf_or_node = std::move(node)};
|
||||
|
||||
write_and_read(tree);
|
||||
|
||||
}
|
||||
} // namespace test_ref
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_ref{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
#include "test_save_load.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/cbor.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace test_save_load {
|
||||
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 10,
|
||||
.email = "bart@simpson.com",
|
||||
.children = std::vector<Person>()};
|
||||
|
||||
const auto lisa = Person{.first_name = "Lisa",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 8,
|
||||
.email = "lisa@simpson.com"};
|
||||
|
||||
const auto maggie = Person{.first_name = "Maggie",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 0,
|
||||
.email = "maggie@simpson.com"};
|
||||
|
||||
const auto homer1 =
|
||||
Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 45,
|
||||
.email = "homer@simpson.com",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
rfl::cbor::save("homer.cbor", homer1);
|
||||
|
||||
const auto homer2 = rfl::cbor::load<Person>("homer.cbor").value();
|
||||
|
||||
const auto string1 = rfl::cbor::write(homer1);
|
||||
const auto string2 = rfl::cbor::write(homer2);
|
||||
|
||||
if (string1 != string2) {
|
||||
std::cout << "Test failed. Content was not identical." << std::endl
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_save_load
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_save_load {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#include "test_set.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_set {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::set<std::string>> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto children = std::make_unique<std::set<std::string>>(
|
||||
std::set<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_set
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_set{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "test_size.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_size {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
rfl::Validator<std::vector<Person>,
|
||||
rfl::Size<rfl::AnyOf<rfl::EqualTo<0>, rfl::EqualTo<3>>>>
|
||||
children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{
|
||||
.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
|
||||
const auto lisa = Person{
|
||||
.first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
|
||||
const auto maggie = Person{
|
||||
.first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_size
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_size{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "test_string_map.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_string_map {
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
std::map<std::string, std::unique_ptr<std::string>> homer;
|
||||
homer.insert(
|
||||
std::make_pair("firstName", std::make_unique<std::string>("Homer")));
|
||||
homer.insert(
|
||||
std::make_pair("lastName", std::make_unique<std::string>("Simpson")));
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_string_map
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_string_map{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "test_tagged_union.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_tagged_union {
|
||||
|
||||
struct Circle {
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
double width;
|
||||
};
|
||||
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
|
||||
write_and_read(r);
|
||||
}
|
||||
} // namespace test_tagged_union
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_tagged_union{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "test_timestamp.hpp"
|
||||
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_timestamp {
|
||||
|
||||
using TS = rfl::Timestamp<"%Y-%m-%d">;
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
TS birthday;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto result = TS::from_string("nonsense");
|
||||
|
||||
if (result) {
|
||||
std::cout << "Failed: Expected an error, but got none." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"};
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_timestamp
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_timestamp{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "test_unique_ptr.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_unique_ptr {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::vector<Person>> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto children = std::make_unique<std::vector<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_unique_ptr
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_unique_ptr{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
#include "test_unique_ptr2.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_unique_ptr2 {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
using Tag = rfl::Literal<"Leaf">;
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
using Tag = rfl::Literal<"Node">;
|
||||
rfl::Rename<"criticalValue", double> critical_value;
|
||||
std::unique_ptr<DecisionTree> lesser;
|
||||
std::unique_ptr<DecisionTree> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
|
||||
auto node = DecisionTree::Node{
|
||||
.critical_value = 10.0,
|
||||
.lesser = std::make_unique<DecisionTree>(DecisionTree{leaf1}),
|
||||
.greater = std::make_unique<DecisionTree>(DecisionTree{leaf2})};
|
||||
|
||||
const DecisionTree tree{.leaf_or_node = std::move(node)};
|
||||
|
||||
write_and_read(tree);
|
||||
}
|
||||
} // namespace test_unique_ptr2
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_unique_ptr2{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "test_variant.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_variant {
|
||||
|
||||
struct Circle {
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
double width;
|
||||
};
|
||||
|
||||
using Shapes = std::variant<Circle, Rectangle, std::unique_ptr<Square>>;
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
|
||||
write_and_read(r);
|
||||
}
|
||||
} // namespace test_variant
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_variant{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
#include "test_array.hpp"
|
||||
#include "test_box.hpp"
|
||||
#include "test_custom_class1.hpp"
|
||||
#include "test_custom_class3.hpp"
|
||||
#include "test_custom_class4.hpp"
|
||||
#include "test_default_values.hpp"
|
||||
#include "test_deque.hpp"
|
||||
#include "test_enum.hpp"
|
||||
#include "test_field_variant.hpp"
|
||||
#include "test_flag_enum.hpp"
|
||||
#include "test_flag_enum_with_int.hpp"
|
||||
#include "test_flatten.hpp"
|
||||
#include "test_flatten_anonymous.hpp"
|
||||
#include "test_forward_list.hpp"
|
||||
#include "test_literal.hpp"
|
||||
#include "test_literal_map.hpp"
|
||||
#include "test_map.hpp"
|
||||
#include "test_map_with_key_validation.hpp"
|
||||
#include "test_monster_example.hpp"
|
||||
#include "test_readme_example.hpp"
|
||||
#include "test_readme_example2.hpp"
|
||||
#include "test_ref.hpp"
|
||||
#include "test_save_load.hpp"
|
||||
#include "test_set.hpp"
|
||||
#include "test_size.hpp"
|
||||
#include "test_tagged_union.hpp"
|
||||
#include "test_timestamp.hpp"
|
||||
#include "test_unique_ptr.hpp"
|
||||
#include "test_unique_ptr2.hpp"
|
||||
#include "test_variant.hpp"
|
||||
|
||||
int main() {
|
||||
test_readme_example::test();
|
||||
test_readme_example2::test();
|
||||
test_flatten::test();
|
||||
test_flatten_anonymous::test();
|
||||
test_enum::test();
|
||||
test_flag_enum::test();
|
||||
test_flag_enum_with_int::test();
|
||||
test_map::test();
|
||||
test_map_with_key_validation::test();
|
||||
test_variant::test();
|
||||
test_field_variant::test();
|
||||
test_tagged_union::test();
|
||||
test_deque::test();
|
||||
test_forward_list::test();
|
||||
test_literal_map::test();
|
||||
test_literal::test();
|
||||
test_monster_example::test();
|
||||
test_ref::test();
|
||||
test_set::test();
|
||||
test_size::test();
|
||||
test_timestamp::test();
|
||||
test_unique_ptr::test();
|
||||
test_unique_ptr2::test();
|
||||
test_array::test();
|
||||
test_box::test();
|
||||
test_custom_class1::test();
|
||||
test_custom_class3::test();
|
||||
test_custom_class4::test();
|
||||
test_default_values::test();
|
||||
test_save_load::test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef WRITE_AND_READ_
|
||||
#define WRITE_AND_READ_
|
||||
|
||||
#include <iostream>
|
||||
#include <rfl/cbor.hpp>
|
||||
#include <string>
|
||||
|
||||
template <class T>
|
||||
void write_and_read(const T& _struct) {
|
||||
const auto bytes1 = rfl::cbor::write(_struct);
|
||||
|
||||
const auto res = rfl::cbor::read<T>(bytes1);
|
||||
|
||||
if (!res) {
|
||||
std::cout << "Test failed on read. Error: " << res.error().value().what()
|
||||
<< std::endl
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
const auto bytes2 = rfl::cbor::write(res.value());
|
||||
|
||||
if (bytes1.size() != bytes2.size()) {
|
||||
std::cout << "Test failed on write. Number of bytes was different."
|
||||
<< std::endl
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (bytes1 != bytes2) {
|
||||
std::cout << "Test failed on write. Content was not identical." << std::endl
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -15,6 +15,10 @@
|
||||
"name": "pugixml",
|
||||
"version>=": "1.14"
|
||||
},
|
||||
{
|
||||
"name": "tinycbor",
|
||||
"version>=": "0.6.0"
|
||||
},
|
||||
{
|
||||
"name": "yaml-cpp",
|
||||
"version>=": "0.8.0#1"
|
||||
|
||||
Reference in New Issue
Block a user