Skip to content

Commit 4fc5c40

Browse files
Added static_asserts
1 parent 9a57392 commit 4fc5c40

5 files changed

Lines changed: 49 additions & 1 deletion

File tree

include/rfl/internal/has_default_val_v.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#ifndef RFL_HASDEFAULTVALV_HPP_
22
#define RFL_HASDEFAULTVALV_HPP_
3+
4+
#include <array>
35
#include <type_traits>
46

57
#include "../NamedTuple.hpp"
@@ -11,6 +13,11 @@ namespace rfl::internal {
1113
template <class T>
1214
struct HasDefaultVal;
1315

16+
template <class T>
17+
struct HasDefaultVal {
18+
static constexpr bool value = false;
19+
};
20+
1421
template <class... Fields>
1522
struct HasDefaultVal<NamedTuple<Fields...>> {
1623
static constexpr bool value =
@@ -20,7 +27,18 @@ struct HasDefaultVal<NamedTuple<Fields...>> {
2027
};
2128

2229
template <class T>
23-
constexpr bool has_default_val_v = HasDefaultVal<named_tuple_t<T>>::value;
30+
requires(std::is_class_v<T> && std::is_aggregate_v<T>)
31+
struct HasDefaultVal<T> : HasDefaultVal<named_tuple_t<T>> {};
32+
33+
template <class T, size_t N>
34+
struct HasDefaultVal<std::array<T, N>> : HasDefaultVal<std::remove_cvref_t<T>> {
35+
};
36+
37+
template <class T, size_t N>
38+
struct HasDefaultVal<T[N]> : HasDefaultVal<std::remove_cvref_t<T>> {};
39+
40+
template <class T>
41+
constexpr bool has_default_val_v = HasDefaultVal<std::remove_cvref_t<T>>::value;
2442

2543
} // namespace rfl::internal
2644

include/rfl/parsing/Parser_box.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../atomic/is_atomic.hpp"
1010
#include "../atomic/remove_atomic_t.hpp"
1111
#include "../atomic/set_atomic.hpp"
12+
#include "../internal/has_default_val_v.hpp"
1213
#include "Parser_base.hpp"
1314
#include "schema/Type.hpp"
1415

@@ -23,6 +24,12 @@ struct Parser<R, W, Box<T, C>, ProcessorsType> {
2324
const InputVarType& _var) noexcept {
2425
if constexpr (atomic::is_atomic_v<T>) {
2526
using RemoveAtomicT = atomic::remove_atomic_t<T>;
27+
28+
static_assert(!internal::has_default_val_v<RemoveAtomicT>,
29+
"Atomic types cannot be mixed with rfl::DefaultVal");
30+
static_assert(!ProcessorsType::default_if_missing_,
31+
"Atomic types cannot be mixed with rfl::DefaultIfMissing");
32+
2633
return Parser<R, W, RemoveAtomicT, ProcessorsType>::read(_r, _var)
2734
.transform([](auto&& _t) {
2835
auto atomic_box = Box<T>::make();

include/rfl/parsing/Parser_ref.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "../atomic/is_atomic.hpp"
1111
#include "../atomic/remove_atomic_t.hpp"
1212
#include "../atomic/set_atomic.hpp"
13+
#include "../internal/has_default_val_v.hpp"
1314
#include "Parser_base.hpp"
1415
#include "schema/Type.hpp"
1516

@@ -23,6 +24,12 @@ struct Parser<R, W, Ref<T>, ProcessorsType> {
2324
static Result<Ref<T>> read(const R& _r, const InputVarType& _var) noexcept {
2425
if constexpr (atomic::is_atomic_v<T>) {
2526
using RemoveAtomicT = atomic::remove_atomic_t<T>;
27+
28+
static_assert(!internal::has_default_val_v<RemoveAtomicT>,
29+
"Atomic types cannot be mixed with rfl::DefaultVal");
30+
static_assert(!ProcessorsType::default_if_missing_,
31+
"Atomic types cannot be mixed with rfl::DefaultIfMissing");
32+
2633
return Parser<R, W, RemoveAtomicT, ProcessorsType>::read(_r, _var)
2734
.transform([](auto&& _t) {
2835
auto atomic_ref = Ref<T>::make();

include/rfl/parsing/Parser_shared_ptr.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../atomic/is_atomic.hpp"
1212
#include "../atomic/remove_atomic_t.hpp"
1313
#include "../atomic/set_atomic.hpp"
14+
#include "../internal/has_default_val_v.hpp"
1415
#include "Parent.hpp"
1516
#include "Parser_base.hpp"
1617
#include "schema/Type.hpp"
@@ -31,6 +32,12 @@ struct Parser<R, W, std::shared_ptr<T>, ProcessorsType> {
3132
const InputVarType& _var) noexcept {
3233
if constexpr (atomic::is_atomic_v<T>) {
3334
using RemoveAtomicT = std::shared_ptr<atomic::remove_atomic_t<T>>;
35+
36+
static_assert(!internal::has_default_val_v<RemoveAtomicT>,
37+
"Atomic types cannot be mixed with rfl::DefaultVal");
38+
static_assert(!ProcessorsType::default_if_missing_,
39+
"Atomic types cannot be mixed with rfl::DefaultIfMissing");
40+
3441
return Parser<R, W, RemoveAtomicT, ProcessorsType>::read(_r, _var)
3542
.transform([](auto&& _t) {
3643
if (!_t) {
@@ -48,6 +55,7 @@ struct Parser<R, W, std::shared_ptr<T>, ProcessorsType> {
4855
return _r.template read_union<std::shared_ptr<T>, S>(_u);
4956
};
5057
return _r.to_union(_var).and_then(to_shared);
58+
5159
} else {
5260
if (_r.is_empty(_var)) {
5361
return std::shared_ptr<T>();

include/rfl/parsing/Parser_unique_ptr.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../atomic/is_atomic.hpp"
1212
#include "../atomic/remove_atomic_t.hpp"
1313
#include "../atomic/set_atomic.hpp"
14+
#include "../internal/has_default_val_v.hpp"
1415
#include "Parent.hpp"
1516
#include "Parser_base.hpp"
1617
#include "schema/Type.hpp"
@@ -31,6 +32,12 @@ struct Parser<R, W, std::unique_ptr<T>, ProcessorsType> {
3132
const InputVarType& _var) noexcept {
3233
if constexpr (atomic::is_atomic_v<T>) {
3334
using RemoveAtomicT = std::unique_ptr<atomic::remove_atomic_t<T>>;
35+
36+
static_assert(!internal::has_default_val_v<RemoveAtomicT>,
37+
"Atomic types cannot be mixed with rfl::DefaultVal");
38+
static_assert(!ProcessorsType::default_if_missing_,
39+
"Atomic types cannot be mixed with rfl::DefaultIfMissing");
40+
3441
return Parser<R, W, RemoveAtomicT, ProcessorsType>::read(_r, _var)
3542
.transform([](auto&& _t) {
3643
if (!_t) {
@@ -48,6 +55,7 @@ struct Parser<R, W, std::unique_ptr<T>, ProcessorsType> {
4855
return _r.template read_union<std::unique_ptr<T>, S>(_u);
4956
};
5057
return _r.to_union(_var).and_then(to_unique);
58+
5159
} else {
5260
if (_r.is_empty(_var)) {
5361
return std::unique_ptr<T>();

0 commit comments

Comments
 (0)