|
| 1 | +// ----------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin |
| 3 | +// Copyright (c) 2016-2021, Knut Reinert & MPI für molekulare Genetik |
| 4 | +// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License |
| 5 | +// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md |
| 6 | +// ----------------------------------------------------------------------------------------------------- |
| 7 | + |
| 8 | +/*!\file |
| 9 | + * \brief Provides seqan3::add_enum_bitwise_operators. |
| 10 | + * \author Hannes Hauswedell <hannes.hauswedell AT fu-berlin.de> |
| 11 | + */ |
| 12 | + |
| 13 | +#pragma once |
| 14 | + |
| 15 | +#include <type_traits> |
| 16 | + |
| 17 | +#include <seqan3/core/platform.hpp> |
| 18 | + |
| 19 | +namespace bio::map_io |
| 20 | +{ |
| 21 | +/*!\interface seqan3::enum_bitwise_operators |
| 22 | + * \brief You can expect these functions on all types that overload seqan3::add_enum_bitwise_operators. |
| 23 | + */ |
| 24 | +/*!\name Requirements for seqan3::enum_bitwise_operators |
| 25 | + * \relates seqan3::enum_bitwise_operators |
| 26 | + * \brief You can expect these member functions. |
| 27 | + * \{ |
| 28 | + * \fn operator&(t lhs, t rhs) |
| 29 | + * \brief Returns the binary `&` operator of lhs and rhs. |
| 30 | + * \param lhs First enum. |
| 31 | + * \param rhs Second enum. |
| 32 | + * |
| 33 | + * \returns the binary conjunction of `lhs` and `rhs`. |
| 34 | + * |
| 35 | + * \fn operator|(t lhs, t rhs) |
| 36 | + * \brief Returns the binary `|` operator of lhs and rhs. |
| 37 | + * \param lhs First enum. |
| 38 | + * \param rhs Second enum. |
| 39 | + * |
| 40 | + * \returns the binary disjunction of `lhs` and `rhs`. |
| 41 | + * |
| 42 | + * \fn operator^(t lhs, t rhs) |
| 43 | + * \brief Returns the binary `^` operator of lhs and rhs. |
| 44 | + * \param lhs First enum. |
| 45 | + * \param rhs Second enum. |
| 46 | + * |
| 47 | + * \returns the binary XOR operation on `lhs` and `rhs`. |
| 48 | + * |
| 49 | + * \fn operator~(t lhs) |
| 50 | + * \brief Returns the binary `~` operator of lhs. |
| 51 | + * \param lhs First enum. |
| 52 | + * |
| 53 | + * \returns the binary NOT operation on `lhs`. |
| 54 | + * |
| 55 | + * \fn operator&=(t & lhs, t rhs) |
| 56 | + * \brief Returns the binary `&=` operator of lhs and rhs. |
| 57 | + * \param lhs First enum. |
| 58 | + * \param rhs Second enum. |
| 59 | + * |
| 60 | + * \returns the binary AND assigment on `lhs`. |
| 61 | + * |
| 62 | + * \fn operator|=(t & lhs, t rhs) |
| 63 | + * \brief Returns the binary `|=` operator of lhs and rhs. |
| 64 | + * \param lhs First enum. |
| 65 | + * \param rhs Second enum. |
| 66 | + * |
| 67 | + * \returns the binary OR assignment on `lhs`. |
| 68 | + * |
| 69 | + * \fn operator^=(t & lhs, t rhs) |
| 70 | + * \brief Returns the binary `^=` operator of lhs and rhs. |
| 71 | + * \param lhs First enum. |
| 72 | + * \param rhs Second enum. |
| 73 | + * |
| 74 | + * \returns the binary XOR assignment on `lhs`. |
| 75 | + * \} |
| 76 | + */ |
| 77 | + |
| 78 | +//!\cond DEV |
| 79 | +/*!\brief Set to true for a scoped enum to have binary operators overloaded. |
| 80 | + * \ingroup core |
| 81 | + * |
| 82 | + * \details |
| 83 | + * |
| 84 | + * If this type trait is specialised for an enum, the binary operators `&`, `|`, `^`, `~`, `&=`, `|=`, `^=` will be |
| 85 | + * added and behave just like for ints or unscoped enums. |
| 86 | + * |
| 87 | + * ### Example |
| 88 | + * |
| 89 | + * \include test/snippet/core/add_enum_bitwise_operators.cpp |
| 90 | + */ |
| 91 | +template <typename t> |
| 92 | +constexpr bool add_enum_bitwise_operators = false; |
| 93 | + |
| 94 | +/*!\name Binary operators for scoped enums |
| 95 | + * \brief Perform binary operations like on ints or weak enums. These overloads are available if |
| 96 | + * seqan3::add_enum_bitwise_operators is defined for your type. |
| 97 | + * \ingroup core |
| 98 | + * |
| 99 | + * \details |
| 100 | + * |
| 101 | + * \see seqan3::add_enum_bitwise_operators |
| 102 | + * \{ |
| 103 | + */ |
| 104 | +template <typename t> |
| 105 | +constexpr t operator&(t lhs, t rhs) noexcept requires std::is_enum_v<t> && add_enum_bitwise_operators<t> |
| 106 | +{ |
| 107 | + return static_cast<t>(static_cast<std::underlying_type_t<t>>(lhs) & static_cast<std::underlying_type_t<t>>(rhs)); |
| 108 | +} |
| 109 | + |
| 110 | +template <typename t> |
| 111 | +constexpr t operator|(t lhs, t rhs) noexcept requires std::is_enum_v<t> && add_enum_bitwise_operators<t> |
| 112 | +{ |
| 113 | + return static_cast<t>(static_cast<std::underlying_type_t<t>>(lhs) | static_cast<std::underlying_type_t<t>>(rhs)); |
| 114 | +} |
| 115 | + |
| 116 | +template <typename t> |
| 117 | +constexpr t operator^(t lhs, t rhs) noexcept requires std::is_enum_v<t> && add_enum_bitwise_operators<t> |
| 118 | +{ |
| 119 | + return static_cast<t>(static_cast<std::underlying_type_t<t>>(lhs) ^ static_cast<std::underlying_type_t<t>>(rhs)); |
| 120 | +} |
| 121 | + |
| 122 | +template <typename t> |
| 123 | +constexpr t operator~(t lhs) noexcept requires std::is_enum_v<t> && add_enum_bitwise_operators<t> |
| 124 | +{ |
| 125 | + return static_cast<t>(~static_cast<std::underlying_type_t<t>>(lhs)); |
| 126 | +} |
| 127 | + |
| 128 | +template <typename t> |
| 129 | +constexpr t & operator&=(t & lhs, t rhs) noexcept requires std::is_enum_v<t> && add_enum_bitwise_operators<t> |
| 130 | +{ |
| 131 | + lhs = lhs & rhs; |
| 132 | + return lhs; |
| 133 | +} |
| 134 | + |
| 135 | +template <typename t> |
| 136 | +constexpr t & operator|=(t & lhs, t rhs) noexcept requires std::is_enum_v<t> && add_enum_bitwise_operators<t> |
| 137 | +{ |
| 138 | + lhs = lhs | rhs; |
| 139 | + return lhs; |
| 140 | +} |
| 141 | + |
| 142 | +template <typename t> |
| 143 | +constexpr t & operator^=(t & lhs, t rhs) noexcept requires std::is_enum_v<t> && add_enum_bitwise_operators<t> |
| 144 | +{ |
| 145 | + lhs = lhs ^ rhs; |
| 146 | + return lhs; |
| 147 | +} |
| 148 | +//!\} |
| 149 | +//!\endcond |
| 150 | + |
| 151 | +} // namespace bio::map_io |
0 commit comments