draconisplusplus/include/rfl/Flatten.hpp

113 lines
2.7 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_FLATTEN_HPP_
#define RFL_FLATTEN_HPP_
#include <algorithm>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
namespace rfl {
2024-06-08 14:10:59 -04:00
/// Used to embed another struct into the generated output.
template <class T>
struct Flatten {
/// The underlying type.
using Type = std::remove_cvref_t<T>;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
Flatten(const Type& _value) : value_(_value) {}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
Flatten(Type&& _value) noexcept : value_(std::forward<Type>(_value)) {}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
Flatten(const Flatten<T>& _f) = default;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
Flatten(Flatten<T>&& _f) noexcept = default;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
template <class U>
Flatten(const Flatten<U>& _f) : value_(_f.get()) {}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
template <class U>
Flatten(Flatten<U>&& _f) : value_(_f.get()) {}
2024-05-31 22:59:00 -04:00
2024-06-08 15:53:06 -04:00
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
2024-06-08 14:10:59 -04:00
Flatten(const U& _value) : value_(_value) {}
2024-05-31 22:59:00 -04:00
2024-06-08 15:53:06 -04:00
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
2024-06-08 14:10:59 -04:00
Flatten(U&& _value) : value_(_value) {}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
~Flatten() = default;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Returns the underlying object.
Type& get() { return value_; }
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Returns the underlying object.
const Type& get() const { return value_; }
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Returns the underlying object.
Type& operator()() { return value_; }
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Returns the underlying object.
const Type& operator()() const { return value_; }
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Assigns the underlying object.
Flatten<T>& operator=(const T& _value) {
value_ = _value;
return *this;
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Assigns the underlying object.
Flatten<T>& operator=(T&& _value) {
value_ = std::forward<Type>(_value);
return *this;
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Assigns the underlying object.
2024-06-08 15:53:06 -04:00
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
2024-06-08 14:10:59 -04:00
Flatten<T>& operator=(const U& _value) {
value_ = _value;
return *this;
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Assigns the underlying object.
Flatten<T>& operator=(const Flatten<T>& _f) = default;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Assigns the underlying object.
Flatten<T>& operator=(Flatten<T>&& _f) = default;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Assigns the underlying object.
template <class U>
Flatten<T>& operator=(const Flatten<U>& _f) {
value_ = _f.get();
return *this;
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Assigns the underlying object.
template <class U>
Flatten<T>& operator=(Flatten<U>&& _f) {
value_ = std::forward<U>(_f);
return *this;
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Assigns the underlying object.
void set(const Type& _value) { value_ = _value; }
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Assigns the underlying object.
void set(Type&& _value) { value_ = std::forward<Type>(_value); }
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// The underlying value.
Type value_;
};
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif