Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/rfl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
#include "rfl/always_false.hpp"
#include "rfl/apply.hpp"
#include "rfl/as.hpp"
#include "rfl/atomic/is_atomic.hpp"
#include "rfl/atomic/remove_atomic_t.hpp"
#include "rfl/atomic/set_atomic.hpp"
#include "rfl/comparisons.hpp"
#include "rfl/concepts.hpp"
#include "rfl/default.hpp"
Expand Down
131 changes: 131 additions & 0 deletions include/rfl/atomic/is_atomic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#ifndef RFL_ATOMIC_ISATOMIC_HPP_
#define RFL_ATOMIC_ISATOMIC_HPP_

#include <array>
#include <atomic>
#include <type_traits>

#include "../NamedTuple.hpp"
#include "../Tuple.hpp"
#include "../named_tuple_t.hpp"
#include "../to_view.hpp"

namespace rfl::atomic {

template <class T>
struct is_atomic;

template <class T>
struct is_atomic {
static constexpr bool value = false;
using RemoveAtomicT = T;
static void set(RemoveAtomicT&& val, T* _t) { *_t = std::forward<T>(val); };
};

template <class T>
struct is_atomic<std::atomic<T>> {
static constexpr bool value = true;
using RemoveAtomicT = T;
static void set(RemoveAtomicT&& val, std::atomic<T>* _t) {
_t->store(std::forward<RemoveAtomicT>(val), std::memory_order_relaxed);
};
};

template <>
struct is_atomic<std::atomic_flag> {
static constexpr bool value = true;
using RemoveAtomicT = bool;
static void set(RemoveAtomicT&& val, std::atomic_flag* _t) {
if (val) {
_t->test_and_set(std::memory_order_relaxed);
} else {
_t->clear(std::memory_order_relaxed);
}
}
};

template <class T, size_t N>
struct is_atomic<std::array<T, N>> {
using Type = std::remove_cvref_t<T>;

static constexpr bool value = is_atomic<Type>::value;
using RemoveAtomicT = std::array<typename is_atomic<Type>::RemoveAtomicT, N>;
static void set(RemoveAtomicT&& val, std::array<T, N>* _t) {
for (size_t i = 0; i < N; ++i) {
is_atomic<T>::set(
std::forward<typename is_atomic<Type>::RemoveAtomicT>(val[i]),
&((*_t)[i]));
Comment on lines +55 to +57

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There's an inconsistency in the use of T versus Type (which is std::remove_cvref_t<T>). The set function uses is_atomic<T>::set, but T can have cv-qualifiers, which would cause the base template of is_atomic to be instantiated instead of the correct specialization. This can lead to incorrect behavior or compilation errors. You should consistently use Type, which is the cv-ref removed version of T.

      is_atomic<Type>::set(
          std::forward<typename is_atomic<Type>::RemoveAtomicT>(val[i]),
          &((*_t)[i]));

}
}
};

template <class T, size_t N>
struct is_atomic<T[N]> {
using Type = std::remove_cvref_t<T>;

static constexpr bool value = is_atomic<Type>::value;
using RemoveAtomicT = std::array<typename is_atomic<Type>::RemoveAtomicT, N>;
static void set(RemoveAtomicT&& val, T (*_t)[N]) {
for (size_t i = 0; i < N; ++i) {
is_atomic<T>::set(
std::forward<typename is_atomic<Type>::RemoveAtomicT>(val[i]),
&((*_t)[i]));
}
}
};

template <class... Fields>
struct is_atomic<NamedTuple<Fields...>> {
static constexpr bool value =
(is_atomic<typename Fields::Type>::value || ...);

using RemoveAtomicT = NamedTuple<
rfl::Field<Fields::name_,
typename is_atomic<typename Fields::Type>::RemoveAtomicT>...>;

static void set(RemoveAtomicT&& val, NamedTuple<Fields...>* _t) {
(is_atomic<typename Fields::Type>::set(
std::forward<typename is_atomic<
std::remove_cvref_t<typename Fields::Type>>::RemoveAtomicT>(
val.template get<Fields::name_>()),
&(_t->template get<Fields::name_>())),
...);
}
};

template <class T>
requires(std::is_class_v<T> && std::is_aggregate_v<T>)
struct is_atomic<T> {
static constexpr bool value = is_atomic<named_tuple_t<T>>::value;

using RemoveAtomicT = typename is_atomic<named_tuple_t<T>>::RemoveAtomicT;

static void set(RemoveAtomicT&& val, T* _t) {
using Fields = typename named_tuple_t<T>::Fields;

const auto view = to_view(*_t);

const auto set_field = [&]<size_t _i>(std::integral_constant<size_t, _i>) {
using FieldType = typename rfl::tuple_element_t<_i, Fields>::Type;
using FieldRemoveAtomicT =
typename is_atomic<std::remove_cvref_t<FieldType>>::RemoveAtomicT;

is_atomic<std::remove_cvref_t<FieldType>>::set(
std::forward<FieldRemoveAtomicT>(val.template get<_i>()),
view.template get<_i>());
};

constexpr size_t num_fields = std::remove_cvref_t<decltype(view)>::size();

[&]<size_t... _is>(std::index_sequence<_is...>) {
(set_field(std::integral_constant<size_t, _is>{}), ...);
}(std::make_index_sequence<num_fields>{});
}
};

template <class T>
constexpr bool is_atomic_v = is_atomic<std::remove_cvref_t<T>>::value;

} // namespace rfl::atomic

