2012-06-14 06:20:47 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-06-15 00:55:11 +00:00
|
|
|
/* Template-based metaprogramming and type-testing facilities. */
|
|
|
|
|
2012-06-14 06:20:47 +00:00
|
|
|
#ifndef mozilla_TypeTraits_h_
|
|
|
|
#define mozilla_TypeTraits_h_
|
|
|
|
|
2013-02-08 21:18:49 +00:00
|
|
|
/*
|
|
|
|
* These traits are approximate copies of the traits and semantics from C++11's
|
|
|
|
* <type_traits> header. Don't add traits not in that header! When all
|
|
|
|
* platforms provide that header, we can convert all users and remove this one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <wchar.h>
|
|
|
|
|
2012-06-14 06:20:47 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.3 Helper classes [meta.help] */
|
2013-03-22 07:14:38 +00:00
|
|
|
|
2012-10-17 21:37:53 +00:00
|
|
|
/**
|
2012-12-16 23:20:17 +00:00
|
|
|
* Helper class used as a base for various type traits, exposed publicly
|
|
|
|
* because <type_traits> exposes it as well.
|
2012-10-17 21:37:53 +00:00
|
|
|
*/
|
2012-12-16 23:20:17 +00:00
|
|
|
template<typename T, T Value>
|
|
|
|
struct IntegralConstant
|
|
|
|
{
|
|
|
|
static const T value = Value;
|
|
|
|
typedef T ValueType;
|
|
|
|
typedef IntegralConstant<T, Value> Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Convenient aliases. */
|
|
|
|
typedef IntegralConstant<bool, true> TrueType;
|
|
|
|
typedef IntegralConstant<bool, false> FalseType;
|
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.4 Unary type traits [meta.unary] */
|
2013-03-22 07:14:38 +00:00
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.4.1 Primary type categories [meta.unary.cat] */
|
2013-03-22 07:14:38 +00:00
|
|
|
|
2013-03-22 07:17:59 +00:00
|
|
|
/**
|
|
|
|
* IsPointer determines whether a type is a pointer type (but not a pointer-to-
|
|
|
|
* member type).
|
|
|
|
*
|
|
|
|
* mozilla::IsPointer<struct S*>::value is true;
|
|
|
|
* mozilla::IsPointer<int**>::value is true;
|
|
|
|
* mozilla::IsPointer<void (*)(void)>::value is true;
|
|
|
|
* mozilla::IsPointer<int>::value is false;
|
|
|
|
* mozilla::IsPointer<struct S>::value is false.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
struct IsPointer : FalseType {};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct IsPointer<T*> : TrueType {};
|
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.4.2 Composite type traits [meta.unary.comp] */
|
2013-03-22 07:14:38 +00:00
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.4.3 Type properties [meta.unary.prop] */
|
2013-03-22 07:14:38 +00:00
|
|
|
|
2013-03-22 07:22:05 +00:00
|
|
|
/**
|
|
|
|
* Traits class for identifying POD types. Until C++11 there's no automatic
|
|
|
|
* way to detect PODs, so for the moment this is done manually. Users may
|
|
|
|
* define specializations of this class that inherit from mozilla::TrueType and
|
|
|
|
* mozilla::FalseType (or equivalently mozilla::IntegralConstant<bool, true or
|
|
|
|
* false>, or conveniently from mozilla::IsPod for composite types) as needed to
|
|
|
|
* ensure correct IsPod behavior.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
struct IsPod : public FalseType {};
|
|
|
|
|
|
|
|
template<> struct IsPod<char> : TrueType {};
|
|
|
|
template<> struct IsPod<signed char> : TrueType {};
|
|
|
|
template<> struct IsPod<unsigned char> : TrueType {};
|
|
|
|
template<> struct IsPod<short> : TrueType {};
|
|
|
|
template<> struct IsPod<unsigned short> : TrueType {};
|
|
|
|
template<> struct IsPod<int> : TrueType {};
|
|
|
|
template<> struct IsPod<unsigned int> : TrueType {};
|
|
|
|
template<> struct IsPod<long> : TrueType {};
|
|
|
|
template<> struct IsPod<unsigned long> : TrueType {};
|
|
|
|
template<> struct IsPod<long long> : TrueType {};
|
|
|
|
template<> struct IsPod<unsigned long long> : TrueType {};
|
|
|
|
template<> struct IsPod<bool> : TrueType {};
|
|
|
|
template<> struct IsPod<float> : TrueType {};
|
|
|
|
template<> struct IsPod<double> : TrueType {};
|
|
|
|
template<> struct IsPod<wchar_t> : TrueType {};
|
|
|
|
template<typename T> struct IsPod<T*> : TrueType {};
|
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.5 Type property queries [meta.unary.prop.query] */
|
2013-03-22 07:14:38 +00:00
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.6 Relationships between types [meta.rel] */
|
2013-03-22 07:14:38 +00:00
|
|
|
|
2013-03-22 07:17:59 +00:00
|
|
|
/**
|
|
|
|
* IsSame tests whether two types are the same type.
|
|
|
|
*
|
|
|
|
* mozilla::IsSame<int, int>::value is true;
|
|
|
|
* mozilla::IsSame<int*, int*>::value is true;
|
|
|
|
* mozilla::IsSame<int, unsigned int>::value is false;
|
|
|
|
* mozilla::IsSame<void, void>::value is true;
|
|
|
|
* mozilla::IsSame<const int, int>::value is false;
|
|
|
|
* mozilla::IsSame<struct S, struct S>::value is true.
|
|
|
|
*/
|
|
|
|
template<typename T, typename U>
|
|
|
|
struct IsSame : FalseType {};
|
2013-03-22 07:14:38 +00:00
|
|
|
|
2013-03-22 07:17:59 +00:00
|
|
|
template<typename T>
|
|
|
|
struct IsSame<T, T> : TrueType {};
|
2013-03-22 07:14:38 +00:00
|
|
|
|
2012-12-16 23:20:17 +00:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
// The trickery used to implement IsBaseOf here makes it possible to use it for
|
|
|
|
// the cases of private and multiple inheritance. This code was inspired by the
|
|
|
|
// sample code here:
|
|
|
|
//
|
|
|
|
// http://stackoverflow.com/questions/2910979/how-is-base-of-works
|
2012-10-17 21:37:53 +00:00
|
|
|
template<class Base, class Derived>
|
2012-12-16 23:20:17 +00:00
|
|
|
struct BaseOfHelper
|
2012-10-17 21:37:53 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
operator Base*() const;
|
|
|
|
operator Derived*();
|
|
|
|
};
|
|
|
|
|
2012-06-15 00:55:11 +00:00
|
|
|
template<class Base, class Derived>
|
2012-12-16 23:20:17 +00:00
|
|
|
struct BaseOfTester
|
2012-06-14 06:20:47 +00:00
|
|
|
{
|
|
|
|
private:
|
2012-10-17 21:37:53 +00:00
|
|
|
template<class T>
|
|
|
|
static char test(Derived*, T);
|
|
|
|
static int test(Base*, int);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static const bool value =
|
2012-12-16 23:20:17 +00:00
|
|
|
sizeof(test(BaseOfHelper<Base, Derived>(), int())) == sizeof(char);
|
2012-10-17 21:37:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class Base, class Derived>
|
2012-12-16 23:20:17 +00:00
|
|
|
struct BaseOfTester<Base, const Derived>
|
2012-10-17 21:37:53 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
template<class T>
|
|
|
|
static char test(Derived*, T);
|
|
|
|
static int test(Base*, int);
|
2012-06-19 20:55:23 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static const bool value =
|
2012-12-16 23:20:17 +00:00
|
|
|
sizeof(test(BaseOfHelper<Base, Derived>(), int())) == sizeof(char);
|
2012-10-17 21:37:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class Base, class Derived>
|
2012-12-16 23:20:17 +00:00
|
|
|
struct BaseOfTester<Base&, Derived&> : FalseType {};
|
2012-10-17 21:37:53 +00:00
|
|
|
|
|
|
|
template<class Type>
|
2012-12-16 23:20:17 +00:00
|
|
|
struct BaseOfTester<Type, Type> : TrueType {};
|
2012-10-17 21:37:53 +00:00
|
|
|
|
|
|
|
template<class Type>
|
2012-12-16 23:20:17 +00:00
|
|
|
struct BaseOfTester<Type, const Type> : TrueType {};
|
|
|
|
|
|
|
|
} /* namespace detail */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* IsBaseOf allows to know whether a given class is derived from another.
|
|
|
|
*
|
|
|
|
* Consider the following class definitions:
|
|
|
|
*
|
|
|
|
* class A {};
|
|
|
|
* class B : public A {};
|
|
|
|
* class C {};
|
|
|
|
*
|
|
|
|
* mozilla::IsBaseOf<A, B>::value is true;
|
|
|
|
* mozilla::IsBaseOf<A, C>::value is false;
|
|
|
|
*/
|
|
|
|
template<class Base, class Derived>
|
|
|
|
struct IsBaseOf
|
2013-03-25 18:11:55 +00:00
|
|
|
: IntegralConstant<bool, detail::BaseOfTester<Base, Derived>::value>
|
2012-12-16 23:20:17 +00:00
|
|
|
{};
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template<typename From, typename To>
|
|
|
|
struct ConvertibleTester
|
2012-10-17 21:37:53 +00:00
|
|
|
{
|
2012-12-16 23:20:17 +00:00
|
|
|
private:
|
|
|
|
static From create();
|
|
|
|
|
|
|
|
template<typename From1, typename To1>
|
|
|
|
static char test(To to);
|
|
|
|
|
|
|
|
template<typename From1, typename To1>
|
|
|
|
static int test(...);
|
|
|
|
|
2012-10-17 21:37:53 +00:00
|
|
|
public:
|
2012-12-16 23:20:17 +00:00
|
|
|
static const bool value =
|
|
|
|
sizeof(test<From, To>(create())) == sizeof(char);
|
2012-06-19 20:55:23 +00:00
|
|
|
};
|
|
|
|
|
2012-12-16 23:20:17 +00:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/**
|
2012-06-19 20:55:23 +00:00
|
|
|
* IsConvertible determines whether a value of type From will implicitly convert
|
|
|
|
* to a value of type To. For example:
|
|
|
|
*
|
|
|
|
* struct A {};
|
|
|
|
* struct B : public A {};
|
|
|
|
* struct C {};
|
|
|
|
*
|
|
|
|
* mozilla::IsConvertible<A, A>::value is true;
|
|
|
|
* mozilla::IsConvertible<A*, A*>::value is true;
|
|
|
|
* mozilla::IsConvertible<B, A>::value is true;
|
|
|
|
* mozilla::IsConvertible<B*, A*>::value is true;
|
|
|
|
* mozilla::IsConvertible<C, A>::value is false;
|
|
|
|
* mozilla::IsConvertible<A, C>::value is false;
|
|
|
|
* mozilla::IsConvertible<A*, C*>::value is false;
|
|
|
|
* mozilla::IsConvertible<C*, A*>::value is false.
|
|
|
|
*
|
|
|
|
* For obscure reasons, you can't use IsConvertible when the types being tested
|
|
|
|
* are related through private inheritance, and you'll get a compile error if
|
|
|
|
* you try. Just don't do it!
|
|
|
|
*/
|
|
|
|
template<typename From, typename To>
|
|
|
|
struct IsConvertible
|
2013-03-25 18:11:55 +00:00
|
|
|
: IntegralConstant<bool, detail::ConvertibleTester<From, To>::value>
|
2012-12-16 23:20:17 +00:00
|
|
|
{};
|
2012-06-14 06:20:47 +00:00
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.7 Transformations between types [meta.trans] */
|
2013-03-22 07:18:58 +00:00
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.7.1 Const-volatile modifications [meta.trans.cv] */
|
2013-03-22 07:18:58 +00:00
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.7.2 Reference modifications [meta.trans.ref] */
|
2013-03-22 07:18:58 +00:00
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.7.3 Sign modifications [meta.trans.sign] */
|
2013-03-22 07:18:58 +00:00
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.7.4 Array modifications [meta.trans.arr] */
|
2013-03-22 07:18:58 +00:00
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.7.5 Pointer modifications [meta.trans.ptr] */
|
2013-03-22 07:18:58 +00:00
|
|
|
|
2013-03-25 19:05:19 +00:00
|
|
|
/* 20.9.7.6 Other transformations [meta.trans.other] */
|
2013-03-22 07:18:58 +00:00
|
|
|
|
2012-12-16 23:20:17 +00:00
|
|
|
/**
|
2012-06-19 02:06:33 +00:00
|
|
|
* EnableIf is a struct containing a typedef of T if and only if B is true.
|
|
|
|
*
|
|
|
|
* mozilla::EnableIf<true, int>::Type is int;
|
|
|
|
* mozilla::EnableIf<false, int>::Type is a compile-time error.
|
|
|
|
*
|
|
|
|
* Use this template to implement SFINAE-style (Substitution Failure Is not An
|
|
|
|
* Error) requirements. For example, you might use it to impose a restriction
|
|
|
|
* on a template parameter:
|
|
|
|
*
|
|
|
|
* template<typename T>
|
|
|
|
* class PodVector // vector optimized to store POD (memcpy-able) types
|
|
|
|
* {
|
2012-12-16 23:20:17 +00:00
|
|
|
* EnableIf<IsPod<T>::value, T>::Type* vector;
|
2012-06-19 02:06:33 +00:00
|
|
|
* size_t length;
|
|
|
|
* ...
|
|
|
|
* };
|
|
|
|
*/
|
|
|
|
template<bool B, typename T = void>
|
|
|
|
struct EnableIf
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct EnableIf<true, T>
|
|
|
|
{
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
|
2013-03-22 07:20:41 +00:00
|
|
|
/**
|
|
|
|
* Conditional selects a class between two, depending on a given boolean value.
|
|
|
|
*
|
|
|
|
* mozilla::Conditional<true, A, B>::Type is A;
|
|
|
|
* mozilla::Conditional<false, A, B>::Type is B;
|
|
|
|
*/
|
|
|
|
template<bool Condition, typename A, typename B>
|
|
|
|
struct Conditional
|
|
|
|
{
|
|
|
|
typedef A Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class A, class B>
|
|
|
|
struct Conditional<false, A, B>
|
|
|
|
{
|
|
|
|
typedef B Type;
|
|
|
|
};
|
|
|
|
|
2012-06-14 06:20:47 +00:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* mozilla_TypeTraits_h_ */
|