|
- /*!
- * \file operations.h
- * \brief Integral constant operations and logical operations
- *
- * \copyright
- * Copyright (C) 2018 Christos Choutouridis <christos@choutouridis.net>\n
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation, either version 3
- * of the License, or (at your option) any later version.\n
- * 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 Lesser General Public License for more details.\n
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef __utl_meta_operations_h__
- #define __utl_meta_operations_h__
-
- #include <utl/core/impl.h>
- #include <utl/meta/selection.h>
-
-
- /*!
- * \ingroup meta
- * \defgroup logic_operations Logic Operations
- * logic operators and type relations support
- */
- //! @{
- namespace utl {
- namespace meta{
-
- /*!
- * \name Logical relation for types
- */
- //! @{
-
- //! Negate the *bool* constant parameter and return bool_
- template <bool B>
- using not_c = bool_<!B>;
-
- //! negate the bool_ parameter and return bool_
- template<typename Tp>
- using not_ = not_c<Tp::type::value>;
-
- //! \name OR implementation
- //! @{
- namespace details {
- template<typename...> struct _or_;
-
- template<>
- struct _or_<> : false_ { };
-
- template<typename T1>
- struct _or_<T1> : T1 { };
-
- template<typename T1, typename T2>
- struct _or_ <T1, T2>
- : if_<T1, T1, T2> { };
-
- template<typename T1, typename T2, typename T3, typename... Tn>
- struct _or_<T1, T2, T3, Tn...>
- : if_<T1, T1, _or_<T2, T3, Tn...>> { };
- }
-
- //! Operator or for bool_ types
- //! \tparam Ts Variadic args of type bool_
- //! \return Logical or as bool_
- template <typename... Ts>
- using or_ = eval_t<details::_or_<Ts...>>;
- //! @}
-
- //! \name AND implementation
- //! @{
- namespace details {
- template<typename...> struct _and_;
-
- template<>
- struct _and_<>
- : true_ { };
-
- template<typename T1>
- struct _and_ <T1>
- : T1 { };
-
- template<typename T1, typename T2>
- struct _and_<T1, T2>
- : if_<T1, T2, T1> { };
-
- template<typename T1, typename T2, typename T3, typename... Tn>
- struct _and_<T1, T2, T3, Tn...>
- : if_<T1, _and_<T2, T3, Tn...>, T1> { };
- }
-
- //! Operator and for bool_ types
- //! \tparam Ts Variadic args of type bool_
- //! \return Logical and as bool_
- template <typename... Ts>
- using and_ = eval_t<details::_and_<Ts...>>;
- //! @}
-
- //! \name same
- //! @{
- template<typename T1, typename T2>
- struct same_ : false_ { };
-
- template<typename Tp>
- struct same_ <Tp, Tp> : true_ { };
-
- template<typename T1, typename T2>
- using not_same_ = not_<eval_t<same_<T1, T2>>>;
- //! @}
-
- //! @}
- }}
-
- //! @}
-
-
- /*!
- * \ingroup meta
- * \defgroup integral_operators integral operators
- * Type arithmetic and operations
- */
- //! @{
-
- namespace utl {
- namespace meta {
-
- /*!
- * \name Math operations
- */
- //! @{
-
- //! Negation
- template <typename Tp>
- using negate = integral_<decltype(-Tp()), -Tp()>;
- //! Addition
- template <typename Tp1, typename Tp2>
- using add = integral_<
- decltype(Tp1() + Tp2()),
- Tp1() + Tp2()
- >;
- //! Multiplication
- template <typename Tp1, typename Tp2>
- using mult = integral_<
- decltype(Tp2() * Tp2()),
- Tp1() * Tp2()
- >;
- //! Division
- template <typename Tp1, typename Tp2>
- using divide = integral_<
- decltype(Tp2() / Tp2()),
- Tp1() / Tp2()
- >;
- //! Modulo
- template <typename Tp1, typename Tp2>
- using modulo = integral_<
- decltype(Tp1() % Tp2()),
- Tp1() % Tp2()
- >;
- //! Substruction
- template <typename Tp1, typename Tp2>
- using sub = add<Tp1, negate<Tp2>>;
-
- //! Increase
- template <typename Tp>
- using inc = add<Tp, int_<1>>;
-
- //! decrease
- template <typename Tp>
- using dec = add<Tp, int_<-1>>;
-
- //! @}
-
- /*!
- * \name Comparison operations
- */
- //! @{
-
- //! \return a true-valued Integral Constant if Tp1 and Tp2 are equal.
- template <typename Tp1, typename Tp2> using comp_eq = bool_<Tp1() == Tp2()>;
- //! \return a true-valued Integral Constant if Tp1 is less than Tp2.
- template <typename Tp1, typename Tp2> using comp_lt = bool_<(Tp1() < Tp2())>;
-
- //! Not equal
- template <typename Tp1, typename Tp2> using comp_ne = not_<comp_eq<Tp1, Tp2>>;
- //! Greater than
- template <typename Tp1, typename Tp2> using comp_gt = comp_lt <Tp2, Tp1>;
- //! Less or equal
- template <typename Tp1, typename Tp2> using comp_le = not_<comp_lt<Tp2, Tp1>>;
- //! Greater or equal
- template <typename Tp1, typename Tp2> using comp_ge = not_<comp_lt<Tp1, Tp2>>;
- //! @}
-
- /*!
- * \name Bitwise operations
- */
- //! @{
-
- //! \return bitwise not (~) operation of its argument.
- template <typename T> using bitnot_ = integral_<typename T::valueType, (typename T::valueType)(~T())>;
- //! \return bitwise and (&) operation of its arguments
- template <typename Tp1, typename Tp2>
- using bitand_ = integral_<decltype(Tp1() & Tp2()), Tp1() & Tp2()>;
- //! \return bitwise or (|) operation of its arguments.
- template <typename Tp1, typename Tp2>
- using bitor_ = integral_<decltype(Tp1() | Tp2()), Tp1() | Tp2()>;
-
- //! \return bitwise xor (^) operation of its arguments.
- template <typename Tp1, typename Tp2>
- using bitxor_ = integral_<decltype(Tp1() ^ Tp2()), Tp1() ^ Tp2()>;
- //! \return the result of bitwise shift left (<<) operation on Tp.
- template <typename Tp, typename shift>
- using shift_left = integral_<typename Tp::value_type, (typename Tp::value_type)(Tp() << shift())>;
- //! \return the result of bitwise shift right (>>) operation on Tp.
- template <typename Tp, typename shift>
- using shift_right = integral_<typename Tp::value_type, (typename Tp::value_type)(Tp() >> shift())>;
- //! @}
- }}
- //!@}
-
-
- #endif /* __utl_meta_operations_h__ */
|