#endif
16 changes: 16 additions & 0 deletions include/rfl/atomic/remove_atomic_t.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef RFL_ATOMIC_REMOVE_ATOMIC_T_HPP_
#define RFL_ATOMIC_REMOVE_ATOMIC_T_HPP_

#include <type_traits>

#include "is_atomic.hpp"

namespace rfl::atomic {

template <class T>
using remove_atomic_t =
typename is_atomic<std::remove_cvref_t<T>>::RemoveAtomicT;

} // namespace rfl::atomic

#endif
24 changes: 24 additions & 0 deletions include/rfl/atomic/set_atomic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef RFL_ATOMIC_SET_ATOMIC_HPP_
#define RFL_ATOMIC_SET_ATOMIC_HPP_

#include <type_traits>

#include "../Result.hpp"
#include "is_atomic.hpp"

namespace rfl::atomic {

template <class T, class U>
Nothing set_atomic(U&& val, T* _t) {
is_atomic<std::remove_cvref_t<T>>::set(std::forward<U>(val), _t);
return Nothing{};
}

template <class T, class U>
Nothing set_atomic(U&& val, T& _t) {
return set_atomic(std::forward<U>(val), &_t);
}

} // namespace rfl::atomic

#endif
2 changes: 2 additions & 0 deletions include/rfl/parsing/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define RFL_PARSING_PARSER_HPP_

#include "Parser_array.hpp"
#include "Parser_atomic.hpp"
#include "Parser_atomic_flag.hpp"
#include "Parser_base.hpp"
#include "Parser_basic_type.hpp"
#include "Parser_box.hpp"
Expand Down
39 changes: 39 additions & 0 deletions include/rfl/parsing/Parser_atomic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef RFL_PARSING_PARSER_ATOMIC_HPP_
#define RFL_PARSING_PARSER_ATOMIC_HPP_

#include <atomic>
#include <map>
#include <type_traits>

#include "../DefaultVal.hpp"
#include "AreReaderAndWriter.hpp"
#include "Parent.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"

namespace rfl::parsing {

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::atomic<T>>
struct Parser<R, W, std::atomic<T>, ProcessorsType> {
using InputVarType = typename R::InputVarType;

/// Read is not supported for atomic types - we must used rfl::atomic instead.

template <class P>
static void write(const W& _w, const std::atomic<T>& _a, const P& _parent) {
Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(
_w, _a.load(std::memory_order_relaxed), _parent);
}

static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions) {
using U = std::remove_cvref_t<T>;
return schema::Type{
Parser<R, W, U, ProcessorsType>::to_schema(_definitions)};
}
};

} // namespace rfl::parsing

