Eric Fiselier | 257fd69 | 2016-05-07 01:04:55 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | #ifndef SUPPORT_TYPE_ID_H |
| 10 | #define SUPPORT_TYPE_ID_H |
| 11 | |
| 12 | #include <functional> |
| 13 | #include <cassert> |
| 14 | |
| 15 | #include "test_macros.h" |
| 16 | |
| 17 | #if TEST_STD_VER < 11 |
| 18 | #error This header requires C++11 or greater |
| 19 | #endif |
| 20 | |
| 21 | // TypeID - Represent a unique identifier for a type. TypeID allows equality |
| 22 | // comparisons between different types. |
| 23 | struct TypeID { |
| 24 | friend bool operator==(TypeID const& LHS, TypeID const& RHS) |
| 25 | {return LHS.m_id == RHS.m_id; } |
| 26 | friend bool operator!=(TypeID const& LHS, TypeID const& RHS) |
| 27 | {return LHS.m_id != RHS.m_id; } |
| 28 | private: |
| 29 | explicit constexpr TypeID(const int* xid) : m_id(xid) {} |
| 30 | |
| 31 | TypeID(const TypeID&) = delete; |
| 32 | TypeID& operator=(TypeID const&) = delete; |
| 33 | |
| 34 | const int* const m_id; |
| 35 | template <class T> friend TypeID const& makeTypeID(); |
| 36 | |
| 37 | }; |
| 38 | |
| 39 | // makeTypeID - Return the TypeID for the specified type 'T'. |
| 40 | template <class T> |
| 41 | inline TypeID const& makeTypeID() { |
| 42 | static int dummy; |
| 43 | static const TypeID id(&dummy); |
| 44 | return id; |
| 45 | } |
| 46 | |
| 47 | template <class ...Args> |
| 48 | struct ArgumentListID {}; |
| 49 | |
| 50 | // makeArgumentID - Create and return a unique identifier for a given set |
| 51 | // of arguments. |
| 52 | template <class ...Args> |
| 53 | inline TypeID const& makeArgumentID() { |
| 54 | return makeTypeID<ArgumentListID<Args...>>(); |
| 55 | } |
| 56 | |
| 57 | #endif // SUPPORT_TYPE_ID_H |