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 #ifndef SUPPORT_TYPE_ID_H
9 #define SUPPORT_TYPE_ID_H
10 
11 #include <string>
12 #include <cassert>
13 
14 #include "test_macros.h"
15 
16 #if TEST_STD_VER < 11
17 #error This header requires C++11 or greater
18 #endif
19 
20 // TypeID - Represent a unique identifier for a type. TypeID allows equality
21 // comparisons between different types.
22 struct TypeID {
23   friend bool operator==(TypeID const& LHS, TypeID const& RHS)
24   {return LHS.m_id == RHS.m_id; }
25   friend bool operator!=(TypeID const& LHS, TypeID const& RHS)
26   {return LHS.m_id != RHS.m_id; }
27 
nameTypeID28   std::string name() const {
29     return m_id;
30   }
31 
32 private:
TypeIDTypeID33   explicit constexpr TypeID(const char* xid) : m_id(xid) {}
34 
35   TypeID(const TypeID&) = delete;
36   TypeID& operator=(TypeID const&) = delete;
37 
38   const char* const m_id;
39   template <class T> friend TypeID const& makeTypeIDImp();
40 };
41 
42 // makeTypeID - Return the TypeID for the specified type 'T'.
43 template <class T>
makeTypeIDImp()44 inline TypeID const& makeTypeIDImp() {
45 #ifdef _MSC_VER
46   static const TypeID id(__FUNCSIG__);
47 #else
48   static const TypeID id(__PRETTY_FUNCTION__);
49 #endif // _MSC_VER
50   return id;
51 }
52 
53 template <class T>
54 struct TypeWrapper {};
55 
56 template <class T>
makeTypeID()57 inline  TypeID const& makeTypeID() {
58   return makeTypeIDImp<TypeWrapper<T>>();
59 }
60 
61 template <class ...Args>
62 struct ArgumentListID {};
63 
64 // makeArgumentID - Create and return a unique identifier for a given set
65 // of arguments.
66 template <class ...Args>
makeArgumentID()67 inline  TypeID const& makeArgumentID() {
68   return makeTypeIDImp<ArgumentListID<Args...>>();
69 }
70 
71 #endif // SUPPORT_TYPE_ID_H
72