VTK: Common/Core/vtkTypeName.h Source File

| VTK 9.3.1

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>
98 inline std::string TypeName()
99 {
100 return detail::Name<ObjectType>::value();
101 }
102
113 template <typename ObjectType>
114 inline vtkStringToken TypeToken()
115 {
116 return vtkStringToken(detail::Name<ObjectType>::token());
117 }
118
119 VTK_ABI_NAMESPACE_END
120 } // namespace vtk
121
122 #endif // vtkTypeName_h
123
124 // VTK-HeaderTest-Exclude: vtkTypeName.h
vtkStringToken
Represent a string by its integer hash.
Definition: vtkStringToken.h:34
vtkStringToken::StringHash
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.
Definition: vtkStringToken.h:67
vtkStringToken::Hash
std::uint32_t Hash
Definition: vtkStringToken.h:36
detail
Definition: vtkCellTreeLocator.h:59
vtkX3D::name
@ name
Definition: vtkX3D.h:219
vtkX3D::size
@ size
Definition: vtkX3D.h:253
vtkX3D::string
@ string
Definition: vtkX3D.h:490
vtk
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
Definition: vtkSMPThreadLocalAPI.h:30
vtk::TypeToken
vtkStringToken TypeToken()
Return a string token holding a hash of the demangled type-name of the provided ObjectType.
Definition: vtkTypeName.h:114
vtk::TypeName
std::string TypeName()
Return the demangled type-name of the provided ObjectType.
Definition: vtkTypeName.h:98
vtk::detail::Name
Definition: vtkTypeName.h:21
vtk::detail::Name::token
static vtkStringToken::Hash token()
Return an integer hash of the ObjectType's typename.
Definition: vtkTypeName.h:78
vtk::detail::Name::value
static std::string value()
Definition: vtkTypeName.h:22
vtkStringToken.h