Skip to content

Commit fe935c4

Browse files
Began using if constexpr instead of SFINAE
1 parent 92413ca commit fe935c4

8 files changed

Lines changed: 158 additions & 25 deletions

File tree

include/rfl/parsing/Parser.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "Parser_rfl_array.hpp"
2929
#include "Parser_rfl_tuple.hpp"
3030
#include "Parser_rfl_variant.hpp"
31-
#include "Parser_shared_ptr.hpp"
3231
#include "Parser_short.hpp"
3332
#include "Parser_skip.hpp"
3433
#include "Parser_span.hpp"
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@
2222

2323
namespace rfl::parsing {
2424

25+
template <class T>
26+
struct is_shared_ptr : std::false_type {};
27+
28+
template <class T>
29+
struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
30+
31+
template <class T>
32+
constexpr bool is_shared_ptr_v = is_shared_ptr<std::remove_cvref_t<T>>::value;
33+
2534
template <class R, class W, class T, class ProcessorsType>
26-
requires AreReaderAndWriter<R, W, std::shared_ptr<T>>
27-
struct Parser<R, W, std::shared_ptr<T>, ProcessorsType> {
35+
struct ParserSharedPtr {
2836
using InputVarType = typename R::InputVarType;
2937

3038
using ParentType = Parent<W>;

include/rfl/parsing/Parser_box.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,20 @@
1616

1717
namespace rfl::parsing {
1818

19+
template <class T>
20+
struct is_box : std::false_type {};
21+
22+
template <class T, Copyability C>
23+
struct is_box<Box<T, C>> : std::true_type {
24+
using element_type = T;
25+
static constexpr Copyability copyability = C;
26+
};
27+
28+
template <class T>
29+
constexpr bool is_box_v = is_box<std::remove_cvref_t<T>>::value;
30+
1931
template <class R, class W, class T, Copyability C, class ProcessorsType>
20-
requires AreReaderAndWriter<R, W, Box<T, C>>
21-
struct Parser<R, W, Box<T, C>, ProcessorsType> {
32+
struct ParserBox {
2233
using InputVarType = typename R::InputVarType;
2334

2435
/**

include/rfl/parsing/Parser_default.hpp

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
#include "../to_view.hpp"
2626
#include "AreReaderAndWriter.hpp"
2727
#include "Parent.hpp"
28+
#include "ParserSharedPtr.hpp"
29+
#include "Parser_box.hpp"
30+
#include "Parser_optional.hpp"
31+
#include "Parser_ref.hpp"
32+
#include "Parser_reference_wrapper.hpp"
33+
#include "Parser_unique_ptr.hpp"
2834
#include "Parser_base.hpp"
2935
#include "call_destructors_where_necessary.hpp"
3036
#include "is_tagged_union_wrapper.hpp"
@@ -52,7 +58,33 @@ struct Parser {
5258
* @return A Result containing the parsed value or an error.
5359
*/
5460
static Result<T> read(const R& _r, const InputVarType& _var) noexcept {
55-
if constexpr (internal::has_read_reflector<T>) {
61+
if constexpr (is_shared_ptr_v<T>) {
62+
return ParserSharedPtr<R, W, typename T::element_type,
63+
ProcessorsType>::read(_r, _var);
64+
65+
} else if constexpr (is_unique_ptr_v<T>) {
66+
return ParserUniquePtr<R, W, typename T::element_type,
67+
ProcessorsType>::read(_r, _var);
68+
69+
} else if constexpr (is_optional_v<T>) {
70+
return ParserOptional<R, W, typename std::remove_cvref_t<T>::value_type,
71+
ProcessorsType>::read(_r, _var);
72+
73+
} else if constexpr (is_box_v<T>) {
74+
using IsBox = is_box<std::remove_cvref_t<T>>;
75+
return ParserBox<R, W, typename IsBox::element_type, IsBox::copyability,
76+
ProcessorsType>::read(_r, _var);
77+
78+
} else if constexpr (is_ref_v<T>) {
79+
using IsRef = is_ref<std::remove_cvref_t<T>>;
80+
return ParserRef<R, W, typename IsRef::element_type,
81+
ProcessorsType>::read(_r, _var);
82+
83+
} else if constexpr (is_reference_wrapper_v<T>) {
84+
return ParserReferenceWrapper<R, W, typename std::remove_cvref_t<T>::type,
85+
ProcessorsType>::read(_r, _var);
86+
87+
} else if constexpr (internal::has_read_reflector<T>) {
5688
const auto wrap_in_t = [](auto&& _named_tuple) -> Result<T> {
5789
try {
5890
using NT = decltype(_named_tuple);
@@ -106,7 +138,33 @@ struct Parser {
106138
*/
107139
template <class P>
108140
static void write(const W& _w, const T& _var, const P& _parent) {
109-
if constexpr (internal::has_write_reflector<T>) {
141+
if constexpr (is_shared_ptr_v<T>) {
142+
ParserSharedPtr<R, W, typename T::element_type, ProcessorsType>::write(
143+
_w, _var, _parent);
144+
145+
} else if constexpr (is_unique_ptr_v<T>) {
146+
ParserUniquePtr<R, W, typename T::element_type, ProcessorsType>::write(
147+
_w, _var, _parent);
148+
149+
} else if constexpr (is_optional_v<T>) {
150+
ParserOptional<R, W, typename std::remove_cvref_t<T>::value_type,
151+
ProcessorsType>::write(_w, _var, _parent);
152+
153+
} else if constexpr (is_box_v<T>) {
154+
using IsBox = is_box<std::remove_cvref_t<T>>;
155+
ParserBox<R, W, typename IsBox::element_type, IsBox::copyability,
156+
ProcessorsType>::write(_w, _var, _parent);
157+
158+
} else if constexpr (is_ref_v<T>) {
159+
using IsRef = is_ref<std::remove_cvref_t<T>>;
160+
ParserRef<R, W, typename IsRef::element_type, ProcessorsType>::write(
161+
_w, _var, _parent);
162+
163+
} else if constexpr (is_reference_wrapper_v<T>) {
164+
ParserReferenceWrapper<R, W, typename std::remove_cvref_t<T>::type,
165+
ProcessorsType>::write(_w, _var, _parent);
166+
167+
} else if constexpr (internal::has_write_reflector<T>) {
110168
Parser<R, W, typename Reflector<T>::ReflType, ProcessorsType>::write(
111169
_w, Reflector<T>::from(_var), _parent);
112170

@@ -144,7 +202,33 @@ struct Parser {
144202
using U = std::remove_cvref_t<T>;
145203
using Type = schema::Type;
146204

147-
if constexpr (rfl::internal::is_description_v<U>) {
205+
if constexpr (is_shared_ptr_v<U>) {
206+
return ParserSharedPtr<R, W, typename U::element_type,
207+
ProcessorsType>::to_schema(_definitions);
208+
209+
} else if constexpr (is_unique_ptr_v<U>) {
210+
return ParserUniquePtr<R, W, typename U::element_type,
211+
ProcessorsType>::to_schema(_definitions);
212+
213+
} else if constexpr (is_optional_v<U>) {
214+
return ParserOptional<R, W, typename U::value_type,
215+
ProcessorsType>::to_schema(_definitions);
216+
217+
} else if constexpr (is_box_v<U>) {
218+
using IsBox = is_box<U>;
219+
return ParserBox<R, W, typename IsBox::element_type, IsBox::copyability,
220+
ProcessorsType>::to_schema(_definitions);
221+
222+
} else if constexpr (is_ref_v<U>) {
223+
using IsRef = is_ref<U>;
224+
return ParserRef<R, W, typename IsRef::element_type,
225+
ProcessorsType>::to_schema(_definitions);
226+
227+
} else if constexpr (is_reference_wrapper_v<U>) {
228+
return ParserReferenceWrapper<R, W, typename U::type,
229+
ProcessorsType>::to_schema(_definitions);
230+
231+
} else if constexpr (rfl::internal::is_description_v<U>) {
148232
return make_description<U>(_definitions);
149233

150234
} else if constexpr (rfl::internal::is_deprecated_v<U>) {

include/rfl/parsing/Parser_optional.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@
1515
#include "schemaful/IsSchemafulWriter.hpp"
1616
#include "schemaful/OptionalReader.hpp"
1717

18-
namespace rfl {
19-
namespace parsing {
18+
namespace rfl::parsing {
19+
20+
template <class T>
21+
struct is_optional : std::false_type {};
22+
23+
template <class T>
24+
struct is_optional<std::optional<T>> : std::true_type {};
25+
26+
template <class T>
27+
constexpr bool is_optional_v = is_optional<std::remove_cvref_t<T>>::value;
2028

2129
template <class R, class W, class T, class ProcessorsType>
22-
requires AreReaderAndWriter<R, W, std::optional<T>>
23-
struct Parser<R, W, std::optional<T>, ProcessorsType> {
30+
struct ParserOptional {
2431
using InputVarType = typename R::InputVarType;
2532

2633
using ParentType = Parent<W>;
@@ -98,7 +105,6 @@ struct Parser<R, W, std::optional<T>, ProcessorsType> {
98105
}
99106
};
100107

101-
} // namespace parsing
102-
} // namespace rfl
108+
} // namespace rfl::parsing
103109

104110
#endif

include/rfl/parsing/Parser_ref.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@
1717

1818
namespace rfl::parsing {
1919

20+
template <class T>
21+
struct is_ref : std::false_type {};
22+
23+
template <class T>
24+
struct is_ref<Ref<T>> : std::true_type {
25+
using element_type = T;
26+
};
27+
28+
template <class T>
29+
constexpr bool is_ref_v = is_ref<std::remove_cvref_t<T>>::value;
30+
2031
template <class R, class W, class T, class ProcessorsType>
21-
requires AreReaderAndWriter<R, W, Ref<T>>
22-
struct Parser<R, W, Ref<T>, ProcessorsType> {
32+
struct ParserRef {
2333
using InputVarType = typename R::InputVarType;
2434

2535
/**

include/rfl/parsing/Parser_reference_wrapper.hpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@
1010
#include "Parser_base.hpp"
1111
#include "schema/Type.hpp"
1212

13-
namespace rfl {
14-
namespace parsing {
13+
namespace rfl::parsing {
14+
15+
template <class T>
16+
struct is_reference_wrapper : std::false_type {};
17+
18+
template <class T>
19+
struct is_reference_wrapper<std::reference_wrapper<T>> : std::true_type {};
20+
21+
template <class T>
22+
constexpr bool is_reference_wrapper_v =
23+
is_reference_wrapper<std::remove_cvref_t<T>>::value;
1524

1625
template <class R, class W, class T, class ProcessorsType>
17-
requires AreReaderAndWriter<R, W, std::reference_wrapper<T>>
18-
struct Parser<R, W, std::reference_wrapper<T>, ProcessorsType> {
26+
struct ParserReferenceWrapper {
1927
using InputVarType = typename R::InputVarType;
2028

2129
/**
@@ -62,7 +70,6 @@ struct Parser<R, W, std::reference_wrapper<T>, ProcessorsType> {
6270
}
6371
};
6472

65-
} // namespace parsing
66-
} // namespace rfl
73+
} // namespace rfl::parsing
6774

6875
#endif

include/rfl/parsing/Parser_unique_ptr.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@
2020
#include "schemaful/IsSchemafulWriter.hpp"
2121
#include "schemaful/UniquePtrReader.hpp"
2222

23-
namespace rfl ::parsing {
23+
namespace rfl::parsing {
24+
25+
template <class T>
26+
struct is_unique_ptr : std::false_type {};
27+
28+
template <class T>
29+
struct is_unique_ptr<std::unique_ptr<T>> : std::true_type {};
30+
31+
template <class T>
32+
constexpr bool is_unique_ptr_v = is_unique_ptr<std::remove_cvref_t<T>>::value;
2433

2534
template <class R, class W, class T, class ProcessorsType>
26-
requires AreReaderAndWriter<R, W, std::unique_ptr<T>>
27-
struct Parser<R, W, std::unique_ptr<T>, ProcessorsType> {
35+
struct ParserUniquePtr {
2836
using InputVarType = typename R::InputVarType;
2937

3038
using ParentType = Parent<W>;

0 commit comments

Comments
 (0)