From 36c73a778209b02903135663cfd348c87daf2673 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 28 Dec 2019 15:15:40 +0100 Subject: derivation_clause: extract derivation clause type --- include/newtype/derivation_clause.hpp | 114 ++++++++++++++++++++++++++++++++++ include/newtype/deriving.hpp | 102 +----------------------------- 2 files changed, 115 insertions(+), 101 deletions(-) create mode 100644 include/newtype/derivation_clause.hpp diff --git a/include/newtype/derivation_clause.hpp b/include/newtype/derivation_clause.hpp new file mode 100644 index 0000000..6550b4c --- /dev/null +++ b/include/newtype/derivation_clause.hpp @@ -0,0 +1,114 @@ +#ifndef NEWTYPE_DERIVATION_CLAUSE_HPP +#define NEWTYPE_DERIVATION_CLAUSE_HPP + +#include "newtype/derivable.hpp" + +#include + +namespace nt +{ + + /** + * A @p deriving clause type + * + * @tparam DerivableTags A list of tag types defining a set of derivable features + */ + template + struct derivation_clause + { + constexpr derivation_clause(derivable...) noexcept + { + } + + /** + * Check whether the derivation clause contains a given derivable + */ + template + auto constexpr operator()(derivable) const noexcept -> bool + { + return (std::is_same_v || ...); + } + + /** + * Check whether the derivation clause contains all derivables in a given lists + */ + template + auto constexpr operator()(derivable, derivable...) const noexcept -> bool + { + return (*this)(derivable{}) && (*this)(derivable{}...); + } + + /** + * Check whether this derivation clause is less than an other derivation clause + * + * A derivation clause is considered less than an other derivation clause iff. its set of derivables is a strict subset of + * the set derivables of the other. + */ + template + auto constexpr operator<(derivation_clause other) const noexcept -> bool + { + return (sizeof...(DerivableTags) < sizeof...(OtherDerivableTags)) && other(derivable{}...); + } + + /** + * Check whether this derivation clause is greater than an other derivation clause + * + * A derivation clause is considered greater than an other derivation clause iff. its set of derivables is a strict superset + * of the set derivables of the other. + */ + template + auto constexpr operator>(derivation_clause other) const noexcept -> bool + { + return other < *this; + } + + /** + * Check whether this derivation clause is equal to an other derivation clause + * + * Two derivation clauses are considered equal if both have the same set of derivables + */ + template + auto constexpr operator==(derivation_clause other) const noexcept -> bool + { + return sizeof...(DerivableTags) == sizeof...(OtherDerivableTags) && other(derivable{}...); + } + + /** + * Check whether this derivation clause is not equal to an other derivation clause + * + * Two derivation clauses are considered not equal if neither has the same set of derivables as the other + */ + template + auto constexpr operator!=(derivation_clause other) const noexcept -> bool + { + return !(*this == other); + } + + /** + * Check whether this derivation clause is less-than or equal to an other derivation clause + * + * @see nt::distinct::operator== + * @see nt::distinct::operator< + */ + template + auto constexpr operator<=(derivation_clause other) const noexcept -> bool + { + return *this < other || *this == other; + } + + /** + * Check whether this derivation clause is greater-than or equal to an other derivation clause + * + * @see nt::distinct::operator== + * @see nt::distinct::operator< + */ + template + auto constexpr operator>=(derivation_clause other) const noexcept -> bool + { + return *this > other || *this == other; + } + }; + +} // namespace nt + +#endif \ No newline at end of file diff --git a/include/newtype/deriving.hpp b/include/newtype/deriving.hpp index 431742f..7224d55 100644 --- a/include/newtype/deriving.hpp +++ b/include/newtype/deriving.hpp @@ -2,113 +2,13 @@ #define NEWTYPE_DERIVING_HPP #include "newtype/derivable.hpp" +#include "newtype/derivation_clause.hpp" #include namespace nt { - /** - * A @p deriving clause type - * - * @tparam DerivableTags A list of tag types defining a set of derivable features - */ - template - struct derivation_clause - { - constexpr derivation_clause(derivable...) noexcept - { - } - - /** - * Check whether the derivation clause contains a given derivable - */ - template - auto constexpr operator()(derivable) const noexcept -> bool - { - return (std::is_same_v || ...); - } - - /** - * Check whether the derivation clause contains all derivables in a given lists - */ - template - auto constexpr operator()(derivable, derivable...) const noexcept -> bool - { - return (*this)(derivable{}) && (*this)(derivable{}...); - } - - /** - * Check whether this derivation clause is less than an other derivation clause - * - * A derivation clause is considered less than an other derivation clause iff. its set of derivables is a strict subset of - * the set derivables of the other. - */ - template - auto constexpr operator<(derivation_clause other) const noexcept -> bool - { - return (sizeof...(DerivableTags) < sizeof...(OtherDerivableTags)) && other(derivable{}...); - } - - /** - * Check whether this derivation clause is greater than an other derivation clause - * - * A derivation clause is considered greater than an other derivation clause iff. its set of derivables is a strict superset - * of the set derivables of the other. - */ - template - auto constexpr operator>(derivation_clause other) const noexcept -> bool - { - return other < *this; - } - - /** - * Check whether this derivation clause is equal to an other derivation clause - * - * Two derivation clauses are considered equal if both have the same set of derivables - */ - template - auto constexpr operator==(derivation_clause other) const noexcept -> bool - { - return sizeof...(DerivableTags) == sizeof...(OtherDerivableTags) && other(derivable{}...); - } - - /** - * Check whether this derivation clause is not equal to an other derivation clause - * - * Two derivation clauses are considered not equal if neither has the same set of derivables as the other - */ - template - auto constexpr operator!=(derivation_clause other) const noexcept -> bool - { - return !(*this == other); - } - - /** - * Check whether this derivation clause is less-than or equal to an other derivation clause - * - * @see nt::distinct::operator== - * @see nt::distinct::operator< - */ - template - auto constexpr operator<=(derivation_clause other) const noexcept -> bool - { - return *this < other || *this == other; - } - - /** - * Check whether this derivation clause is greater-than or equal to an other derivation clause - * - * @see nt::distinct::operator== - * @see nt::distinct::operator< - */ - template - auto constexpr operator>=(derivation_clause other) const noexcept -> bool - { - return *this > other || *this == other; - } - }; - /** * Create a new derivation clause with the given derivables */ -- cgit v1.2.3