Removed unnecessary boilerplate code

This commit is contained in:
Patrick Urbanke
2023-10-15 17:58:37 +02:00
parent 4c020e7e82
commit 013708b77c
8 changed files with 97 additions and 189 deletions
+7 -12
View File
@@ -20,32 +20,27 @@ class Box {
Box(const Box<T>& _other) = delete;
Box(Box<T>&& _other) : ptr_(std::move(_other.ptr_)) {}
Box(Box<T>&& _other) = default;
~Box() = default;
/// Returns a pointer to the underlying object
inline T* get() const { return ptr_.get(); }
T* get() const { return ptr_.get(); }
/// Copy assignment operator
inline Box<T>& operator=(const Box<T>& _other) = delete;
Box<T>& operator=(const Box<T>& _other) = delete;
/// Move assignment operator
inline Box<T>& operator=(Box<T>&& _other) noexcept {
if (&_other != this) {
ptr_ = std::move(_other.ptr_);
}
return *this;
}
Box<T>& operator=(Box<T>&& _other) noexcept = default;
/// Returns the underlying object.
inline T& operator*() const { return *ptr_; }
T& operator*() const { return *ptr_; }
/// Returns the underlying object.
inline T* operator->() const { return ptr_.get(); }
T* operator->() const { return ptr_.get(); }
/// Returns the underlying unique_ptr
inline const std::unique_ptr<T>& ptr() const { return ptr_; }
const std::unique_ptr<T>& ptr() const { return ptr_; }
private:
/// Only make is allowed to use this constructor.
+17 -28
View File
@@ -26,10 +26,9 @@ struct Field {
Field(Type&& _value) noexcept : value_(std::move(_value)) {}
Field(Field<_name, _Type>&& _field) noexcept
: value_(std::move(_field.value_)) {}
Field(Field<_name, _Type>&& _field) noexcept = default;
Field(const Field<_name, Type>& _field) : value_(_field.get()) {}
Field(const Field<_name, Type>& _field) = default;
template <class T>
Field(const Field<_name, T>& _field) : value_(_field.get()) {}
@@ -61,22 +60,22 @@ struct Field {
constexpr static const internal::StringLiteral name_ = _name;
/// Returns the underlying object.
inline const Type& get() const { return value_; }
const Type& get() const { return value_; }
/// Returns the underlying object.
inline Type& operator()() { return value_; }
Type& operator()() { return value_; }
/// Returns the underlying object.
inline const Type& operator()() const { return value_; }
const Type& operator()() const { return value_; }
/// Assigns the underlying object.
inline auto& operator=(const Type& _value) {
auto& operator=(const Type& _value) {
value_ = _value;
return *this;
}
/// Assigns the underlying object.
inline auto& operator=(Type&& _value) noexcept {
auto& operator=(Type&& _value) noexcept {
value_ = std::move(_value);
return *this;
}
@@ -84,7 +83,7 @@ struct Field {
/// Assigns the underlying object.
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
bool>::type = true>
inline auto& operator=(const T& _value) {
auto& operator=(const T& _value) {
value_ = _value;
return *this;
}
@@ -93,51 +92,41 @@ struct Field {
template <class T = Type,
typename std::enable_if<std::is_default_constructible_v<T>,
bool>::type = true>
inline auto& operator=(const Default& _default) {
auto& operator=(const Default& _default) {
value_ = Type();
return *this;
}
/// Assigns the underlying object.
inline Field<_name, _Type>& operator=(const Field<_name, _Type>& _field) {
if (&_field != this) {
value_ = _field.get();
}
return *this;
}
Field<_name, _Type>& operator=(const Field<_name, _Type>& _field) = default;
/// Assigns the underlying object.
inline Field<_name, _Type>& operator=(Field<_name, _Type>&& _field) {
if (&_field != this) {
value_ = std::move(_field.value_);
}
return *this;
}
Field<_name, _Type>& operator=(Field<_name, _Type>&& _field) = default;
/// Assigns the underlying object.
template <class T>
inline auto& operator=(const Field<_name, T>& _field) {
auto& operator=(const Field<_name, T>& _field) {
value_ = _field.get();
return *this;
}
/// Assigns the underlying object.
template <class T>
inline auto& operator=(Field<_name, T>&& _field) {
auto& operator=(Field<_name, T>&& _field) {
value_ = std::forward<T>(_field.value_);
return *this;
}
/// Assigns the underlying object.
inline void set(const Type& _value) { value_ = _value; }
void set(const Type& _value) { value_ = _value; }
/// Assigns the underlying object.
inline void set(Type&& _value) { value_ = std::move(_value); }
void set(Type&& _value) { value_ = std::move(_value); }
/// Returns the underlying object.
inline Type& value() { return value_; }
Type& value() { return value_; }
/// Returns the underlying object.
inline const Type& value() const { return value_; }
const Type& value() const { return value_; }
/// The underlying value.
Type value_;
+14 -26
View File
@@ -19,10 +19,9 @@ struct Flatten {
Flatten(Type&& _value) noexcept : value_(std::forward<Type>(_value)) {}
Flatten(const Flatten<_Type>& _f) : value_(_f.get()) {}
Flatten(const Flatten<_Type>& _f) = default;
Flatten(Flatten<_Type>&& _f) noexcept
: value_(std::forward<Type>(_f.value_)) {}
Flatten(Flatten<_Type>&& _f) noexcept = default;
template <class T>
Flatten(const Flatten<T>& _f) : value_(_f.get()) {}
@@ -41,22 +40,22 @@ struct Flatten {
~Flatten() = default;
/// Returns the underlying object.
inline const Type& get() const { return value_; }
const Type& get() const { return value_; }
/// Returns the underlying object.
inline Type& operator()() { return value_; }
Type& operator()() { return value_; }
/// Returns the underlying object.
inline const Type& operator()() const { return value_; }
const Type& operator()() const { return value_; }
/// Assigns the underlying object.
inline Flatten<_Type>& operator=(const _Type& _value) {
Flatten<_Type>& operator=(const _Type& _value) {
value_ = _value;
return *this;
}
/// Assigns the underlying object.
inline Flatten<_Type>& operator=(_Type&& _value) {
Flatten<_Type>& operator=(_Type&& _value) {
value_ = std::forward<Type>(_value);
return *this;
}
@@ -64,46 +63,35 @@ struct Flatten {
/// Assigns the underlying object.
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
bool>::type = true>
inline Flatten<_Type>& operator=(const T& _value) {
Flatten<_Type>& operator=(const T& _value) {
value_ = _value;
return *this;
}
/// Assigns the underlying object.
inline Flatten<_Type>& operator=(const Flatten<_Type>& _f) {
if (&_f != this) {
value_ = _f.get();
}
return *this;
}
Flatten<_Type>& operator=(const Flatten<_Type>& _f) = default;
/// Assigns the underlying object.
inline Flatten<_Type>& operator=(Flatten<_Type>&& _f) {
if (&_f != this) {
value_ = std::move(_f.value_);
}
return *this;
}
Flatten<_Type>& operator=(Flatten<_Type>&& _f) = default;
/// Assigns the underlying object.
template <class T>
inline Flatten<_Type>& operator=(const Flatten<T>& _f) {
Flatten<_Type>& operator=(const Flatten<T>& _f) {
value_ = _f.get();
return *this;
}
/// Assigns the underlying object.
template <class T>
inline Flatten<_Type>& operator=(Flatten<T>&& _f) {
Flatten<_Type>& operator=(Flatten<T>&& _f) {
value_ = std::forward<T>(_f);
return *this;
}
/// Assigns the underlying object.
inline void set(const Type& _value) { value_ = _value; }
void set(const Type& _value) { value_ = _value; }
/// Assigns the underlying object.
inline void set(Type&& _value) { value_ = std::forward<Type>(_value); }
void set(Type&& _value) { value_ = std::forward<Type>(_value); }
/// The underlying value.
Type value_;
+23 -40
View File
@@ -34,10 +34,10 @@ class Literal {
static constexpr ValueType num_fields_ = sizeof...(fields_);
/// Constructs a Literal from another literal.
Literal(const Literal<fields_...>& _other) : value_(_other.value_) {}
Literal(const Literal<fields_...>& _other) = default;
/// Constructs a Literal from another literal.
Literal(Literal<fields_...>&& _other) noexcept : value_(_other.value_) {}
Literal(Literal<fields_...>&& _other) noexcept = default;
/// A single-field literal is special because it
/// can also have a default constructor.
@@ -48,20 +48,18 @@ class Literal {
~Literal() = default;
/// Determines whether the literal contains the string.
inline static bool contains(const std::string& _str) {
return has_value(_str);
}
static bool contains(const std::string& _str) { return has_value(_str); }
/// Determines whether the literal contains the string at compile time.
template <internal::StringLiteral _name>
inline static constexpr bool contains() {
static constexpr bool contains() {
return find_value_of<_name>() != -1;
}
/// Determines whether the literal contains any of the strings in the other
/// literal at compile time.
template <class OtherLiteralType, int _i = 0>
inline static constexpr bool contains_any() {
static constexpr bool contains_any() {
if constexpr (_i == num_fields_) {
return false;
} else {
@@ -74,7 +72,7 @@ class Literal {
/// Determines whether the literal contains all of the strings in the other
/// literal at compile time.
template <class OtherLiteralType, int _i = 0, int _n_found = 0>
inline static constexpr bool contains_all() {
static constexpr bool contains_all() {
if constexpr (_i == num_fields_) {
return _n_found == OtherLiteralType::num_fields_;
} else {
@@ -89,9 +87,7 @@ class Literal {
/// Determines whether the literal has duplicate strings at compile time.
/// These is useful for checking collections of strings in other contexts.
inline static constexpr bool has_duplicates() {
return has_duplicate_strings();
}
static constexpr bool has_duplicates() { return has_duplicate_strings(); }
/// Constructs a Literal from a string. Returns an error if the string
/// cannot be found.
@@ -113,26 +109,17 @@ class Literal {
/// Helper function to retrieve a name at compile time.
template <int _value>
inline constexpr static auto name_of() {
constexpr static auto name_of() {
constexpr auto name = find_name_within_own_fields<_value>();
return Literal<name>();
}
/// Assigns from another literal.
Literal<fields_...>& operator=(const Literal<fields_...>& _other) {
if (this != &_other) {
value_ = _other.value_;
}
return *this;
}
Literal<fields_...>& operator=(const Literal<fields_...>& _other) = default;
/// Assigns from another literal.
Literal<fields_...>& operator=(Literal<fields_...>&& _other) noexcept {
if (this != &_other) {
value_ = _other.value_;
}
return *this;
}
Literal<fields_...>& operator=(Literal<fields_...>&& _other) noexcept =
default;
/// Assigns the literal from a string
Literal<fields_...>& operator=(const std::string& _str) {
@@ -141,37 +128,33 @@ class Literal {
}
/// Equality operator other Literals.
inline bool operator==(const Literal<fields_...>& _other) const {
bool operator==(const Literal<fields_...>& _other) const {
return value() == _other.value();
}
/// Equality operator other Literals with different fields.
template <internal::StringLiteral... other_fields>
inline bool operator==(const Literal<other_fields...>& _other) const {
bool operator==(const Literal<other_fields...>& _other) const {
return name() == _other.name();
}
/// Inequality operator for other Literals.
template <internal::StringLiteral... other_fields>
inline bool operator!=(const Literal<other_fields...>& _other) const {
bool operator!=(const Literal<other_fields...>& _other) const {
return !(*this == _other);
}
/// Equality operator for strings.
inline bool operator==(const std::string& _str) const {
return name() == _str;
}
bool operator==(const std::string& _str) const { return name() == _str; }
/// Inequality operator for strings.
inline bool operator!=(const std::string& _str) const {
return name() != _str;
}
bool operator!=(const std::string& _str) const { return name() != _str; }
/// Alias for .name().
std::string str() const { return name(); }
/// Returns the value actually contained in the Literal.
inline ValueType value() const { return value_; }
ValueType value() const { return value_; }
/// Returns the value of the string literal in the template.
template <internal::StringLiteral _name>
@@ -187,7 +170,7 @@ class Literal {
/// Returns all of the allowed fields.
template <int _i = 0>
inline static std::string allowed_strings(const std::string& _values = "") {
static std::string allowed_strings(const std::string& _values = "") {
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
const auto head = "'" + FieldType::field_.str() + "'";
const auto values = _values.size() == 0 ? head : _values + ", " + head;
@@ -200,7 +183,7 @@ class Literal {
/// Whether the Literal contains duplicate strings.
template <int _i = 1>
inline constexpr static bool has_duplicate_strings() {
constexpr static bool has_duplicate_strings() {
if constexpr (_i >= num_fields_) {
return false;
} else {
@@ -209,7 +192,7 @@ class Literal {
}
template <int _i, int _j = _i - 1>
inline constexpr static bool is_duplicate() {
constexpr static bool is_duplicate() {
using FieldType1 = typename std::tuple_element<_i, FieldsType>::type;
using FieldType2 = typename std::tuple_element<_j, FieldsType>::type;
if constexpr (FieldType1::field_ == FieldType2::field_) {
@@ -239,7 +222,7 @@ class Literal {
/// Finds the correct index associated with
/// the string at compile time within the Literal's own fields.
template <int _i>
inline constexpr static auto find_name_within_own_fields() {
constexpr static auto find_name_within_own_fields() {
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
return FieldType::field_;
}
@@ -247,7 +230,7 @@ class Literal {
/// Finds the correct value associated with
/// the string at run time.
template <int _i = 0>
inline static Result<int> find_value(const std::string& _str) {
static Result<int> find_value(const std::string& _str) {
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
if (FieldType::field_.str() == _str) {
return _i;
@@ -276,7 +259,7 @@ class Literal {
/// Whether the literal contains this string.
template <int _i = 0>
inline static bool has_value(const std::string& _str) {
static bool has_value(const std::string& _str) {
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
if (FieldType::field_.str() == _str) {
return true;
+11 -26
View File
@@ -70,16 +70,10 @@ class NamedTuple {
}
/// Copy constructor.
NamedTuple(const NamedTuple<FieldTypes...>& _other)
: values_(_other.values_) {
// Duplicate field checks not required - has already been checked.
}
NamedTuple(const NamedTuple<FieldTypes...>& _other) = default;
/// Move constructor.
NamedTuple(NamedTuple<FieldTypes...>&& _other)
: values_(std::move(_other.values_)) {
// Duplicate field checks not required - has already been checked.
}
NamedTuple(NamedTuple<FieldTypes...>&& _other) = default;
/// Copy constructor.
template <class... OtherFieldTypes>
@@ -175,62 +169,53 @@ class NamedTuple {
/// Gets a field by index.
template <int _index>
inline auto& get() {
auto& get() {
return rfl::get<_index>(*this);
}
/// Gets a field by name.
template <internal::StringLiteral _field_name>
inline auto& get() {
auto& get() {
return rfl::get<_field_name>(*this);
}
/// Gets a field by the field type.
template <class Field>
inline auto& get() {
auto& get() {
return rfl::get<Field>(*this);
}
/// Gets a field by index.
template <int _index>
inline const auto& get() const {
const auto& get() const {
return rfl::get<_index>(*this);
}
/// Gets a field by name.
template <internal::StringLiteral _field_name>
inline const auto& get() const {
const auto& get() const {
return rfl::get<_field_name>(*this);
}
/// Gets a field by the field type.
template <class Field>
inline const auto& get() const {
const auto& get() const {
return rfl::get<Field>(*this);
}
/// Returns the results wrapped in a field.
template <internal::StringLiteral _field_name>
inline auto get_field() const {
auto get_field() const {
return rfl::make_field<_field_name>(rfl::get<_field_name>(*this));
}
/// Copy assignment operator.
NamedTuple<FieldTypes...>& operator=(
const NamedTuple<FieldTypes...>& _other) {
values_ = _other.values_;
return *this;
}
const NamedTuple<FieldTypes...>& _other) = default;
/// Move assignment operator.
NamedTuple<FieldTypes...>& operator=(
NamedTuple<FieldTypes...>&& _other) noexcept {
if (this == &_other) {
return *this;
}
values_ = std::move(_other.values_);
return *this;
}
NamedTuple<FieldTypes...>&& _other) noexcept = default;
/// Replaces one or several fields, returning a new version
/// with the non-replaced fields left unchanged.
+8 -18
View File
@@ -18,9 +18,9 @@ class Ref {
return Ref<T>(std::make_shared<T>(std::forward<Args>(_args)...));
}
Ref(const Ref<T>& _other) : ptr_(_other.ptr_){};
Ref(const Ref<T>& _other) = default;
Ref(Ref<T>&& _other) : ptr_(std::move(_other.ptr_)) {}
Ref(Ref<T>&& _other) = default;
template <class Y>
Ref(const Ref<Y>& _other) : ptr_(_other.ptr()) {}
@@ -28,16 +28,16 @@ class Ref {
~Ref() = default;
/// Returns a pointer to the underlying object
inline T* get() const { return ptr_.get(); }
T* get() const { return ptr_.get(); }
/// Returns the underlying object.
inline T& operator*() const { return *ptr_; }
T& operator*() const { return *ptr_; }
/// Returns the underlying object.
inline T* operator->() const { return ptr_.get(); }
T* operator->() const { return ptr_.get(); }
/// Returns the underlying shared_ptr
inline const std::shared_ptr<T>& ptr() const { return ptr_; }
const std::shared_ptr<T>& ptr() const { return ptr_; }
/// Copy assignment operator.
template <class Y>
@@ -47,20 +47,10 @@ class Ref {
}
/// Move assignment operator
inline Ref<T>& operator=(Ref<T>&& _other) noexcept {
if (&_other != this) {
ptr_ = std::move(_other.ptr_);
}
return *this;
}
Ref<T>& operator=(Ref<T>&& _other) noexcept = default;
/// Copy assignment operator
inline Ref<T>& operator=(const Ref<T>& _other) {
if (&_other != this) {
ptr_ = _other.ptr_;
}
return *this;
}
Ref<T>& operator=(const Ref<T>& _other) = default;
private:
/// Only make is allowed to use this constructor.
+3 -13
View File
@@ -191,25 +191,15 @@ class Result {
const T& operator*() const { return *std::get_if<T>(&t_or_err_); }
/// Assigns the underlying object.
inline Result<T>& operator=(const Result<T>& _other) {
if (this != &_other) {
t_or_err_ = _other.t_or_err_;
}
return *this;
}
Result<T>& operator=(const Result<T>& _other) = default;
/// Assigns the underlying object.
inline Result<T>& operator=(Result<T>&& _other) {
if (this != &_other) {
t_or_err_ = std::move(_other.t_or_err_);
}
return *this;
}
Result<T>& operator=(Result<T>&& _other) = default;
/// Assigns the underlying object.
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
bool>::type = true>
inline auto& operator=(const Result<U>& _other) {
auto& operator=(const Result<U>& _other) {
const auto to_t = [](const U& _u) -> T { return _u; };
t_or_err_ = _other.transform(to_t).t_or_err_;
return *this;
+14 -26
View File
@@ -20,13 +20,11 @@ struct TaggedUnion {
TaggedUnion(VariantType&& _variant) noexcept
: variant_(std::move(_variant)) {}
TaggedUnion(
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tagged_union)
: variant_(_tagged_union.variant_) {}
TaggedUnion(const TaggedUnion<_discriminator, NamedTupleTypes...>&
_tagged_union) = default;
TaggedUnion(TaggedUnion<_discriminator, NamedTupleTypes...>&&
_tagged_union) noexcept
: variant_(std::move(_tagged_union.variant_)) {}
_tagged_union) noexcept = default;
template <class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>,
@@ -41,14 +39,14 @@ struct TaggedUnion {
~TaggedUnion() = default;
/// Assigns the underlying object.
inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(
TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(
const VariantType& _variant) {
variant_ = _variant;
return *this;
}
/// Assigns the underlying object.
inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(
TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(
VariantType&& _variant) {
variant_ = std::move(_variant);
return *this;
@@ -58,8 +56,7 @@ struct TaggedUnion {
template <class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>,
bool>::type = true>
inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(
T&& _variant) {
TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(T&& _variant) {
variant_ = std::forward<T>(_variant);
return *this;
}
@@ -68,35 +65,26 @@ struct TaggedUnion {
template <class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>,
bool>::type = true>
inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(
TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(
const T& _variant) {
variant_ = _variant;
return *this;
}
/// Assigns the underlying object.
inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(
const TaggedUnion<_discriminator, NamedTupleTypes...>& _other) {
if (this != &_other) {
variant_ = _other.get();
}
return *this;
}
TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(
const TaggedUnion<_discriminator, NamedTupleTypes...>& _other) =
default;
/// Assigns the underlying object.
inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(
TaggedUnion<_discriminator, NamedTupleTypes...>&& _other) {
if (this != &_other) {
variant_ = std::move(_other.variant_);
}
return *this;
}
TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(
TaggedUnion<_discriminator, NamedTupleTypes...>&& _other) = default;
/// Returns the underlying variant.
inline VariantType& variant() { return variant_; }
VariantType& variant() { return variant_; }
/// Returns the underlying variant.
inline const VariantType& variant() const { return variant_; }
const VariantType& variant() const { return variant_; }
/// The underlying variant - a TaggedUnion is a thin wrapper
/// around a variant that is mainly used for parsing.