draconisplusplus/include/rfl/Rename.hpp

152 lines
4 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_RENAME_HPP_
#define RFL_RENAME_HPP_
#include <algorithm>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include "Literal.hpp"
#include "default.hpp"
#include "internal/StringLiteral.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
/// Used to assign a new name to a field, which is different from the name
/// inside the struct.
template <internal::StringLiteral _name, class T>
struct Rename {
/// The underlying type.
using Type = T;
/// The name of the field.
using Name = rfl::Literal<_name>;
Rename() : value_(Type()) {}
Rename(const Type& _value) : value_(_value) {}
Rename(Type&& _value) noexcept : value_(std::move(_value)) {}
Rename(Rename<_name, T>&& _field) noexcept = default;
Rename(const Rename<_name, Type>& _field) = default;
template <class U>
Rename(const Rename<_name, U>& _field) : value_(_field.get()) {}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
template <class U>
Rename(Rename<_name, U>&& _field) : value_(_field.get()) {}
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
Rename(const U& _value) : value_(_value) {}
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
Rename(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
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
Rename(const Rename<_name, U>& _field) : value_(_field.value()) {}
/// Assigns the underlying object to its default value.
2024-06-08 15:53:06 -04:00
template <
class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
type = true>
2024-06-08 14:10:59 -04:00
Rename(const Default& _default) : value_(Type()) {}
~Rename() = default;
/// The name of the field, for internal use.
constexpr static const internal::StringLiteral name_ = _name;
/// Returns the underlying object.
const Type& get() const { return value_; }
/// Returns the underlying object.
Type& operator()() { return value_; }
/// Returns the underlying object.
const Type& operator()() const { return value_; }
/// Assigns the underlying object.
auto& operator=(const Type& _value) {
value_ = _value;
return *this;
}
/// Assigns the underlying object.
auto& operator=(Type&& _value) noexcept {
value_ = std::move(_value);
return *this;
}
/// 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
auto& operator=(const U& _value) {
value_ = _value;
return *this;
}
/// Assigns the underlying object to its default value.
2024-06-08 15:53:06 -04:00
template <
class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
type = true>
2024-06-08 14:10:59 -04:00
auto& operator=(const Default& _default) {
value_ = Type();
return *this;
}
/// Assigns the underlying object.
Rename<_name, T>& operator=(const Rename<_name, T>& _field) = default;
/// Assigns the underlying object.
Rename<_name, T>& operator=(Rename<_name, T>&& _field) = default;
/// Assigns the underlying object.
template <class U>
auto& operator=(const Rename<_name, U>& _field) {
value_ = _field.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>
auto& operator=(Rename<_name, U>&& _field) {
value_ = std::forward<T>(_field.value_);
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::move(_value); }
/// Returns the underlying object.
Type& value() { return value_; }
/// Returns the underlying object.
const Type& value() const { return value_; }
/// The underlying value.
Type value_;
};
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif