VTK  9.3.20230925
vtkTypeName.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 
4 #ifndef vtkTypeName_h
5 #define vtkTypeName_h
6 
7 #include "vtkCxxABIConfigure.h"
8 #include "vtkStringToken.h" // For tokenized type-name.
9 
10 #include <cstring> // For std::strlen.
11 #include <string> // For return value.
12 #include <typeinfo> // For typeid().
13 
14 #if VTK_HAS_ABI_NAMESPACE
15 #define VTK_TYPENAME_STRINGIFY_INTERNAL(x) #x
16 #define VTK_TYPENAME_STRINGIFY(x) VTK_TYPENAME_STRINGIFY_INTERNAL(x)
17 #define VTK_ABI_NAMESPACE_STRING VTK_TYPENAME_STRINGIFY(VTK_ABI_NAMESPACE_NAME) "::"
18 #endif
19 
20 namespace vtk
21 {
22 namespace detail
23 {
24 VTK_ABI_NAMESPACE_BEGIN
25 
26 template <typename ObjectType>
27 struct Name
28 {
29  inline static std::string value()
30  {
31  std::string result = typeid(ObjectType).name();
32 #ifdef VTK_HAS_CXXABI_DEMANGLE
33  int status = 0;
34  std::size_t size = 0;
35  char* demangledSymbol = abi::__cxa_demangle(result.c_str(), nullptr, &size, &status);
36  if (!status && size > 0)
37  {
38  result = demangledSymbol;
39  }
40  free(demangledSymbol);
41 #endif
42 
43  // Now that we have a (probably) demangled symbol, we need to remove
44  // MSVC-specific cruft from the symbol name.
45 #ifdef VTK_COMPILER_MSVC
46  // MSVC returns a name with "class " or "struct " prepended. Remove it
47  // for consistency with other platforms. Note that template parameters
48  // also include "class " or "struct ", so we must search and replace
49  // repeatedly.
50  for (std::string::size_type pos = result.find("class "); pos != std::string::npos;
51  pos = result.find("class ", pos + 1))
52  {
53  result = result.substr(0, pos) + result.substr(pos + 6);
54  }
55  for (std::string::size_type pos = result.find("struct "); pos != std::string::npos;
56  pos = result.find("struct ", pos + 1))
57  {
58  result = result.substr(0, pos) + result.substr(pos + 7);
59  }
60  // MSVC reports anonymous namespaces like so: `anonymous namespace'
61  // while others report them like so: (anonymous namespace). Fix it
62  // to be consistent.
63  for (std::string::size_type pos = result.find("`anonymous namespace'");
64  pos != std::string::npos; pos = result.find("`anonymous namespace'", pos + 1))
65  {
66  result = result.substr(0, pos) + "(anonymous namespace)" + result.substr(pos + 21);
67  }
68  // MSVC does not include spaces after commas separating template
69  // parameters. Add it in:
70  for (std::string::size_type pos = result.find(','); pos != std::string::npos;
71  pos = result.find(',', pos + 1))
72  {
73  result = result.substr(0, pos) + ", " + result.substr(pos + 1);
74  }
75 #endif
76  // Finally, vtkABINamespace.h provides a namespace name, we will strip it
77  // from the name. This is done to avoid burdening developers with string
78  // processing when linking to multiple versions of VTK.
79 #if VTK_HAS_ABI_NAMESPACE
80  const std::size_t nsLen = std::strlen(VTK_ABI_NAMESPACE_STRING);
81  for (std::string::size_type pos = result.find(VTK_ABI_NAMESPACE_STRING);
82  pos != std::string::npos; pos = result.find(VTK_ABI_NAMESPACE_STRING, pos + 1))
83  {
84  result = result.substr(0, pos) + result.substr(pos + nsLen);
85  }
86 #endif
87  return result;
88  }
89 
96  static inline vtkStringToken::Hash token()
97  {
98  auto nameStr = Name<ObjectType>::value();
99  auto result = vtkStringToken::StringHash(nameStr.c_str(), nameStr.size());
100  return result;
101  }
102 };
103 
104 VTK_ABI_NAMESPACE_END
105 } // namespace detail
106 
107 VTK_ABI_NAMESPACE_BEGIN
115 template <typename ObjectType>
117 {
119 }
120 
131 template <typename ObjectType>
133 {
135 }
136 
137 VTK_ABI_NAMESPACE_END
138 } // namespace vtk
139 
140 #endif // vtkTypeName_h
141 
142 // VTK-HeaderTest-Exclude: vtkTypeName.h
Represent a string by its integer hash.
static constexpr Hash StringHash(const char *data, std::size_t size) noexcept
Return the hash of a string This is used internally but also by the ""_token() literal operator.
std::uint32_t Hash
@ name
Definition: vtkX3D.h:219
@ size
Definition: vtkX3D.h:253
@ string
Definition: vtkX3D.h:490
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
vtkStringToken TypeToken()
Return a string token holding a hash of the demangled type-name of the provided ObjectType.
Definition: vtkTypeName.h:132
std::string TypeName()
Return the demangled type-name of the provided ObjectType.
Definition: vtkTypeName.h:116
static vtkStringToken::Hash token()
Return an integer hash of the ObjectType's typename.
Definition: vtkTypeName.h:96
static std::string value()
Definition: vtkTypeName.h:29