Added support for clang

This commit is contained in:
Patrick Urbanke
2023-10-09 21:08:05 +02:00
parent c7fe17cf77
commit 745c7ead2c
9 changed files with 61 additions and 79 deletions
+4 -30
View File
@@ -149,17 +149,6 @@ class NamedTuple {
}
}
/// Template specialization for std::tuple, so we can pass fields from other
/// named tuples.
template <class... TupContent, class... Tail>
auto add(const std::tuple<TupContent...>& _tuple, Tail&&... _tail) const {
if constexpr (sizeof...(Tail) > 0) {
return add_tuple(_tuple).add(std::forward<Tail>(_tail)...);
} else {
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple));
}
}
/// Template specialization for NamedTuple, so we can pass fields from other
/// named tuples.
template <class... TupContent, class... Tail>
@@ -178,21 +167,6 @@ class NamedTuple {
std::forward<Tail>(_tail)...);
}
/// Template specialization for NamedTuple, so we can pass fields from other
/// named tuples.
template <class... TupContent, class... Tail>
auto add(const NamedTuple<TupContent...>& _named_tuple, Tail&&... _tail) {
return add(_named_tuple.fields(), std::forward<Tail>(_tail)...);
}
/// Template specialization for NamedTuple, so we can pass fields from other
/// named tuples.
template <class... TupContent, class... Tail>
auto add(const NamedTuple<TupContent...>& _named_tuple,
Tail&&... _tail) const {
return add(_named_tuple.fields(), std::forward<Tail>(_tail)...);
}
/// Returns a tuple containing the fields.
Fields fields() { return make_fields(); }
@@ -345,7 +319,7 @@ class NamedTuple {
template <class... TupContent>
constexpr auto add_tuple(std::tuple<TupContent...>&& _tuple) {
const auto a = [this](auto&&... _fields) {
return add(std::forward<TupContent>(_fields)...);
return this->add(std::forward<TupContent>(_fields)...);
};
return std::apply(a, std::forward<std::tuple<TupContent...>>(_tuple));
}
@@ -355,7 +329,7 @@ class NamedTuple {
template <class... TupContent>
constexpr auto add_tuple(std::tuple<TupContent...>&& _tuple) const {
const auto a = [this](auto&&... _fields) {
return add(std::forward<TupContent>(_fields)...);
return this->add(std::forward<TupContent>(_fields)...);
};
return std::apply(a, std::forward<std::tuple<TupContent...>>(_tuple));
}
@@ -458,7 +432,7 @@ class NamedTuple {
template <class... TupContent, size_t... Is>
auto replace_tuple(std::tuple<TupContent...>&& _tuple) {
const auto r = [this](auto&&... _fields) {
return replace(std::forward<TupContent>(_fields)...);
return this->replace(std::forward<TupContent>(_fields)...);
};
return std::apply(r, std::forward<std::tuple<TupContent...>>(_tuple));
}
@@ -468,7 +442,7 @@ class NamedTuple {
template <class... TupContent, size_t... Is>
auto replace_tuple(std::tuple<TupContent...>&& _tuple) const {
const auto r = [this](auto&&... _fields) {
return replace(std::forward<TupContent>(_fields)...);
return this->replace(std::forward<TupContent>(_fields)...);
};
return std::apply(r, std::forward<std::tuple<TupContent...>>(_tuple));
}
+18 -5
View File
@@ -144,7 +144,7 @@ class Result {
/// them within a std::range.
const T* end() const noexcept {
const auto get_ptr =
[this]<class TOrError>(const TOrError& _t_or_err) -> const T* {
[]<class TOrError>(const TOrError& _t_or_err) -> const T* {
if constexpr (!std::is_same<TOrError, Error>()) {
return &_t_or_err + 1;
} else {
@@ -158,7 +158,7 @@ class Result {
/// or std::nullopt otherwise.
std::optional<Error> error() const noexcept {
const auto get_err =
[this]<class TOrError>(
[]<class TOrError>(
const TOrError& _t_or_err) -> std::optional<Error> {
if constexpr (!std::is_same<TOrError, Error>()) {
return std::nullopt;
@@ -284,11 +284,11 @@ class Result {
/// Returns the value if the result does not contain an error, throws an
/// exceptions if not. Similar to .unwrap() in Rust.
T& value() {
T value() {
const auto handle_variant =
[&]<class TOrError>(TOrError& _t_or_err) -> T& {
[&]<class TOrError>(TOrError& _t_or_err) -> T {
if constexpr (!std::is_same<TOrError, Error>()) {
return _t_or_err;
return std::forward<T>(_t_or_err);
} else {
throw std::runtime_error(_t_or_err.what());
}
@@ -310,6 +310,19 @@ class Result {
return std::visit(handle_variant, t_or_err_);
}
/// Returns the value or a default.
T value_or(const T& _default) noexcept {
const auto handle_variant =
[&]<class TOrError>(const TOrError& _t_or_err) -> T {
if constexpr (!std::is_same<TOrError, Error>()) {
return std::forward<T>(_t_or_err);
} else {
return _default;
}
};
return std::visit(handle_variant, t_or_err_);
}
/// Returns the value or a default.
T value_or(const T& _default) const noexcept {
const auto handle_variant =
@@ -50,7 +50,7 @@ template <class T, class Pointers, class... Args>
auto move_from_pointers(Pointers& _ptrs, Args&&... _args) {
constexpr auto i = sizeof...(Args);
if constexpr (i == std::tuple_size_v<std::decay_t<Pointers>>) {
return T(std::move(_args)...);
return T{std::move(_args)...};
} else {
using FieldType = std::tuple_element_t<i, std::decay_t<Pointers>>;
+3 -18
View File
@@ -18,34 +18,19 @@
namespace rfl {
namespace json {
struct YYJSONOutputArray {
yyjson_mut_val* val_;
};
struct YYJSONOutputObject {
yyjson_mut_val* val_;
};
struct YYJSONOutputVar {
YYJSONOutputVar(yyjson_mut_val* _val) : val_(_val) {}
YYJSONOutputVar(YYJSONOutputArray _arr) : val_(_arr.val_) {}
YYJSONOutputVar(YYJSONOutputObject _obj) : val_(_obj.val_) {}
yyjson_mut_val* val_;
};
struct Reader {
struct YYJSONInputArray {
YYJSONInputArray(yyjson_val* _val) : val_(_val) {}
yyjson_val* val_;
};
struct YYJSONInputObject {
YYJSONInputObject(yyjson_val* _val) : val_(_val) {}
yyjson_val* val_;
};
struct YYJSONInputVar {
YYJSONInputVar(yyjson_val* _val) : val_(_val) {}
yyjson_val* val_;
};
+2
View File
@@ -19,10 +19,12 @@ namespace json {
struct Writer {
struct YYJSONOutputArray {
YYJSONOutputArray(yyjson_mut_val* _val) : val_(_val) {}
yyjson_mut_val* val_;
};
struct YYJSONOutputObject {
YYJSONOutputObject(yyjson_mut_val* _val) : val_(_val) {}
yyjson_mut_val* val_;
};
+21 -13
View File
@@ -221,7 +221,7 @@ struct Parser<ReaderType, WriterType, Box<T>> {
/// Expresses the variables as type T.
static Result<Box<T>> read(const ReaderType& _r,
InputVarType* _var) noexcept {
const auto to_box = [&](auto&& _t) {
const auto to_box = [](auto&& _t) {
return Box<T>::make(std::move(_t));
};
return Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r, _var)
@@ -444,9 +444,7 @@ struct Parser<ReaderType, WriterType, std::optional<T>> {
if (_r.is_empty(_var)) {
return std::optional<T>();
}
const auto to_opt = [&_r](auto&& _t) {
return std::make_optional<T>(_t);
};
const auto to_opt = [](auto&& _t) { return std::make_optional<T>(_t); };
return Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r, _var)
.transform(to_opt);
}
@@ -922,7 +920,8 @@ struct Parser<ReaderType, WriterType, std::variant<FieldTypes...>> {
// ----------------------------------------------------------------------------
/// This can be used for data structures that would be expressed as array in
/// serialized format (std::vector, std::set, std::deque, ...).
/// serialized format (std::vector, std::set, std::deque, ...),
/// but also includes map-like types, when the key is not of type std::string.
template <class ReaderType, class WriterType, class VecType>
struct VectorParser {
public:
@@ -938,23 +937,32 @@ struct VectorParser {
InputVarType* _var) noexcept {
using namespace std::ranges::views;
const auto get_result = [&](auto& _v) {
return std::move(
Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r, &_v)
.value());
const auto get_elem = [&](auto& _v) {
return Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r,
&_v)
.value();
};
const auto to_vec = [&](InputArrayType _arr) -> Result<VecType> {
const auto to_container =
[&](InputArrayType&& _arr) -> Result<VecType> {
auto input_vars = _r.to_vec(&_arr);
auto range = input_vars | transform(get_result);
VecType vec;
for (auto it = input_vars.begin(); it != input_vars.end(); ++it) {
vec.emplace_back(get_elem(*it));
}
return vec;
};
const auto to_result = [&](InputArrayType&& _arr) -> Result<VecType> {
try {
return Result<VecType>(VecType(range.begin(), range.end()));
return Result<VecType>(
to_container(std::forward<InputArrayType>(_arr)));
} catch (std::exception& e) {
return Error(e.what());
}
};
return _r.to_array(_var).and_then(to_vec);
return _r.to_array(_var).and_then(to_result);
}
static OutputVarType write(const WriterType& _w,
+4 -4
View File
@@ -32,10 +32,10 @@ void test_box() {
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
auto node =
DecisionTree::Node{.critical_value = 10.0,
.lesser = rfl::make_box<DecisionTree>(leaf1),
.greater = rfl::make_box<DecisionTree>(leaf2)};
auto node = DecisionTree::Node{
.critical_value = 10.0,
.lesser = rfl::make_box<DecisionTree>(DecisionTree{leaf1}),
.greater = rfl::make_box<DecisionTree>(DecisionTree{leaf2})};
const DecisionTree tree{.leaf_or_node = std::move(node)};
+4 -4
View File
@@ -32,10 +32,10 @@ void test_ref() {
const auto leaf2 = DecisionTree::Leaf{.value = 5.0};
auto node =
DecisionTree::Node{.critical_value = 10.0,
.lesser = rfl::make_ref<DecisionTree>(leaf1),
.greater = rfl::make_ref<DecisionTree>(leaf2)};
auto node = DecisionTree::Node{
.critical_value = 10.0,
.lesser = rfl::make_ref<DecisionTree>(DecisionTree{leaf1}),
.greater = rfl::make_ref<DecisionTree>(DecisionTree{leaf2})};
const DecisionTree tree{.leaf_or_node = std::move(node)};
+4 -4
View File
@@ -32,10 +32,10 @@ void test_unique_ptr2() {
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
auto node =
DecisionTree::Node{.critical_value = 10.0,
.lesser = std::make_unique<DecisionTree>(leaf1),
.greater = std::make_unique<DecisionTree>(leaf2)};
auto node = DecisionTree::Node{
.critical_value = 10.0,
.lesser = std::make_unique<DecisionTree>(DecisionTree{leaf1}),
.greater = std::make_unique<DecisionTree>(DecisionTree{leaf2})};
const DecisionTree tree{.leaf_or_node = std::move(node)};