// Copyright 2020 The Dawn & Tint Authors // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // 3. Neither the name of the copyright holder nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef SRC_TINT_UTILS_TRAITS_TRAITS_H_ #define SRC_TINT_UTILS_TRAITS_TRAITS_H_ #include <ostream> #include <string> #include <tuple> #include <type_traits> #include <utility> namespace tint::traits { /// Convience type definition for std::decay<T>::type Decay; /// NthTypeOf returns the `N`th type in `Types` NthTypeOf; /// Signature describes the signature of a function. template <typename RETURN, typename... PARAMETERS> struct Signature { … }; /// SignatureOf is a traits helper that infers the signature of the function, /// method, static method, lambda, or function-like object `F`. template <typename F> struct SignatureOf { … }; /// SignatureOf specialization for a regular function or static method. SignatureOf<R (*)(ARGS...)>; /// SignatureOf specialization for a non-static method. SignatureOf<R (C::*)(ARGS...)>; /// SignatureOf specialization for a non-static, const method. SignatureOf<R (C::*)(ARGS...) const>; /// SignatureOfT is an alias to `typename SignatureOf<F>::type`. SignatureOfT; /// ParameterType is an alias to `typename SignatureOf<F>::type::parameter<N>`. ParameterType; /// LastParameterType returns the type of the last parameter of `F`. `F` must have at least one /// parameter. LastParameterType; /// ReturnType is an alias to `typename SignatureOf<F>::type::ret`. ReturnType; /// Returns true iff decayed T and decayed U are the same. IsType; /// IsTypeOrDerived<T, BASE> is true iff `T` is of type `BASE`, or derives from /// `BASE`. IsTypeOrDerived; /// If `CONDITION` is true then EnableIf resolves to type T, otherwise an invalid type. EnableIf; /// If `T` is of type `BASE`, or derives from `BASE`, then EnableIfIsType /// resolves to type `T`, otherwise an invalid type. EnableIfIsType; /// @returns the std::index_sequence with all the indices shifted by OFFSET. template <std::size_t OFFSET, std::size_t... INDICES> constexpr auto Shift(std::index_sequence<INDICES...>) { … } /// @returns a std::integer_sequence with the integers `[OFFSET..OFFSET+COUNT)` template <std::size_t OFFSET, std::size_t COUNT> constexpr auto Range() { … } namespace detail { /// @returns the tuple `t` swizzled by `INDICES` template <typename TUPLE, std::size_t... INDICES> constexpr auto Swizzle(TUPLE&& t, std::index_sequence<INDICES...>) -> std::tuple<std::tuple_element_t<INDICES, std::remove_reference_t<TUPLE>>...> { … } /// @returns a nullptr of the tuple type `TUPLE` swizzled by `INDICES`. /// @note: This function is intended to be used in a `decltype()` expression, /// and returns a pointer-to-tuple as the tuple may hold non-constructable /// types. template <typename TUPLE, std::size_t... INDICES> constexpr auto* SwizzlePtrTy(std::index_sequence<INDICES...>) { … } } // namespace detail /// @returns the slice of the tuple `t` with the tuple elements /// `[OFFSET..OFFSET+COUNT)` template <std::size_t OFFSET, std::size_t COUNT, typename TUPLE> constexpr auto Slice(TUPLE&& t) { … } /// Resolves to the slice of the tuple `t` with the tuple elements /// `[OFFSET..OFFSET+COUNT)` SliceTuple; namespace detail { /// Base template for IsTypeIn template <typename T, typename TypeList> struct IsTypeIn; /// Specialization for IsTypeIn IsTypeIn<T, TypeContainer<Ts...>>; } // namespace detail /// Evaluates to true if T is one of the types in the TypeContainer's template arguments. /// Works for std::variant, std::tuple, std::pair, or any typename template where all parameters are /// types. IsTypeIn; /// Evaluates to the decayed pointer element type, or the decayed type T if T is not a pointer. PtrElTy; /// Evaluates to true if `T` decayed is a `std::string`, `std::string_view`, `const char*` or /// `char*` IsStringLike; namespace detail { /// Helper for CharArrayToCharPtr template <typename T> struct CharArrayToCharPtrImpl { … }; /// Specialization of CharArrayToCharPtrImpl for `char[N]` CharArrayToCharPtrImpl<char[N]>; /// Specialization of CharArrayToCharPtrImpl for `const char[N]` CharArrayToCharPtrImpl<const char[N]>; } // namespace detail /// Evaluates to `char*` or `const char*` if `T` is `char[N]` or `const char[N]`, respectively, /// otherwise T. CharArrayToCharPtr; //////////////////////////////////////////////////////////////////////////////// // IsOStream //////////////////////////////////////////////////////////////////////////////// namespace detail { /// Helper for determining whether the type T can be used as a stream writer template <typename T, typename ENABLE = void> struct IsOStream : std::false_type { … }; /// Specialization for types that declare a `static constexpr bool IsStreamWriter` member IsOStream<T, std::void_t<decltype(T::IsStreamWriter)>>; /// Specialization for std::ostream IsOStream<T, std::enable_if_t<std::is_same_v<T, std::ostream>>>; /// Specialization for std::stringstream IsOStream<T, std::enable_if_t<std::is_same_v<T, std::stringstream>>>; } // namespace detail /// Is true if the class T can be treated as an output stream IsOStream; /// If `CONDITION` is true then EnableIfIsOStream resolves to type T, otherwise an invalid type. EnableIfIsOStream; //////////////////////////////////////////////////////////////////////////////// // HasOperatorShiftLeft //////////////////////////////////////////////////////////////////////////////// namespace detail { /// Helper for determining whether the operator<<(LHS, RHS) exists template <typename LHS, typename RHS, typename = void> struct HasOperatorShiftLeft : std::false_type { … }; /// Specialization to detect operator HasOperatorShiftLeft<LHS, RHS, std::void_t<decltype(std::declval<LHS &>() << std::declval<RHS>())>>; } // namespace detail /// Is true if operator<<(LHS, RHS) exists HasOperatorShiftLeft; } // namespace tint::traits #endif // SRC_TINT_UTILS_TRAITS_TRAITS_H_