SMTK
@SMTK_VERSION@
Simulation Modeling Tool Kit
smtk
SharedFromThis.h
Go to the documentation of this file.
1
//=========================================================================
2
// Copyright (c) Kitware, Inc.
3
// All rights reserved.
4
// See LICENSE.txt for details.
5
//
6
// This software is distributed WITHOUT ANY WARRANTY; without even
7
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8
// PURPOSE. See the above copyright notice for more information.
9
//=========================================================================
10
#ifndef smtk_SharedFromThis_h
11
#define smtk_SharedFromThis_h
12
16
#include "smtk/SharedPtr.h"
17
#include "
smtk/common/TypeHierarchy.h
"
18
19
#include <string>
20
#include <unordered_set>
21
#include <vector>
22
24
#define smtkTypedefs(...) \
25
typedef __VA_ARGS__ SelfType; \
26
typedef smtk::shared_ptr<__VA_ARGS__> Ptr; \
27
typedef smtk::shared_ptr<const __VA_ARGS__> ConstPtr; \
28
typedef smtk::weak_ptr<__VA_ARGS__> WeakPtr; \
29
typedef smtk::weak_ptr<const __VA_ARGS__> WeakConstPtr
30
32
#define smtkInheritanceHierarchyBase(...) \
33
virtual smtk::string::Token typeToken() const { return smtk::string::Token(type_name); } \
34
virtual std::vector<smtk::string::Token> classHierarchy() const \
35
{ \
36
static std::vector<smtk::string::Token> baseTypes; \
37
if (baseTypes.empty()) \
38
{ \
39
smtk::common::typeHierarchy<__VA_ARGS__>(baseTypes); \
40
} \
41
return baseTypes; \
42
} \
43
virtual bool matchesType(smtk::string::Token candidate) const \
44
{ \
45
static std::unordered_set<smtk::string::Token> baseTypes; \
46
if (baseTypes.empty()) \
47
{ \
48
smtk::common::typeHierarchy<__VA_ARGS__>(baseTypes); \
49
} \
50
return baseTypes.find(candidate) != baseTypes.end(); \
51
} \
52
std::size_t generationsFromBase(smtk::string::Token base) \
53
{ \
54
std::size_t generations = 0; \
55
for (const auto& entry : this->classHierarchy()) \
56
{ \
57
if (entry == base) \
58
{ \
59
return generations; \
60
} \
61
++generations; \
62
} \
63
return std::string::npos; \
64
}
65
66
#define smtkInheritanceHierarchy(...) \
67
smtk::string::Token typeToken() const override { return smtk::string::Token(type_name); } \
68
std::vector<smtk::string::Token> classHierarchy() const override \
69
{ \
70
static std::vector<smtk::string::Token> baseTypes; \
71
if (baseTypes.empty()) \
72
{ \
73
smtk::common::typeHierarchy<__VA_ARGS__>(baseTypes); \
74
} \
75
return baseTypes; \
76
} \
77
bool matchesType(smtk::string::Token candidate) const override \
78
{ \
79
static std::unordered_set<smtk::string::Token> baseTypes; \
80
if (baseTypes.empty()) \
81
{ \
82
smtk::common::typeHierarchy<__VA_ARGS__>(baseTypes); \
83
} \
84
return baseTypes.find(candidate) != baseTypes.end(); \
85
}
86
88
94
#define smtkTypenameMacroBase(...) \
95
static constexpr const char* const type_name = #__VA_ARGS__; \
96
virtual std::string typeName() const { return type_name; } \
97
smtkInheritanceHierarchyBase(__VA_ARGS__);
98
#define smtkTypenameMacro(...) \
99
static constexpr const char* const type_name = #__VA_ARGS__; \
100
std::string typeName() const override { return type_name; } \
101
smtkInheritanceHierarchy(__VA_ARGS__);
102
105
128
#define smtkTypeMacro(...) \
129
smtkTypedefs(__VA_ARGS__); \
130
smtkTypenameMacro(__VA_ARGS__);
131
#define smtkTypeMacroBase(...) \
132
smtkTypedefs(__VA_ARGS__); \
133
smtkTypenameMacroBase(__VA_ARGS__);
134
147
#define smtkSuperclassMacro(...) \
148
typedef __VA_ARGS__ Superclass; \
149
typedef smtk::shared_ptr<__VA_ARGS__> SuperclassPtr
150
173
#define smtkCreateMacro(...) \
174
static smtk::shared_ptr<SelfType> create() \
175
{ \
176
smtk::shared_ptr<__VA_ARGS__> shared(new SelfType); \
177
return smtk::static_pointer_cast<SelfType>(shared); \
178
} \
179
/* variant for declarative programming: */
\
180
static smtk::shared_ptr<SelfType> create(smtk::shared_ptr<SelfType>& ref) \
181
{ \
182
ref = SelfType::create(); \
183
return ref; \
184
}
185
214
#define smtkEnableSharedPtr(...) \
215
public \
216
smtk::enable_shared_from_this<__VA_ARGS__>
217
250
#define smtkSharedFromThisMacro(...) \
251
typedef __VA_ARGS__ SharedPtrBaseType; \
252
smtk::shared_ptr<SelfType> shared_from_this() \
253
{ \
254
return smtk::static_pointer_cast<SelfType>(SharedPtrBaseType::shared_from_this()); \
255
} \
256
smtk::shared_ptr<const SelfType> shared_from_this() const \
257
{ \
258
return smtk::static_pointer_cast<const SelfType>(SharedPtrBaseType::shared_from_this()); \
259
}
260
267
#define smtkSharedPtrCreateMacro(...) \
268
smtkSharedFromThisMacro(__VA_ARGS__); \
269
smtkCreateMacro(__VA_ARGS__)
270
302
#define smtkSharedPtrHelper(...) \
303
smtk::static_pointer_cast<SelfType>(SharedPtrBaseType::Ptr(__VA_ARGS__))
304
305
#endif // smtk_SharedFromThis_h
TypeHierarchy.h
Compute a class hierarchy using developer-provided type-aliases.
Generated by
1.8.17