/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ANDROID_TRAITS_H #define ANDROID_TRAITS_H // ----------------------------------------------------------------------- // Typelists namespace android { // end-of-list marker class NullType {}; // type-list node template struct TypeList { typedef T Head; typedef U Tail; }; // helpers to build typelists #define TYPELIST_1(T1) TypeList #define TYPELIST_2(T1, T2) TypeList #define TYPELIST_3(T1, T2, T3) TypeList #define TYPELIST_4(T1, T2, T3, T4) TypeList // typelists algorithms namespace TL { template struct IndexOf; template struct IndexOf { enum { value = -1 }; }; template struct IndexOf, T> { enum { value = 0 }; }; template struct IndexOf, T> { private: enum { temp = IndexOf::value }; public: enum { value = temp == -1 ? -1 : 1 + temp }; }; }; // namespace TL // type selection based on a boolean template struct Select { typedef T Result; }; template struct Select { typedef U Result; }; // ----------------------------------------------------------------------- // Type traits template class TypeTraits { typedef TYPELIST_4( unsigned char, unsigned short, unsigned int, unsigned long int) UnsignedInts; typedef TYPELIST_4( signed char, signed short, signed int, signed long int) SignedInts; typedef TYPELIST_1( bool) OtherInts; typedef TYPELIST_3( float, double, long double) Floats; template struct PointerTraits { enum { result = false }; typedef NullType PointeeType; }; template struct PointerTraits { enum { result = true }; typedef U PointeeType; }; public: enum { isStdUnsignedInt = TL::IndexOf::value >= 0 }; enum { isStdSignedInt = TL::IndexOf::value >= 0 }; enum { isStdIntegral = TL::IndexOf::value >= 0 || isStdUnsignedInt || isStdSignedInt }; enum { isStdFloat = TL::IndexOf::value >= 0 }; enum { isPointer = PointerTraits::result }; enum { isStdArith = isStdIntegral || isStdFloat }; // best parameter type for given type typedef typename Select::Result ParameterType; }; // ----------------------------------------------------------------------- }; // namespace android #endif /* ANDROID_TRAITS_H */