diff --git a/include/rfl/avro/read.hpp b/include/rfl/avro/read.hpp index be58f88c..673bbc4e 100644 --- a/include/rfl/avro/read.hpp +++ b/include/rfl/avro/read.hpp @@ -9,6 +9,8 @@ #include #include "../Processors.hpp" +#include "../concepts.hpp" +#include "../internal/ptr_cast.hpp" #include "../internal/wrap_in_rfl_array_t.hpp" #include "Parser.hpp" #include "Reader.hpp" @@ -30,8 +32,10 @@ auto read(const InputVarType& _obj) { /// Parses an object from AVRO using reflection. template Result> read( - const char* _bytes, const size_t _size, const Schema& _schema) noexcept { - avro_reader_t avro_reader = avro_reader_memory(_bytes, _size); + const concepts::ByteLike auto* _bytes, const size_t _size, + const Schema& _schema) noexcept { + avro_reader_t avro_reader = + avro_reader_memory(internal::ptr_cast(_bytes), _size); avro_value_t root; avro_generic_value_new(_schema.iface(), &root); auto err = avro_value_read(avro_reader, &root); @@ -48,20 +52,21 @@ Result> read( /// Parses an object from AVRO using reflection. template -auto read(const char* _bytes, const size_t _size) { +auto read(const concepts::ByteLike auto* _bytes, const size_t _size) { const auto schema = to_schema, Ps...>(); return read(_bytes, _size, schema); } /// Parses an object from AVRO using reflection. template -auto read(const std::vector& _bytes, const Schema& _schema) noexcept { +auto read(const concepts::ContiguousByteContainer auto& _bytes, + const Schema& _schema) noexcept { return read(_bytes.data(), _bytes.size(), _schema); } /// Parses an object from AVRO using reflection. template -auto read(const std::vector& _bytes) { +auto read(const concepts::ContiguousByteContainer auto& _bytes) { return read(_bytes.data(), _bytes.size()); } diff --git a/include/rfl/bson/read.hpp b/include/rfl/bson/read.hpp index 0ac57d60..04ca463d 100644 --- a/include/rfl/bson/read.hpp +++ b/include/rfl/bson/read.hpp @@ -7,6 +7,7 @@ #include #include "../Processors.hpp" +#include "../concepts.hpp" #include "../internal/ptr_cast.hpp" #include "../internal/wrap_in_rfl_array_t.hpp" #include "Parser.hpp" @@ -31,23 +32,18 @@ Result> read(const InputVarType& _obj) { /// Parses an BSON object using reflection. template -auto read(const uint8_t* _bytes, const size_t _size) { +auto read(const concepts::ByteLike auto* _bytes, const size_t _size) { bson_value_t val; val.value.v_doc.data_len = static_cast(_size); - val.value.v_doc.data = const_cast(_bytes); + val.value.v_doc.data = + const_cast(internal::ptr_cast(_bytes)); val.value_type = BSON_TYPE_DOCUMENT; return read(Reader::InputVarType{&val}); } -/// Parses an BSON object using reflection. -template -auto read(const char* _bytes, const size_t _size) { - return read(internal::ptr_cast(_bytes), _size); -} - /// Parses an object from BSON using reflection. template -auto read(const std::vector& _bytes) { +auto read(const concepts::ContiguousByteContainer auto& _bytes) { return read(_bytes.data(), _bytes.size()); } diff --git a/include/rfl/capnproto/read.hpp b/include/rfl/capnproto/read.hpp index f136505b..f209603b 100644 --- a/include/rfl/capnproto/read.hpp +++ b/include/rfl/capnproto/read.hpp @@ -12,6 +12,8 @@ #include "../Processors.hpp" #include "../SnakeCaseToCamelCase.hpp" +#include "../concepts.hpp" +#include "../internal/ptr_cast.hpp" #include "../internal/strings/strings.hpp" #include "../internal/wrap_in_rfl_array_t.hpp" #include "Parser.hpp" @@ -34,9 +36,9 @@ auto read(const InputVarType& _obj) { /// Parses an object from CAPNPROTO using reflection. template -Result> read(const char* _bytes, - const size_t _size, - const Schema& _schema) { +Result> read( + const concepts::ByteLike auto* _bytes, const size_t _size, + const Schema& _schema) { const auto array_ptr = kj::ArrayPtr( internal::ptr_cast(_bytes), _size); auto input_stream = kj::ArrayInputStream(array_ptr); @@ -57,13 +59,14 @@ auto read(const char* _bytes, const size_t _size) { /// Parses an object from CAPNPROTO using reflection. template -auto read(const std::vector& _bytes, const Schema& _schema) noexcept { +auto read(const concepts::ContiguousByteContainer auto& _bytes, + const Schema& _schema) noexcept { return read(_bytes.data(), _bytes.size(), _schema); } /// Parses an object from CAPNPROTO using reflection. template -auto read(const std::vector& _bytes) { +auto read(const concepts::ContiguousByteContainer auto& _bytes) { return read(_bytes.data(), _bytes.size()); } diff --git a/include/rfl/cbor/read.hpp b/include/rfl/cbor/read.hpp index 52a27cf8..12b4508e 100644 --- a/include/rfl/cbor/read.hpp +++ b/include/rfl/cbor/read.hpp @@ -7,8 +7,8 @@ #include #include -#include "../concepts.hpp" #include "../Processors.hpp" +#include "../concepts.hpp" #include "../internal/wrap_in_rfl_array_t.hpp" #include "Parser.hpp" #include "Reader.hpp" @@ -20,12 +20,13 @@ using InputVarType = typename Reader::InputVarType; /// Parses an object from CBOR using reflection. template -Result> read(const ContiguousByteContainer auto& _bytes) { +Result> read( + const concepts::ContiguousByteContainer auto& _bytes) { try { auto val = jsoncons::cbor::decode_cbor(_bytes); auto r = Reader(); return Parser>::read(r, InputVarType{&val}); - } catch(const jsoncons::ser_error& e) { + } catch (const jsoncons::ser_error& e) { std::string error("Could not parse CBOR: "); error.append(e.what()); return rfl::error(error); @@ -35,12 +36,13 @@ Result> read(const ContiguousByteContainer auto /// Parses an object from a stream. template Result> read(std::istream& _stream) { - // TODO: Use a non-throwing decode_cbor(), pending https://github.com/danielaparker/jsoncons/issues/615 + // TODO: Use a non-throwing decode_cbor(), pending + // https://github.com/danielaparker/jsoncons/issues/615 try { auto val = jsoncons::cbor::decode_cbor(_stream); auto r = Reader(); return Parser>::read(r, InputVarType{&val}); - } catch(const jsoncons::ser_error& e) { + } catch (const jsoncons::ser_error& e) { std::string error("Could not parse CBOR: "); error.append(e.what()); return rfl::error(error); diff --git a/include/rfl/concepts.hpp b/include/rfl/concepts.hpp index d752f06d..8ffa10c0 100644 --- a/include/rfl/concepts.hpp +++ b/include/rfl/concepts.hpp @@ -2,64 +2,66 @@ #define RFL_CONCEPTS_HPP_ #include -#include -#include #include +#include #include +#include -namespace rfl { +namespace rfl::concepts { -/// @brief Concept for byte-like types that can be used in contiguous containers +/// Concept for byte-like types that can be used in contiguous containers /// Includes char, signed char, unsigned char, std::byte, and uint8_t -template -concept ByteLike = std::same_as || - std::same_as || +template +concept ByteLike = std::same_as || std::same_as || std::same_as || - std::same_as || - std::same_as; + std::same_as || std::same_as; -/// @brief Concept for containers with a contiguous sequence of byte-like types +/// Concept for containers with a contiguous sequence of byte-like types /// Requires: /// - Container has a value_type that is byte-like /// - Container provides data() method returning a pointer to contiguous memory /// - Container provides size() method returning the number of elements /// - Container supports range-based for loops (begin/end) -template +template concept ContiguousByteContainer = requires(const Container& c) { - typename Container::value_type; - { c.data() } -> std::convertible_to; - { c.size() } -> std::convertible_to; - { c.begin() } -> std::input_iterator; - { c.end() } -> std::input_iterator; - requires ByteLike; - requires std::contiguous_iterator; + typename Container::value_type; + { c.data() } -> std::convertible_to; + { c.size() } -> std::convertible_to; + { c.begin() } -> std::input_iterator; + { c.end() } -> std::input_iterator; + requires ByteLike; + requires std::contiguous_iterator; }; -/// @brief Concept for mutable containers with a contiguous sequence of byte-like types +/// Concept for mutable containers with a contiguous sequence of byte-like types /// Extends ContiguousByteContainer with mutable access requirements -template -concept MutableContiguousByteContainer = ContiguousByteContainer && requires(Container& c) { - { c.data() } -> std::convertible_to; - { c.begin() } -> std::output_iterator; - { c.end() } -> std::output_iterator; -}; +template +concept MutableContiguousByteContainer = + ContiguousByteContainer && requires(Container& c) { + { c.data() } -> std::convertible_to; + { c.begin() } -> std::output_iterator; + { c.end() } -> std::output_iterator; + }; -/// @brief Concept for back-insertable byte containers (like std::vector) +/// Concept for back-insertable byte containers (like std::vector) /// Useful for containers that can grow dynamically during serialization -template -concept BackInsertableByteContainer = ContiguousByteContainer && requires(Container& c, typename Container::value_type v) { - c.push_back(v); - c.reserve(std::size_t{}); - { c.capacity() } -> std::convertible_to; -}; +template +concept BackInsertableByteContainer = + ContiguousByteContainer && + requires(Container& c, typename Container::value_type v) { + c.push_back(v); + c.reserve(std::size_t{}); + { c.capacity() } -> std::convertible_to; + }; -/// @brief Concept for byte spans or views (read-only, non-owning containers) -/// Includes std::span, std::string_view when used with char data, etc. -template +/// Concept for byte spans or views (read-only, non-owning containers) +/// Includes std::span, std::string_view when used with char +/// data, etc. +template concept ByteSpanLike = ContiguousByteContainer && - std::is_trivially_copyable_v && - std::is_trivially_destructible_v; + std::is_trivially_copyable_v && + std::is_trivially_destructible_v; -} // namespace rfl +} // namespace rfl::concepts -#endif // RFL_CONCEPTS_HPP_ +#endif // RFL_CONCEPTS_HPP_ diff --git a/include/rfl/flexbuf/read.hpp b/include/rfl/flexbuf/read.hpp index 26f80103..fa24b7c1 100644 --- a/include/rfl/flexbuf/read.hpp +++ b/include/rfl/flexbuf/read.hpp @@ -8,6 +8,7 @@ #include "../Processors.hpp" #include "../Result.hpp" +#include "../concepts.hpp" #include "../internal/ptr_cast.hpp" #include "Parser.hpp" @@ -25,7 +26,7 @@ auto read(const InputVarType& _obj) { /// Parses an object from flexbuf using reflection. template -auto read(const char* _bytes, const size_t _size) { +auto read(const concepts::ByteLike auto* _bytes, const size_t _size) { const InputVarType root = flexbuffers::GetRoot(internal::ptr_cast(_bytes), _size); return read(root); @@ -33,7 +34,7 @@ auto read(const char* _bytes, const size_t _size) { /// Parses an object from flexbuf using reflection. template -auto read(const std::vector& _bytes) { +auto read(const concepts::ContiguousByteContainer auto& _bytes) { return read(_bytes.data(), _bytes.size()); } diff --git a/include/rfl/msgpack/read.hpp b/include/rfl/msgpack/read.hpp index 0e0b6763..b3597264 100644 --- a/include/rfl/msgpack/read.hpp +++ b/include/rfl/msgpack/read.hpp @@ -7,6 +7,8 @@ #include #include "../Processors.hpp" +#include "../concepts.hpp" +#include "../internal/ptr_cast.hpp" #include "../internal/wrap_in_rfl_array_t.hpp" #include "Parser.hpp" #include "Reader.hpp" @@ -26,12 +28,13 @@ auto read(const InputVarType& _obj) { /// Parses an object from MSGPACK using reflection. template -Result> read(const char* _bytes, - const size_t _size) { +Result> read( + const concepts::ByteLike auto* _bytes, const size_t _size) { msgpack_zone mempool; msgpack_zone_init(&mempool, 2048); msgpack_object deserialized; - msgpack_unpack(_bytes, _size, NULL, &mempool, &deserialized); + msgpack_unpack(internal::ptr_cast(_bytes), _size, NULL, &mempool, + &deserialized); auto r = read(deserialized); msgpack_zone_destroy(&mempool); return r; @@ -39,7 +42,7 @@ Result> read(const char* _bytes, /// Parses an object from MSGPACK using reflection. template -auto read(const std::vector& _bytes) { +auto read(const concepts::ContiguousByteContainer auto& _bytes) { return read(_bytes.data(), _bytes.size()); } diff --git a/include/rfl/ubjson/read.hpp b/include/rfl/ubjson/read.hpp index 5d50cf03..7374906e 100644 --- a/include/rfl/ubjson/read.hpp +++ b/include/rfl/ubjson/read.hpp @@ -7,8 +7,8 @@ #include #include -#include "../concepts.hpp" #include "../Processors.hpp" +#include "../concepts.hpp" #include "../internal/wrap_in_rfl_array_t.hpp" #include "Parser.hpp" #include "Reader.hpp" @@ -20,13 +20,15 @@ using InputVarType = typename Reader::InputVarType; /// Parses an object from UBJSON using reflection. template -Result> read(const ContiguousByteContainer auto& _bytes) { - // TODO: Use a non-throwing decode_ubjson(), pending https://github.com/danielaparker/jsoncons/issues/615 +Result> read( + const concepts::ContiguousByteContainer auto& _bytes) { + // TODO: Use a non-throwing decode_ubjson(), pending + // https://github.com/danielaparker/jsoncons/issues/615 try { auto val = jsoncons::ubjson::decode_ubjson(_bytes); auto r = Reader(); return Parser>::read(r, InputVarType{&val}); - } catch(const jsoncons::ser_error& e) { + } catch (const jsoncons::ser_error& e) { std::string error("Could not parse UBJSON: "); error.append(e.what()); return rfl::error(error); @@ -36,12 +38,13 @@ Result> read(const ContiguousByteContainer auto /// Parses an object from a stream. template Result> read(std::istream& _stream) { - // TODO: Use a non-throwing decode_ubjson(), pending https://github.com/danielaparker/jsoncons/issues/615 + // TODO: Use a non-throwing decode_ubjson(), pending + // https://github.com/danielaparker/jsoncons/issues/615 try { auto val = jsoncons::ubjson::decode_ubjson(_stream); auto r = Reader(); return Parser>::read(r, InputVarType{&val}); - } catch(const jsoncons::ser_error& e) { + } catch (const jsoncons::ser_error& e) { std::string error("Could not parse UBJSON: "); error.append(e.what()); return rfl::error(error); diff --git a/tests/generic/test_byte_containers.cpp b/tests/generic/test_byte_containers.cpp index 7af46a8b..e39eacf0 100644 --- a/tests/generic/test_byte_containers.cpp +++ b/tests/generic/test_byte_containers.cpp @@ -1,8 +1,8 @@ -#include #include -#include #include #include +#include +#include #include "write_and_read.hpp" @@ -15,40 +15,53 @@ TEST(generic, test_byte_container_concepts) { std::array arr_uint8{1, 2, 3, 4}; std::string str_data{"hello"}; std::string_view str_view{"world"}; - std::array arr_byte{std::byte{5}, std::byte{4}, std::byte{3}, std::byte{2}, std::byte{1}}; + std::array arr_byte{std::byte{5}, std::byte{4}, std::byte{3}, + std::byte{2}, std::byte{1}}; std::span byte_span = arr_byte; // Test with concepts - static_assert(rfl::ByteLike); - static_assert(rfl::ByteLike); - static_assert(rfl::ByteLike); - static_assert(rfl::ByteLike); - static_assert(!rfl::ByteLike); - static_assert(!rfl::ByteLike); + static_assert(rfl::concepts::ByteLike); + static_assert(rfl::concepts::ByteLike); + static_assert(rfl::concepts::ByteLike); + static_assert(rfl::concepts::ByteLike); + static_assert(!rfl::concepts::ByteLike); + static_assert(!rfl::concepts::ByteLike); - static_assert(rfl::ContiguousByteContainer); - static_assert(rfl::ContiguousByteContainer); - static_assert(rfl::ContiguousByteContainer); - static_assert(rfl::ContiguousByteContainer); - static_assert(rfl::ContiguousByteContainer); - static_assert(rfl::ContiguousByteContainer); - static_assert(rfl::ContiguousByteContainer); + static_assert(rfl::concepts::ContiguousByteContainer); + static_assert(rfl::concepts::ContiguousByteContainer); + static_assert(rfl::concepts::ContiguousByteContainer); + static_assert(rfl::concepts::ContiguousByteContainer); + static_assert(rfl::concepts::ContiguousByteContainer); + static_assert(rfl::concepts::ContiguousByteContainer); + static_assert(rfl::concepts::ContiguousByteContainer); - static_assert(rfl::MutableContiguousByteContainer); - static_assert(rfl::MutableContiguousByteContainer); - static_assert(rfl::MutableContiguousByteContainer); - static_assert(rfl::MutableContiguousByteContainer); - static_assert(!rfl::MutableContiguousByteContainer); - static_assert(rfl::MutableContiguousByteContainer); - static_assert(rfl::MutableContiguousByteContainer); + static_assert( + rfl::concepts::MutableContiguousByteContainer); + static_assert( + rfl::concepts::MutableContiguousByteContainer); + static_assert( + rfl::concepts::MutableContiguousByteContainer); + static_assert( + rfl::concepts::MutableContiguousByteContainer); + static_assert( + !rfl::concepts::MutableContiguousByteContainer); + static_assert( + rfl::concepts::MutableContiguousByteContainer); + static_assert( + rfl::concepts::MutableContiguousByteContainer); - static_assert(rfl::BackInsertableByteContainer); - static_assert(rfl::BackInsertableByteContainer); - static_assert(!rfl::BackInsertableByteContainer); - static_assert(rfl::BackInsertableByteContainer); - static_assert(!rfl::BackInsertableByteContainer); - static_assert(!rfl::BackInsertableByteContainer); - static_assert(!rfl::BackInsertableByteContainer); + static_assert( + rfl::concepts::BackInsertableByteContainer); + static_assert(rfl::concepts::BackInsertableByteContainer); + static_assert( + !rfl::concepts::BackInsertableByteContainer); + static_assert(rfl::concepts::BackInsertableByteContainer); + static_assert( + !rfl::concepts::BackInsertableByteContainer); + static_assert( + !rfl::concepts::BackInsertableByteContainer); + static_assert( + !rfl::concepts::BackInsertableByteContainer); } } // namespace test_byte_container_concepts