diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2019-12-28 15:15:40 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2019-12-28 15:15:40 +0100 |
| commit | 36c73a778209b02903135663cfd348c87daf2673 (patch) | |
| tree | 8858bae91afabb0a821b8cb7f9ef495705edcdc6 | |
| parent | aca0416ba6f13d087348089fb4cc5d4c85b3705f (diff) | |
| download | newtype-36c73a778209b02903135663cfd348c87daf2673.tar.xz newtype-36c73a778209b02903135663cfd348c87daf2673.zip | |
derivation_clause: extract derivation clause type
| -rw-r--r-- | include/newtype/derivation_clause.hpp | 114 | ||||
| -rw-r--r-- | include/newtype/deriving.hpp | 102 |
2 files changed, 115 insertions, 101 deletions
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 <type_traits> + +namespace nt +{ + + /** + * A @p deriving clause type + * + * @tparam DerivableTags A list of tag types defining a set of derivable features + */ + template<typename... DerivableTags> + struct derivation_clause + { + constexpr derivation_clause(derivable<DerivableTags>...) noexcept + { + } + + /** + * Check whether the derivation clause contains a given derivable + */ + template<typename DerivableTag> + auto constexpr operator()(derivable<DerivableTag>) const noexcept -> bool + { + return (std::is_same_v<DerivableTags, DerivableTag> || ...); + } + + /** + * Check whether the derivation clause contains all derivables in a given lists + */ + template<typename DerivableTag, typename... RemainingDerivableTags> + auto constexpr operator()(derivable<DerivableTag>, derivable<RemainingDerivableTags>...) const noexcept -> bool + { + return (*this)(derivable<DerivableTag>{}) && (*this)(derivable<RemainingDerivableTags>{}...); + } + + /** + * 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<typename... OtherDerivableTags> + auto constexpr operator<(derivation_clause<OtherDerivableTags...> other) const noexcept -> bool + { + return (sizeof...(DerivableTags) < sizeof...(OtherDerivableTags)) && other(derivable<DerivableTags>{}...); + } + + /** + * 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<typename... OtherDerivableTags> + auto constexpr operator>(derivation_clause<OtherDerivableTags...> 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<typename... OtherDerivableTags> + auto constexpr operator==(derivation_clause<OtherDerivableTags...> other) const noexcept -> bool + { + return sizeof...(DerivableTags) == sizeof...(OtherDerivableTags) && other(derivable<DerivableTags>{}...); + } + + /** + * 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<typename... OtherDerivableTags> + auto constexpr operator!=(derivation_clause<OtherDerivableTags...> 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<typename... OtherDerivableTags> + auto constexpr operator<=(derivation_clause<OtherDerivableTags...> 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<typename... OtherDerivableTags> + auto constexpr operator>=(derivation_clause<OtherDerivableTags...> 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,6 +2,7 @@ #define NEWTYPE_DERIVING_HPP #include "newtype/derivable.hpp" +#include "newtype/derivation_clause.hpp" #include <type_traits> @@ -9,107 +10,6 @@ namespace nt { /** - * A @p deriving clause type - * - * @tparam DerivableTags A list of tag types defining a set of derivable features - */ - template<typename... DerivableTags> - struct derivation_clause - { - constexpr derivation_clause(derivable<DerivableTags>...) noexcept - { - } - - /** - * Check whether the derivation clause contains a given derivable - */ - template<typename DerivableTag> - auto constexpr operator()(derivable<DerivableTag>) const noexcept -> bool - { - return (std::is_same_v<DerivableTags, DerivableTag> || ...); - } - - /** - * Check whether the derivation clause contains all derivables in a given lists - */ - template<typename DerivableTag, typename... RemainingDerivableTags> - auto constexpr operator()(derivable<DerivableTag>, derivable<RemainingDerivableTags>...) const noexcept -> bool - { - return (*this)(derivable<DerivableTag>{}) && (*this)(derivable<RemainingDerivableTags>{}...); - } - - /** - * 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<typename... OtherDerivableTags> - auto constexpr operator<(derivation_clause<OtherDerivableTags...> other) const noexcept -> bool - { - return (sizeof...(DerivableTags) < sizeof...(OtherDerivableTags)) && other(derivable<DerivableTags>{}...); - } - - /** - * 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<typename... OtherDerivableTags> - auto constexpr operator>(derivation_clause<OtherDerivableTags...> 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<typename... OtherDerivableTags> - auto constexpr operator==(derivation_clause<OtherDerivableTags...> other) const noexcept -> bool - { - return sizeof...(DerivableTags) == sizeof...(OtherDerivableTags) && other(derivable<DerivableTags>{}...); - } - - /** - * 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<typename... OtherDerivableTags> - auto constexpr operator!=(derivation_clause<OtherDerivableTags...> 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<typename... OtherDerivableTags> - auto constexpr operator<=(derivation_clause<OtherDerivableTags...> 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<typename... OtherDerivableTags> - auto constexpr operator>=(derivation_clause<OtherDerivableTags...> other) const noexcept -> bool - { - return *this > other || *this == other; - } - }; - - /** * Create a new derivation clause with the given derivables */ template<typename... DerivableTags> |
