diff options
Diffstat (limited to 'chilbert/operators.hpp')
-rw-r--r-- | chilbert/operators.hpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/chilbert/operators.hpp b/chilbert/operators.hpp new file mode 100644 index 0000000..efad7f4 --- /dev/null +++ b/chilbert/operators.hpp @@ -0,0 +1,96 @@ +/* + Copyright (C) 2018 David Robillard <d@drobilla.net> + Copyright (C) 2006-2007 Chris Hamilton <chamilton@cs.dal.ca> + + This program is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free Software + Foundation, either version 2 of the License, or (at your option) any later + version. + + This program is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + details. + + You should have received a copy of the GNU General Public License along with + this program. If not, see <https://www.gnu.org/licenses/>. +*/ + +#ifndef CHILBERT_OPERATORS_HPP +#define CHILBERT_OPERATORS_HPP + +#include "chilbert/detail/traits.hpp" + +#include <iostream> +#include <type_traits> + +namespace chilbert { + +using detail::is_bitvec_v; + +template <class T> +std::enable_if_t<is_bitvec_v<T>, T> operator&(const T& lhs, const T& rhs) +{ + T r{lhs}; + r &= rhs; + return r; +} + +template <class T> +std::enable_if_t<is_bitvec_v<T>, T> +operator|(const T& lhs, const T& rhs) +{ + T r{lhs}; + r |= rhs; + return r; +} + +template <class T> +std::enable_if_t<is_bitvec_v<T>, T> +operator^(const T& lhs, const T& rhs) +{ + T r{lhs}; + r ^= rhs; + return r; +} + +template <class T> +std::enable_if_t<is_bitvec_v<T>, T> +operator~(const T& vec) +{ + T r{vec}; + r.flip(); + return r; +} + +template <class T> +std::enable_if_t<is_bitvec_v<T>, T> +operator<<(const T& vec, const size_t bits) +{ + T r{vec}; + r <<= bits; + return r; +} + +template <class T> +std::enable_if_t<is_bitvec_v<T>, T> +operator>>(const T& vec, const size_t bits) +{ + T r{vec}; + r >>= bits; + return r; +} + +template <class T> +inline std::enable_if_t<is_bitvec_v<T>, std::ostream>& +operator<<(std::ostream& os, const T& vec) +{ + for (size_t i = 0; i < vec.size(); ++i) { + os << vec.test(vec.size() - i - 1); + } + return os; +} + +} // namespace chilbert + +#endif |