VTK  9.3.0
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 <string> // For return value.
11 #include <typeinfo> // For typeid().
12 
13 namespace vtk
14 {
15 namespace detail
16 {
17 VTK_ABI_NAMESPACE_BEGIN
18 
19 template <typename ObjectType>
20 struct Name
21 {
22  inline static std::string value()
23  {
24  std::string result = typeid(ObjectType).name();
25 #ifdef VTK_HAS_CXXABI_DEMANGLE
26  int status = 0;
27  std::size_t size = 0;
28  char* demangledSymbol = abi::__cxa_demangle(result.c_str(), nullptr, &size, &status);
29  if (!status && size > 0)
30  {
31  result = demangledSymbol;
32  }
33  free(demangledSymbol);
34 #endif
35 
36  // Now that we have a (probably) demangled symbol, we need to remove
37  // MSVC-specific cruft from the symbol name.
38 #ifdef VTK_COMPILER_MSVC
39  // MSVC returns a name with "class " or "struct " prepended. Remove it
40  // for consistency with other platforms. Note that template parameters
41  // also include "class " or "struct ", so we must search and replace
42  // repeatedly.
43  for (std::string::size_type pos = result.find("class "); pos != std::string::npos;
44  pos = result.find("class ", pos + 1))
45  {
46  result = result.substr(0, pos) + result.substr(pos + 6);
47  }
48  for (std::string::size_type pos = result.find("struct "); pos != std::string::npos;
49  pos = result.find("struct ", pos + 1))
50  {
51  result = result.substr(0, pos) + result.substr(pos + 7);
52  }
53  // MSVC reports anonymous namespaces like so: `anonymous namespace'
54  // while others report them like so: (anonymous namespace). Fix it
55  // to be consistent.
56  for (std::string::size_type pos = result.find("`anonymous namespace'");
57  pos != std::string::npos; pos = result.find("`anonymous namespace'", pos + 1))
58  {
59  result = result.substr(0, pos) + "(anonymous namespace)" + result.substr(pos + 21);
60  }
61  // MSVC does not include spaces after commas separating template
62  // parameters. Add it in:
63  for (std::string::size_type pos = result.find(','); pos != std::string::npos;
64  pos = result.find(',', pos + 1))
65  {
66  result = result.substr(0, pos) + ", " + result.substr(pos + 1);
67  }
68 #endif
69  return result;
70  }
71 
78  static inline vtkStringToken::Hash token()
79  {
80  auto nameStr = Name<ObjectType>::value();
81  auto result = vtkStringToken::StringHash(nameStr.c_str(), nameStr.size());
82  return result;
83  }
84 };
85 
86 VTK_ABI_NAMESPACE_END
87 } // namespace detail
88 
89 VTK_ABI_NAMESPACE_BEGIN
97 template <typename ObjectType>
99 {
101 }
102 
113 template <typename ObjectType>
115 {
117 }
118 
119 VTK_ABI_NAMESPACE_END
120 } // namespace vtk
121 
122 #endif // vtkTypeName_h
123 
124 // 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:114
std::string TypeName()
Return the demangled type-name of the provided ObjectType.
Definition: vtkTypeName.h:98
static vtkStringToken::Hash token()
Return an integer hash of the ObjectType's typename.
Definition: vtkTypeName.h:78
static std::string value()
Definition: vtkTypeName.h:22