Got rid of the reinterpret_casts

This commit is contained in:
Patrick Urbanke
2024-01-03 17:40:42 +01:00
parent fe441a2d9d
commit d6f3fa510d
3 changed files with 38 additions and 80 deletions
-2
View File
@@ -16,8 +16,6 @@ struct Array {
Array() = default;
Array(const StdArrayType &_arr) : arr_(_arr) {}
Array(StdArrayType &&_arr) : arr_(std::move(_arr)) {}
Array(const T &_t) : arr_(reinterpret_cast<const StdArrayType &>(_t)) {}
Array(T &&_t) : arr_(reinterpret_cast<StdArrayType &&>(_t)) {}
~Array() = default;
+38 -11
View File
@@ -83,22 +83,48 @@ auto move_from_pointers(Pointers& _ptrs, Args&&... _args) {
}
}
template <class T, std::size_t n>
auto make_tuple_from_element(T (*arr)[n]) {
if constexpr (std::is_array_v<T>) {
template <class T>
struct is_std_array : public std::false_type {};
template <class T, size_t _n>
struct is_std_array<std::array<T, _n>> : public std::true_type {};
template <class T>
constexpr bool is_std_array_v = is_std_array<T>::value;
template <class T, std::size_t _n>
auto make_tuple_from_element(T (*_arr)[_n]) {
if constexpr (std::is_array_v<T> || is_std_array_v<T>) {
return [&]<std::size_t... i>(std::index_sequence<i...>) {
return std::tuple_cat(make_tuple_from_element(&(*arr)[i])...);
}(std::make_index_sequence<n>());
return std::tuple_cat(make_tuple_from_element(&(*_arr)[i])...);
}
(std::make_index_sequence<_n>());
} else {
return [&]<std::size_t... i>(std::index_sequence<i...>) {
return std::make_tuple(&(*arr)[i]...);
}(std::make_index_sequence<n>());
return std::make_tuple(&(*_arr)[i]...);
}
(std::make_index_sequence<_n>());
}
}
template <class T, std::size_t _n>
auto make_tuple_from_element(std::array<T, _n>* _arr) {
if constexpr (std::is_array_v<T> || is_std_array_v<T>) {
return [&]<std::size_t... i>(std::index_sequence<i...>) {
return std::tuple_cat(make_tuple_from_element(&(*_arr)[i])...);
}
(std::make_index_sequence<_n>());
} else {
return [&]<std::size_t... i>(std::index_sequence<i...>) {
return std::make_tuple(&(*_arr)[i]...);
}
(std::make_index_sequence<_n>());
}
}
template <class T>
auto make_tuple_from_element(Array<T>* arr) {
return make_tuple_from_element(reinterpret_cast<T*>(arr));
auto make_tuple_from_element(Array<T>* _arr) {
return make_tuple_from_element(&(_arr->arr_));
}
auto make_tuple_from_element(auto& e) { return std::make_tuple(e); }
@@ -106,8 +132,9 @@ auto make_tuple_from_element(auto& e) { return std::make_tuple(e); }
auto flatten_c_arrays(const auto& tuple) {
return [&]<std::size_t... i>(std::index_sequence<i...>) {
return std::tuple_cat(make_tuple_from_element(std::get<i>(tuple))...);
}(std::make_index_sequence<
std::tuple_size_v<std::remove_cvref_t<decltype(tuple)>>>());
}
(std::make_index_sequence<
std::tuple_size_v<std::remove_cvref_t<decltype(tuple)>>>());
}
/// Creates a struct of type T from a tuple by moving the underlying
-67
View File
@@ -1,67 +0,0 @@
#ifndef RFL_XML_OUTPUTVALUE_HPP_
#define RFL_XML_OUTPUTVALUE_HPP_
#include <pugixml.hpp>
#include <string>
#include <utility>
#include "../always_false.hpp"
#include "OutputVar.hpp"
namespace rfl {
namespace xml {
template <class T>
class OutputValue : public OutputVar {
public:
static constexpr const char* XML_CONTENT = "xml_content";
public:
OutputValue(T _val, bool _is_attribute)
: is_attribute_(_is_attribute), val_(_val) {}
~OutputValue() = default;
/// Inserts all elements into the builder.
void insert(const std::string& _key, pugi::xml_node* _parent) final {
const auto str = to_string();
if (is_attribute_) {
_parent->append_attribute(_key.c_str()) = str.c_str();
} else if (_key == XML_CONTENT) {
_parent->append_child(pugi::node_pcdata).set_value(str.c_str());
} else {
auto node_child = _parent->append_child(_key.c_str());
node_child.append_child(pugi::node_pcdata).set_value(str.c_str());
}
};
/// Whether this is null.
bool is_null() const final { return false; }
private:
/// Transforms the underlying value into a string.
std::string to_string() {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
return val_;
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
return val_ ? "true" : "false";
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
return std::to_string(val_);
} else {
static_assert(always_false_v<T>, "Unsupported type");
}
}
private:
/// Whether you want this encoded as an attribute.
bool is_attribute_;
/// The underlying value.
T val_;
};
} // namespace xml
} // namespace rfl
#endif