2012-06-14 10:20:47 +04: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 04:55:11 +04:00
|
|
|
/* Template-based metaprogramming and type-testing facilities. */
|
|
|
|
|
2012-06-14 10:20:47 +04:00
|
|
|
#ifndef mozilla_TypeTraits_h_
|
|
|
|
#define mozilla_TypeTraits_h_
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2012-10-18 01:37:53 +04:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The trickery used to implement IsBaseOf here makes it possible to use it for
|
2012-10-23 01:05:07 +04:00
|
|
|
* the cases of private and multiple inheritance. This code was inspired by the
|
|
|
|
* sample code here:
|
2012-10-18 01:37:53 +04:00
|
|
|
*
|
|
|
|
* http://stackoverflow.com/questions/2910979/how-is-base-of-works
|
|
|
|
*/
|
|
|
|
template<class Base, class Derived>
|
|
|
|
class IsBaseOfHelper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
operator Base*() const;
|
|
|
|
operator Derived*();
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace detail */
|
|
|
|
|
2012-06-14 10:20:47 +04:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
*/
|
2012-06-15 04:55:11 +04:00
|
|
|
template<class Base, class Derived>
|
2012-06-14 10:20:47 +04:00
|
|
|
class IsBaseOf
|
|
|
|
{
|
|
|
|
private:
|
2012-10-18 01:37:53 +04:00
|
|
|
template<class T>
|
|
|
|
static char test(Derived*, T);
|
|
|
|
static int test(Base*, int);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static const bool value =
|
|
|
|
sizeof(test(detail::IsBaseOfHelper<Base, Derived>(), int())) == sizeof(char);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Base, class Derived>
|
|
|
|
class IsBaseOf<Base, const Derived>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
template<class T>
|
|
|
|
static char test(Derived*, T);
|
|
|
|
static int test(Base*, int);
|
2012-06-20 00:55:23 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
static const bool value =
|
2012-10-18 01:37:53 +04:00
|
|
|
sizeof(test(detail::IsBaseOfHelper<Base, Derived>(), int())) == sizeof(char);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Base, class Derived>
|
|
|
|
class IsBaseOf<Base&, Derived&>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static const bool value = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Type>
|
|
|
|
class IsBaseOf<Type, Type>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Type>
|
|
|
|
class IsBaseOf<Type, const Type>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static const bool value = true;
|
2012-06-20 00:55:23 +04: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
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
static From create();
|
|
|
|
|
|
|
|
template<typename From1, typename To1>
|
|
|
|
static char test(To to);
|
|
|
|
|
|
|
|
template<typename From1, typename To1>
|
|
|
|
static int test(...);
|
|
|
|
|
2012-06-14 10:20:47 +04:00
|
|
|
public:
|
2012-06-20 00:55:23 +04:00
|
|
|
static const bool value =
|
|
|
|
sizeof(test<From, To>(create())) == sizeof(char);
|
2012-06-14 10:20:47 +04: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;
|
|
|
|
*/
|
2012-06-15 04:55:11 +04:00
|
|
|
template<bool condition, class A, class B>
|
2012-06-14 10:20:47 +04:00
|
|
|
struct Conditional
|
|
|
|
{
|
|
|
|
typedef A Type;
|
|
|
|
};
|
|
|
|
|
2012-06-15 04:55:11 +04:00
|
|
|
template<class A, class B>
|
2012-06-14 10:20:47 +04:00
|
|
|
struct Conditional<false, A, B>
|
|
|
|
{
|
|
|
|
typedef B Type;
|
|
|
|
};
|
|
|
|
|
2012-06-19 06:06:33 +04: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
|
|
|
|
* {
|
|
|
|
* EnableIf<IsPodType<T>, T>::Type* vector;
|
|
|
|
* size_t length;
|
|
|
|
* ...
|
|
|
|
* };
|
|
|
|
*/
|
|
|
|
template<bool B, typename T = void>
|
|
|
|
struct EnableIf
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct EnableIf<true, T>
|
|
|
|
{
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
|
2012-06-14 10:20:47 +04:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* mozilla_TypeTraits_h_ */
|