#endif
38 changes: 38 additions & 0 deletions include/rfl/parsing/Parser_atomic_flag.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef RFL_PARSING_PARSER_ATOMIC_FLAG_HPP_
#define RFL_PARSING_PARSER_ATOMIC_FLAG_HPP_

#include <atomic>
#include <map>
#include <type_traits>

#include "../DefaultVal.hpp"
#include "AreReaderAndWriter.hpp"
#include "Parent.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"

namespace rfl::parsing {

template <class R, class W, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::atomic_flag>
struct Parser<R, W, std::atomic_flag, ProcessorsType> {
using InputVarType = typename R::InputVarType;

/// Read is not supported for atomic types - we must used rfl::atomic instead.

template <class P>
static void write(const W& _w, const std::atomic_flag& _a, const P& _parent) {
Parser<R, W, bool, ProcessorsType>::write(
_w, _a.test(std::memory_order_relaxed), _parent);
}

static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions) {
return schema::Type{
Parser<R, W, bool, ProcessorsType>::to_schema(_definitions)};
}
};

} // namespace rfl::parsing

#endif
31 changes: 22 additions & 9 deletions include/rfl/parsing/Parser_box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

#include "../Box.hpp"
#include "../Result.hpp"
#include "../atomic/is_atomic.hpp"
#include "../atomic/remove_atomic_t.hpp"
#include "../atomic/set_atomic.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"

namespace rfl {
namespace parsing {
namespace rfl::parsing {

template <class R, class W, class T, Copyability C, class ProcessorsType>
requires AreReaderAndWriter<R, W, Box<T, C>>
Expand All @@ -19,11 +21,23 @@ struct Parser<R, W, Box<T, C>, ProcessorsType> {

static Result<Box<T, C>> read(const R& _r,
const InputVarType& _var) noexcept {
const auto to_box = [](auto&& _t) {
return Box<T, C>::make(std::move(_t));
};
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(_r, _var)
.transform(to_box);
if constexpr (atomic::is_atomic_v<T>) {
using RemoveAtomicT = atomic::remove_atomic_t<T>;
return Parser<R, W, RemoveAtomicT, ProcessorsType>::read(_r, _var)
.transform([](auto&& _t) {
auto atomic_box = Box<T>::make();
atomic::set_atomic(std::move(_t), &(*atomic_box));
return atomic_box;
});

} else {
const auto to_box = [](auto&& _t) {
return Box<T, C>::make(std::move(_t));
};
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(_r,
_var)
.transform(to_box);
}
}

template <class P>
Expand All @@ -39,7 +53,6 @@ struct Parser<R, W, Box<T, C>, ProcessorsType> {
}
};

} // namespace parsing
} // namespace rfl
} // namespace rfl::parsing

#endif
29 changes: 22 additions & 7 deletions include/rfl/parsing/Parser_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,37 @@
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../atomic/is_atomic.hpp"
#include "../atomic/remove_atomic_t.hpp"
#include "../atomic/set_atomic.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"

namespace rfl {
namespace parsing {
namespace rfl::parsing {

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, Ref<T>>
struct Parser<R, W, Ref<T>, ProcessorsType> {
using InputVarType = typename R::InputVarType;

static Result<Ref<T>> read(const R& _r, const InputVarType& _var) noexcept {
const auto to_ref = [&](auto&& _t) { return Ref<T>::make(std::move(_t)); };
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(_r, _var)
.transform(to_ref);
if constexpr (atomic::is_atomic_v<T>) {
using RemoveAtomicT = atomic::remove_atomic_t<T>;
return Parser<R, W, RemoveAtomicT, ProcessorsType>::read(_r, _var)
.transform([](auto&& _t) {
auto atomic_ref = Ref<T>::make();
atomic::set_atomic(std::move(_t), &(*atomic_ref));
return atomic_ref;
});

} else {
const auto to_ref = [&](auto&& _t) {
return Ref<T>::make(std::move(_t));
};
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(_r,
_var)
.transform(to_ref);
}
}

template <class P>
Expand All @@ -37,7 +53,6 @@ struct Parser<R, W, Ref<T>, ProcessorsType> {
}
};

} // namespace parsing
} // namespace rfl
} // namespace rfl::parsing

#endif
Loading
Loading