xref: /llvm-project/libcxx/test/support/type_id.h (revision 480cd780d63fd9c658cc2f51d0c54416b8b1a5c3)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef SUPPORT_TYPE_ID_H
10 #define SUPPORT_TYPE_ID_H
11 
12 #include <string>
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 
nameTypeID29   std::string name() const {
30     return m_id;
31   }
32 
33 private:
TypeIDTypeID34   explicit constexpr TypeID(const char* xid) : m_id(xid) {}
35 
36   TypeID(const TypeID&) = delete;
37   TypeID& operator=(TypeID const&) = delete;
38 
39   const char* const m_id;
40   template <class T> friend TypeID const& makeTypeIDImp();
41 };
42 
43 // makeTypeID - Return the TypeID for the specified type 'T'.
44 template <class T>
makeTypeIDImp()45 inline TypeID const& makeTypeIDImp() {
46 #ifdef _MSC_VER
47   static const TypeID id(__FUNCSIG__);
48 #else
49   static const TypeID id(__PRETTY_FUNCTION__);
50 #endif // _MSC_VER
51   return id;
52 }
53 
54 template <class T>
55 struct TypeWrapper {};
56 
57 template <class T>
makeTypeID()58 inline  TypeID const& makeTypeID() {
59   return makeTypeIDImp<TypeWrapper<T>>();
60 }
61 
62 template <class ...Args>
63 struct ArgumentListID {};
64 
65 // makeArgumentID - Create and return a unique identifier for a given set
66 // of arguments.
67 template <class ...Args>
makeArgumentID()68 inline  TypeID const& makeArgumentID() {
69   return makeTypeIDImp<ArgumentListID<Args...>>();
70 }
71 
72 #endif // SUPPORT_TYPE_ID_H
73