diff --git a/include/rfl/Result.hpp b/include/rfl/Result.hpp index c19ae369..c4471dc1 100644 --- a/include/rfl/Result.hpp +++ b/include/rfl/Result.hpp @@ -306,7 +306,7 @@ class Result { void destroy() { if (success_) { - if constexpr (std::is_destructible_v /*&& !internal::is_array_v*/) { + if constexpr (std::is_destructible_v>) { get_t().~T(); } } else { diff --git a/include/rfl/get.hpp b/include/rfl/get.hpp index 0edecb0e..74ed3c6a 100644 --- a/include/rfl/get.hpp +++ b/include/rfl/get.hpp @@ -9,38 +9,38 @@ namespace rfl { /// Gets a field by index. template inline auto& get(NamedTupleType& _tup) { - return internal::Getter::template get<_index>(_tup); + return internal::Getter::template get<_index>(_tup); } /// Gets a field by name. template inline auto& get(NamedTupleType& _tup) { - return internal::Getter::template get<_field_name>(_tup); + return internal::Getter::template get<_field_name>(_tup); } /// Gets a field by the field type. template inline auto& get(NamedTupleType& _tup) { - return internal::Getter::template get(_tup); + return internal::Getter::template get(_tup); } /// Gets a field by index. template inline const auto& get(const NamedTupleType& _tup) { - return internal::Getter::template get_const<_index>(_tup); + return internal::Getter::template get_const<_index>(_tup); } /// Gets a field by name. template inline const auto& get(const NamedTupleType& _tup) { - return internal::Getter::template get_const<_field_name>( - _tup); + return internal::Getter::template get_const<_field_name>( + _tup); } /// Gets a field by the field type. template inline const auto& get(const NamedTupleType& _tup) { - return internal::Getter::template get_const(_tup); + return internal::Getter::template get_const(_tup); } } // namespace rfl diff --git a/include/rfl/parsing/ArrayReader.hpp b/include/rfl/parsing/ArrayReader.hpp index dc3af773..9724b68e 100644 --- a/include/rfl/parsing/ArrayReader.hpp +++ b/include/rfl/parsing/ArrayReader.hpp @@ -21,20 +21,22 @@ class ArrayReader { public: ArrayReader(const R* _r, std::array* _array) - : array_(_array), i_(0), r_(_r) {} + : array_(_array), num_set_(0), r_(_r) {} ~ArrayReader() = default; std::optional check_size() const { - if (i_ != size_) { + if (num_set_ != size_) { return Error("Expected " + std::to_string(size_) + " elements, got " + - std::to_string(i_) + "."); + std::to_string(num_set_) + "."); } return std::nullopt; } + size_t num_set() const { return num_set_; } + std::optional read(const InputVarType& _var) const { - if (i_ == size_) { + if (num_set_ == size_) { return Error("Expected " + std::to_string(size_) + " elements, got at least " + std::to_string(size_ + 1) + "."); @@ -42,41 +44,16 @@ class ArrayReader { auto res = Parser, ProcessorsType>::read(*r_, _var); if (res) { - move_to(&((*array_)[i_]), &(*res)); + move_to(&((*array_)[num_set_]), &(*res)); } else { - return Error("Failed to parse element " + std::to_string(i_) + ": " + - res.error()->what()); - } - ++i_; - return std::nullopt; - } - - /// Because of the way we have allocated the fields, we need to manually - /// trigger the destructors. - void call_destructors_where_necessary() const { - for (size_t i = 0; i < std::min(i_, size_); ++i) { - if constexpr (!std::is_array_v && std::is_pointer_v && - std::is_destructible_v) { - (*array_)[i].~T(); - } else if constexpr (std::is_array_v) { - auto ptr = (*array_)[i]; - call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr); - } + return Error("Failed to parse element " + std::to_string(num_set_) + + ": " + res.error()->what()); } + ++num_set_; + return std::optional(); } private: - template - void call_destructor_on_array(const size_t _size2, U* _ptr) const { - for (size_t i = 0; i < _size2; ++i) { - if constexpr (std::is_array_v) { - call_destructor_on_array(sizeof(*_ptr) / sizeof(**_ptr), *(_ptr + i)); - } else if constexpr (std::is_destructible_v) { - (_ptr + i)->~U(); - } - } - } - template void move_to(Target* _t, Source* _s) const { if constexpr (std::is_const_v) { @@ -100,7 +77,7 @@ class ArrayReader { std::array* array_; /// Indicates the current field - mutable size_t i_; + mutable size_t num_set_; /// The underlying reader. const R* r_; diff --git a/include/rfl/parsing/NamedTupleParser.hpp b/include/rfl/parsing/NamedTupleParser.hpp index d78b5434..e3babc0c 100644 --- a/include/rfl/parsing/NamedTupleParser.hpp +++ b/include/rfl/parsing/NamedTupleParser.hpp @@ -27,6 +27,7 @@ #include "ViewReaderWithDefault.hpp" #include "ViewReaderWithDefaultAndStrippedFieldNames.hpp" #include "ViewReaderWithStrippedFieldNames.hpp" +#include "call_destructors_where_necessary.hpp" #include "is_empty.hpp" #include "is_required.hpp" #include "schema/Type.hpp" @@ -86,30 +87,36 @@ struct NamedTupleParser { auto ptr = std::launder(reinterpret_cast*>(buf)); auto view = rfl::to_view(*ptr); using ViewType = std::remove_cvref_t; - const auto err = + const auto [set, err] = Parser::read_view(_r, _var, &view); if (err) [[unlikely]] { + call_destructors_where_necessary(set, &view); return *err; } - return *ptr; + auto res = Result>(std::move(*ptr)); + call_destructors_where_necessary(set, &view); + return res; } /// Reads the data into a view assuming no default values. - static std::optional read_view( - const R& _r, const InputVarType& _var, - NamedTuple* _view) noexcept { + static std::pair, + std::optional> + read_view(const R& _r, const InputVarType& _var, + NamedTuple* _view) noexcept { static_assert( internal::no_duplicate_field_names()); if constexpr (_no_field_names) { auto arr = _r.to_array(_var); if (!arr) [[unlikely]] { - return arr.error(); + auto set = std::array{}; + return std::make_pair(set, arr.error()); } return read_object_or_array(_r, *arr, _view); } else { auto obj = _r.to_object(_var); if (!obj) [[unlikely]] { - return obj.error(); + auto set = std::array{}; + return std::make_pair(set, obj.error()); } return read_object_or_array(_r, *obj, _view); } @@ -284,9 +291,10 @@ struct NamedTupleParser { } } - static std::optional read_object_or_array( - const R& _r, const InputObjectOrArrayType& _obj_or_arr, - NamedTupleType* _view) noexcept { + static std::pair, + std::optional> + read_object_or_array(const R& _r, const InputObjectOrArrayType& _obj_or_arr, + NamedTupleType* _view) noexcept { auto found = std::array(); found.fill(false); auto set = std::array(); @@ -300,15 +308,14 @@ struct NamedTupleParser { err = _r.read_object(reader, _obj_or_arr); } if (err) { - return err; + return std::make_pair(set, err); } handle_missing_fields(found, *_view, &set, &errors, std::make_integer_sequence()); if (errors.size() != 0) { - reader.call_destructors_where_necessary(); - return to_single_error_message(errors); + return std::make_pair(set, to_single_error_message(errors)); } - return std::nullopt; + return std::make_pair(set, std::optional()); } static std::optional read_object_or_array_with_default( diff --git a/include/rfl/parsing/Parser_array.hpp b/include/rfl/parsing/Parser_array.hpp index 22a6b691..5e1cf636 100644 --- a/include/rfl/parsing/Parser_array.hpp +++ b/include/rfl/parsing/Parser_array.hpp @@ -12,6 +12,7 @@ #include "ArrayReader.hpp" #include "Parent.hpp" #include "Parser_base.hpp" +#include "call_destructors_on_array_where_necessary.hpp" #include "schema/Type.hpp" namespace rfl { @@ -36,15 +37,17 @@ struct Parser, ProcessorsType> { ArrayReader(&_r, ptr); auto err = _r.read_array(array_reader, _arr); if (err) { - array_reader.call_destructors_where_necessary(); + call_destructors_on_array_where_necessary(array_reader.num_set(), ptr); return *err; } err = array_reader.check_size(); if (err) { - array_reader.call_destructors_where_necessary(); + call_destructors_on_array_where_necessary(array_reader.num_set(), ptr); return *err; } - return std::move(*ptr); + auto result = Result>(std::move(*ptr)); + call_destructors_on_array_where_necessary(array_reader.num_set(), ptr); + return result; }; return _r.to_array(_var).and_then(parse); diff --git a/include/rfl/parsing/Parser_default.hpp b/include/rfl/parsing/Parser_default.hpp index 87b7c21c..4695736c 100644 --- a/include/rfl/parsing/Parser_default.hpp +++ b/include/rfl/parsing/Parser_default.hpp @@ -24,6 +24,7 @@ #include "AreReaderAndWriter.hpp" #include "Parent.hpp" #include "Parser_base.hpp" +#include "call_destructors_where_necessary.hpp" #include "is_tagged_union_wrapper.hpp" #include "schema/Type.hpp" @@ -74,11 +75,12 @@ struct Parser { } } else if constexpr (std::is_enum_v) { if constexpr (ProcessorsType::underlying_enums_) { - return static_cast(*_r.template to_basic_type>(_var)); + return static_cast( + *_r.template to_basic_type>(_var)); } else { - using StringConverter = internal::enums::StringConverter; - return _r.template to_basic_type(_var).and_then( - StringConverter::string_to_enum); + using StringConverter = internal::enums::StringConverter; + return _r.template to_basic_type(_var).and_then( + StringConverter::string_to_enum); } } else { return _r.template to_basic_type>(_var); @@ -108,7 +110,7 @@ struct Parser { _w, ptr_named_tuple, _parent); } else if constexpr (std::is_enum_v) { if constexpr (ProcessorsType::underlying_enums_) { - const auto val = static_cast>(_var); + const auto val = static_cast>(_var); ParentType::add_value(_w, val, _parent); } else { using StringConverter = internal::enums::StringConverter; @@ -194,8 +196,7 @@ struct Parser { using S = internal::enums::StringConverter; if constexpr (ProcessorsType::underlying_enums_) { return Type{Type::Integer{}}; - } - else if constexpr (S::is_flag_enum_) { + } else if constexpr (S::is_flag_enum_) { return Type{Type::String{}}; } else { return Parser::to_schema( @@ -257,12 +258,15 @@ struct Parser { auto ptr = std::launder(reinterpret_cast(buf)); auto view = ProcessorsType::template process(to_view(*ptr)); using ViewType = std::remove_cvref_t; - const auto err = + const auto [set, err] = Parser::read_view(_r, _var, &view); if (err) [[unlikely]] { + call_destructors_where_necessary(set, &view); return *err; } - return std::move(*ptr); + auto res = Result(std::move(*ptr)); + call_destructors_where_necessary(set, &view); + return res; } /// This is actually more straight-forward than the standard case - we just diff --git a/include/rfl/parsing/TupleParser.hpp b/include/rfl/parsing/TupleParser.hpp index 3b1bd63f..ef60f7f8 100644 --- a/include/rfl/parsing/TupleParser.hpp +++ b/include/rfl/parsing/TupleParser.hpp @@ -12,6 +12,7 @@ #include "../internal/nth_element_t.hpp" #include "Parent.hpp" #include "TupleReader.hpp" +#include "call_destructors_on_tuple_where_necessary.hpp" #include "schema/Type.hpp" namespace rfl::parsing { @@ -36,15 +37,17 @@ struct TupleParser { ProcessorsType>(&_r, ptr); auto err = _r.read_array(tuple_reader, _arr); if (err) { - tuple_reader.call_destructors_where_necessary(); + call_destructors_on_tuple_where_necessary(tuple_reader.num_set(), ptr); return *err; } err = tuple_reader.handle_missing_fields(); if (err) { - tuple_reader.call_destructors_where_necessary(); + call_destructors_on_tuple_where_necessary(tuple_reader.num_set(), ptr); return *err; } - return std::move(*ptr); + auto res = Result(std::move(*ptr)); + call_destructors_on_tuple_where_necessary(tuple_reader.num_set(), ptr); + return res; }; return _r.to_array(_var).and_then(parse); diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index 25d76bd6..efe0b926 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -20,51 +20,32 @@ class TupleReader { static constexpr size_t size_ = rfl::tuple_size_v; public: - TupleReader(const R* _r, TupleType* _tuple) : i_(0), r_(_r), tuple_(_tuple) {} + TupleReader(const R* _r, TupleType* _tuple) + : num_set_(0), r_(_r), tuple_(_tuple) {} ~TupleReader() = default; std::optional handle_missing_fields() const { std::optional err; - if (i_ < size_) { + if (num_set_ < size_) { handle_missing_fields_impl(&err); } return err; } + size_t num_set() const { return num_set_; } + std::optional read(const InputVarType& _var) const { std::optional err; read_impl(_var, &err); return err; } - /// Because of the way we have allocated the fields, we need to manually - /// trigger the destructors. - template - void call_destructors_where_necessary() const { - if constexpr (_i < size_) { - if (_i >= i_) { - return; - } - using CurrentType = - std::remove_cvref_t>; - if constexpr (!std::is_array_v && - std::is_pointer_v && - std::is_destructible_v) { - rfl::get<_i>(*tuple_)->~CurrentType(); - } else if constexpr (std::is_array_v) { - auto ptr = rfl::get<_i>(*tuple_); - call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr); - } - call_destructors_where_necessary<_i + 1>(); - } - } - private: template void handle_missing_fields_impl(std::optional* _err) const noexcept { if constexpr (_i < size_) { - if (i_ == _i) { + if (num_set_ == _i) { using CurrentType = std::remove_cvref_t>; @@ -75,7 +56,7 @@ class TupleReader { return; } else { ::new (&(rfl::get<_i>(*tuple_))) CurrentType(); - ++i_; + ++num_set_; } } handle_missing_fields_impl<_i + 1>(_err); @@ -85,7 +66,7 @@ class TupleReader { template void read_impl(const InputVarType& _var, std::optional* _err) const { if constexpr (_i < size_) { - if (i_ == _i) { + if (num_set_ == _i) { using CurrentType = std::remove_cvref_t>; @@ -93,7 +74,7 @@ class TupleReader { if (res) { move_to(&(rfl::get<_i>(*tuple_)), &(*res)); - ++i_; + ++num_set_; } else { *_err = Error("Failed to parse field " + std::to_string(_i) + ": " + res.error()->what()); @@ -108,17 +89,6 @@ class TupleReader { } } - template - void call_destructor_on_array(const size_t _size, T* _ptr) const { - for (size_t i = 0; i < _size; ++i) { - if constexpr (std::is_array_v) { - call_destructor_on_array(sizeof(*_ptr) / sizeof(**_ptr), *(_ptr + i)); - } else if constexpr (std::is_destructible_v) { - (_ptr + i)->~T(); - } - } - } - template void move_to(Target* _t, Source* _s) const { if constexpr (std::is_const_v) { @@ -139,7 +109,7 @@ class TupleReader { private: /// Indicates the last field that was set. - mutable size_t i_; + mutable size_t num_set_; /// The underlying reader. const R* r_; diff --git a/include/rfl/parsing/ViewReader.hpp b/include/rfl/parsing/ViewReader.hpp index 1c58fe8e..ea1b4e4f 100644 --- a/include/rfl/parsing/ViewReader.hpp +++ b/include/rfl/parsing/ViewReader.hpp @@ -34,15 +34,6 @@ class ViewReader { std::make_integer_sequence()); } - /// Because of the way we have allocated the fields, we need to manually - /// trigger the destructors. - void call_destructors_where_necessary() const { - [&](std::integer_sequence) { - (call_destructor_on_one_if_necessary(), ...); - } - (std::make_integer_sequence()); - } - private: template static void assign_if_field_matches(const R& _r, @@ -128,37 +119,6 @@ class ViewReader { } } - template - static void call_destructor_on_array(const size_t _size, T* _ptr) { - for (size_t i = 0; i < _size; ++i) { - if constexpr (std::is_array_v) { - call_destructor_on_array(sizeof(*_ptr) / sizeof(**_ptr), *(_ptr + i)); - } else if constexpr (std::is_destructible_v) { - (_ptr + i)->~T(); - } - } - } - - template - void call_destructor_on_one_if_necessary() const { - using FieldType = tuple_element_t<_i, typename ViewType::Fields>; - using OriginalType = std::remove_cvref_t; - using ValueType = - std::remove_cvref_t>; - if constexpr (!std::is_array_v && - std::is_pointer_v && - std::is_destructible_v) { - if (std::get<_i>(*set_)) { - rfl::get<_i>(*view_)->~ValueType(); - } - } else if constexpr (std::is_array_v) { - if (std::get<_i>(*set_)) { - auto ptr = rfl::get<_i>(*view_); - call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr); - } - } - } - template static void move_to(Target* _t, Source* _s) { if constexpr (std::is_const_v) { diff --git a/include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp b/include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp index 1ae18316..678c0231 100644 --- a/include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp +++ b/include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp @@ -46,15 +46,6 @@ class ViewReaderWithStrippedFieldNames { return std::nullopt; } - /// Because of the way we have allocated the fields, we need to manually - /// trigger the destructors. - void call_destructors_where_necessary() const { - [&](std::integer_sequence) { - (call_destructor_on_one_if_necessary(), ...); - } - (std::make_integer_sequence()); - } - private: template static void assign_if_field_is_field_i(const R& _r, const auto& _var, @@ -91,39 +82,6 @@ class ViewReaderWithStrippedFieldNames { ...); } - // TODO: Unnecessary code duplication. - template - static void call_destructor_on_array(const size_t _size, T* _ptr) { - for (size_t i = 0; i < _size; ++i) { - if constexpr (std::is_array_v) { - call_destructor_on_array(sizeof(*_ptr) / sizeof(**_ptr), *(_ptr + i)); - } else if constexpr (std::is_destructible_v) { - (_ptr + i)->~T(); - } - } - } - - // TODO: Unnecessary code duplication. - template - void call_destructor_on_one_if_necessary() const { - using FieldType = tuple_element_t<_i, typename ViewType::Fields>; - using OriginalType = std::remove_cvref_t; - using ValueType = - std::remove_cvref_t>; - if constexpr (!std::is_array_v && - std::is_pointer_v && - std::is_destructible_v) { - if (std::get<_i>(*set_)) { - rfl::get<_i>(*view_)->~ValueType(); - } - } else if constexpr (std::is_array_v) { - if (std::get<_i>(*set_)) { - auto ptr = rfl::get<_i>(*view_); - call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr); - } - } - } - // TODO: Unnecessary code duplication. template static void move_to(Target* _t, Source* _s) { diff --git a/include/rfl/parsing/call_destructors_on_array_where_necessary.hpp b/include/rfl/parsing/call_destructors_on_array_where_necessary.hpp new file mode 100644 index 00000000..1b02d3fd --- /dev/null +++ b/include/rfl/parsing/call_destructors_on_array_where_necessary.hpp @@ -0,0 +1,30 @@ +#ifndef RFL_PARSING_CALL_DESTRUCTORS_ON_ARRAY_WHERE_NECESSARY_HPP_ +#define RFL_PARSING_CALL_DESTRUCTORS_ON_ARRAY_WHERE_NECESSARY_HPP_ + +#include +#include + +#include "call_destructors_where_necessary.hpp" + +namespace rfl::parsing { + +/// Because of the way we have allocated the fields, we need to manually +/// trigger the destructors. + +template +void call_destructors_on_array_where_necessary(const size_t _num_set, + std::array* _array) { + for (size_t i = 0; i < std::min(_num_set, _size); ++i) { + if constexpr (!std::is_array_v && + std::is_destructible_v>) { + (*_array)[i].~T(); + } else if constexpr (std::is_array_v) { + auto ptr = (*_array)[i]; + call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr); + } + } +} + +} // namespace rfl::parsing + +#endif diff --git a/include/rfl/parsing/call_destructors_on_tuple_where_necessary.hpp b/include/rfl/parsing/call_destructors_on_tuple_where_necessary.hpp new file mode 100644 index 00000000..09ae685a --- /dev/null +++ b/include/rfl/parsing/call_destructors_on_tuple_where_necessary.hpp @@ -0,0 +1,47 @@ +#ifndef RFL_PARSING_CALL_DESTRUCTORS_ON_TUPLE_WHERE_NECESSARY_HPP_ +#define RFL_PARSING_CALL_DESTRUCTORS_ON_TUPLE_WHERE_NECESSARY_HPP_ + +#include +#include + +#include "../Tuple.hpp" +#include "call_destructors_where_necessary.hpp" + +namespace rfl::parsing { + +/// Because of the way we have allocated the fields, we need to manually +/// trigger the destructors. + +template +void call_destructor_on_one_tuple_element_if_necessary(const size_t _num_set, + TupleType* _tup) { + using FieldType = tuple_element_t<_i, TupleType>; + using OriginalType = std::remove_cv_t; + using ValueType = std::remove_cv_t>; + if constexpr (!std::is_array_v && + std::is_destructible_v) { + if (_i < _num_set) { + rfl::get<_i>(*_tup).~ValueType(); + } + } else if constexpr (std::is_array_v) { + if (_i < _num_set) { + auto ptr = rfl::get<_i>(*_tup); + call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr); + } + } +} + +template +void call_destructors_on_tuple_where_necessary(const size_t _num_set, + TupleType* _tup) { + [&](std::integer_sequence) { + (call_destructor_on_one_tuple_element_if_necessary< + TupleType, rfl::tuple_size_v, is>(_num_set, _tup), + ...); + } + (std::make_integer_sequence>()); +} + +} // namespace rfl::parsing + +#endif diff --git a/include/rfl/parsing/call_destructors_where_necessary.hpp b/include/rfl/parsing/call_destructors_where_necessary.hpp new file mode 100644 index 00000000..96a30bca --- /dev/null +++ b/include/rfl/parsing/call_destructors_where_necessary.hpp @@ -0,0 +1,56 @@ +#ifndef RFL_PARSING_CALL_DESTRUCTORS_WHERE_NECESSARY_HPP_ +#define RFL_PARSING_CALL_DESTRUCTORS_WHERE_NECESSARY_HPP_ + +#include +#include + +namespace rfl::parsing { + +/// Because of the way we have allocated the fields, we need to manually +/// trigger the destructors. + +template +void call_destructor_on_array(const size_t _size, T* _ptr) { + for (size_t i = 0; i < _size; ++i) { + if constexpr (std::is_array_v) { + call_destructor_on_array(sizeof(*_ptr) / sizeof(**_ptr), *(_ptr + i)); + } else if constexpr (std::is_destructible_v>) { + (_ptr + i)->~T(); + } + } +} + +template +void call_destructor_on_one_if_necessary(const std::array& _set, + ViewType* _view) { + using FieldType = tuple_element_t<_i, typename ViewType::Fields>; + using OriginalType = std::remove_cv_t; + using ValueType = + std::remove_cvref_t>; + if constexpr (!std::is_array_v && + std::is_pointer_v && + std::is_destructible_v) { + if (std::get<_i>(_set)) { + rfl::get<_i>(*_view)->~ValueType(); + } + } else if constexpr (std::is_array_v) { + if (std::get<_i>(_set)) { + auto ptr = rfl::get<_i>(*_view); + call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr); + } + } +} + +template +void call_destructors_where_necessary(const std::array& _set, + ViewType* _view) { + [&](std::integer_sequence) { + (call_destructor_on_one_if_necessary(_set, _view), + ...); + } + (std::make_integer_sequence()); +} + +} // namespace rfl::parsing + +#endif diff --git a/tests/json/test_leak.cpp b/tests/json/test_leak.cpp new file mode 100644 index 00000000..f9b3378f --- /dev/null +++ b/tests/json/test_leak.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_leak { + +class TestLeak { + public: + inline static int create_count = 0; + inline static int delete_count = 0; + + using ReflectionType = std::string; + + TestLeak(const std::string& _str) : moved_(false), str_(_str) { + ++create_count; + } + + TestLeak(const TestLeak& _other) : moved_(false), str_(_other.str_) { + ++create_count; + } + + TestLeak(TestLeak&& _other) noexcept + : moved_(false), str_(std::move(_other.str_)) { + _other.moved_ = true; + ++create_count; + ++delete_count; + } + + ~TestLeak() { + if (!moved_) { + ++delete_count; + } + } + + TestLeak& operator=(const TestLeak& _other) { + str_ = _other.str_; + return *this; + } + + TestLeak& operator=(TestLeak&& _other) noexcept { + str_ = std::move(_other.str_); + _other.moved_ = true; + ++delete_count; + return *this; + } + + const std::string& reflection() { return str_; } + + private: + bool moved_; + std::string str_; +}; + +struct Person { + std::string first_name; + std::string last_name; + const TestLeak test; +}; + +TEST(json, test_leak) { + { + const auto r = rfl::json::read( + R"({"first_name":"Homer","last_name":"Simpson","test":"test"})"); + } + EXPECT_EQ(TestLeak::delete_count, TestLeak::create_count); +} +} // namespace test_leak diff --git a/tests/json/test_leak_arr.cpp b/tests/json/test_leak_arr.cpp new file mode 100644 index 00000000..5ce29dd8 --- /dev/null +++ b/tests/json/test_leak_arr.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_leak_arr { + +class TestLeak { + public: + inline static int create_count = 0; + inline static int delete_count = 0; + + using ReflectionType = std::string; + + TestLeak(const std::string& _str) : moved_(false), str_(_str) { + ++create_count; + } + + TestLeak(const TestLeak& _other) : moved_(false), str_(_other.str_) { + ++create_count; + } + + TestLeak(TestLeak&& _other) noexcept + : moved_(false), str_(std::move(_other.str_)) { + _other.moved_ = true; + ++create_count; + ++delete_count; + } + + ~TestLeak() { + if (!moved_) { + ++delete_count; + } + } + + TestLeak& operator=(const TestLeak& _other) { + str_ = _other.str_; + return *this; + } + + TestLeak& operator=(TestLeak&& _other) noexcept { + str_ = std::move(_other.str_); + _other.moved_ = true; + ++delete_count; + return *this; + } + + const std::string& reflection() { return str_; } + + private: + bool moved_; + std::string str_; +}; + +TEST(json, test_leak_arr) { + { + const auto r = rfl::json::read>( + R"(["Homer","Simpson","test"])"); + } + EXPECT_EQ(TestLeak::delete_count, TestLeak::create_count); +} +} // namespace test_leak_arr diff --git a/tests/json/test_leak_rfl_tup.cpp b/tests/json/test_leak_rfl_tup.cpp new file mode 100644 index 00000000..1f73246d --- /dev/null +++ b/tests/json/test_leak_rfl_tup.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_leak_rfl_tup { + +class TestLeak { + public: + inline static int create_count = 0; + inline static int delete_count = 0; + + using ReflectionType = std::string; + + TestLeak(const std::string& _str) : moved_(false), str_(_str) { + ++create_count; + } + + TestLeak(const TestLeak& _other) : moved_(false), str_(_other.str_) { + ++create_count; + } + + TestLeak(TestLeak&& _other) noexcept + : moved_(false), str_(std::move(_other.str_)) { + _other.moved_ = true; + ++create_count; + ++delete_count; + } + + ~TestLeak() { + if (!moved_) { + ++delete_count; + } + } + + TestLeak& operator=(const TestLeak& _other) { + str_ = _other.str_; + return *this; + } + + TestLeak& operator=(TestLeak&& _other) noexcept { + str_ = std::move(_other.str_); + _other.moved_ = true; + ++delete_count; + return *this; + } + + const std::string& reflection() { return str_; } + + private: + bool moved_; + std::string str_; +}; + +TEST(json, test_leak_rfl_tup) { + { + const auto r = + rfl::json::read>( + R"(["Homer","Simpson","test"])"); + } + EXPECT_EQ(TestLeak::delete_count, TestLeak::create_count); +} +} // namespace test_leak_rfl_tup diff --git a/tests/json/test_leak_tup.cpp b/tests/json/test_leak_tup.cpp new file mode 100644 index 00000000..aba8d70c --- /dev/null +++ b/tests/json/test_leak_tup.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_leak_tup { + +class TestLeak { + public: + inline static int create_count = 0; + inline static int delete_count = 0; + + using ReflectionType = std::string; + + TestLeak(const std::string& _str) : moved_(false), str_(_str) { + ++create_count; + } + + TestLeak(const TestLeak& _other) : moved_(false), str_(_other.str_) { + ++create_count; + } + + TestLeak(TestLeak&& _other) noexcept + : moved_(false), str_(std::move(_other.str_)) { + _other.moved_ = true; + ++create_count; + ++delete_count; + } + + ~TestLeak() { + if (!moved_) { + ++delete_count; + } + } + + TestLeak& operator=(const TestLeak& _other) { + str_ = _other.str_; + return *this; + } + + TestLeak& operator=(TestLeak&& _other) noexcept { + str_ = std::move(_other.str_); + _other.moved_ = true; + ++delete_count; + return *this; + } + + const std::string& reflection() { return str_; } + + private: + bool moved_; + std::string str_; +}; + +TEST(json, test_leak_tup) { + { + const auto r = + rfl::json::read>( + R"(["Homer","Simpson","test"])"); + } + EXPECT_EQ(TestLeak::delete_count, TestLeak::create_count); +} +} // namespace test_leak_tup diff --git a/tests/json_c_arrays_and_inheritance/CMakeLists.txt b/tests/json_c_arrays_and_inheritance/CMakeLists.txt index 6536ee34..a0ed7d98 100644 --- a/tests/json_c_arrays_and_inheritance/CMakeLists.txt +++ b/tests/json_c_arrays_and_inheritance/CMakeLists.txt @@ -9,6 +9,7 @@ add_executable( ) target_precompile_headers(reflect-cpp-json-c-arrays-and-inheritance-tests PRIVATE [["rfl.hpp"]] ) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DREFLECT_CPP_C_ARRAYS_OR_INHERITANCE") target_include_directories(reflect-cpp-json-c-arrays-and-inheritance-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include")