SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
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"
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 \
34  { \
35  return smtk::string::Token(type_name); \
36  } \
37  virtual std::vector<smtk::string::Token> classHierarchy() const \
38  { \
39  static std::vector<smtk::string::Token> baseTypes; \
40  if (baseTypes.empty()) \
41  { \
42  smtk::common::typeHierarchy<__VA_ARGS__>(baseTypes); \
43  } \
44  return baseTypes; \
45  } \
46  virtual bool matchesType(smtk::string::Token candidate) const \
47  { \
48  if (candidate == "*") \
49  { \
50  return true; \
51  } \
52  static std::unordered_set<smtk::string::Token> baseTypes; \
53  if (baseTypes.empty()) \
54  { \
55  smtk::common::typeHierarchy<__VA_ARGS__>(baseTypes); \
56  } \
57  return baseTypes.find(candidate) != baseTypes.end(); \
58  } \
59  std::size_t generationsFromBase(smtk::string::Token base) \
60  { \
61  std::size_t generations = 0; \
62  for (const auto& entry : this->classHierarchy()) \
63  { \
64  if (entry == base) \
65  { \
66  return generations; \
67  } \
68  ++generations; \
69  } \
70  return std::string::npos; \
71  }
72 
73 #define smtkInheritanceHierarchy(...) \
74  smtk::string::Token typeToken() const override \
75  { \
76  return smtk::string::Token(type_name); \
77  } \
78  std::vector<smtk::string::Token> classHierarchy() const override \
79  { \
80  static std::vector<smtk::string::Token> baseTypes; \
81  if (baseTypes.empty()) \
82  { \
83  smtk::common::typeHierarchy<__VA_ARGS__>(baseTypes); \
84  } \
85  return baseTypes; \
86  } \
87  bool matchesType(smtk::string::Token candidate) const override \
88  { \
89  if (candidate == "*") \
90  { \
91  return true; \
92  } \
93  static std::unordered_set<smtk::string::Token> baseTypes; \
94  if (baseTypes.empty()) \
95  { \
96  smtk::common::typeHierarchy<__VA_ARGS__>(baseTypes); \
97  } \
98  return baseTypes.find(candidate) != baseTypes.end(); \
99  }
100 
102 
108 #define smtkTypenameMacroBase(...) \
109  static constexpr const char* const type_name = #__VA_ARGS__; \
110  virtual std::string typeName() const \
111  { \
112  return type_name; \
113  } \
114  smtkInheritanceHierarchyBase(__VA_ARGS__);
115 #define smtkTypenameMacro(...) \
116  static constexpr const char* const type_name = #__VA_ARGS__; \
117  std::string typeName() const override \
118  { \
119  return type_name; \
120  } \
121  smtkInheritanceHierarchy(__VA_ARGS__);
122 
125 
148 #define smtkTypeMacro(...) \
149  smtkTypedefs(__VA_ARGS__); \
150  smtkTypenameMacro(__VA_ARGS__);
151 #define smtkTypeMacroBase(...) \
152  smtkTypedefs(__VA_ARGS__); \
153  smtkTypenameMacroBase(__VA_ARGS__);
154 
167 #define smtkSuperclassMacro(...) \
168  typedef __VA_ARGS__ Superclass; \
169  typedef smtk::shared_ptr<__VA_ARGS__> SuperclassPtr
170 
193 #define smtkCreateMacro(...) \
194  static smtk::shared_ptr<SelfType> create() \
195  { \
196  smtk::shared_ptr<__VA_ARGS__> shared(new SelfType); \
197  return smtk::static_pointer_cast<SelfType>(shared); \
198  } \
199  /* variant for declarative programming: */ \
200  static smtk::shared_ptr<SelfType> create(smtk::shared_ptr<SelfType>& ref) \
201  { \
202  ref = SelfType::create(); \
203  return ref; \
204  }
205 
234 #define smtkEnableSharedPtr(...) \
235 public \
236  smtk::enable_shared_from_this<__VA_ARGS__>
237 
270 #define smtkSharedFromThisMacro(...) \
271  typedef __VA_ARGS__ SharedPtrBaseType; \
272  smtk::shared_ptr<SelfType> shared_from_this() \
273  { \
274  return smtk::static_pointer_cast<SelfType>(SharedPtrBaseType::shared_from_this()); \
275  } \
276  smtk::shared_ptr<const SelfType> shared_from_this() const \
277  { \
278  return smtk::static_pointer_cast<const SelfType>(SharedPtrBaseType::shared_from_this()); \
279  }
280 
287 #define smtkSharedPtrCreateMacro(...) \
288  smtkSharedFromThisMacro(__VA_ARGS__); \
289  smtkCreateMacro(__VA_ARGS__)
290 
322 #define smtkSharedPtrHelper(...) \
323  smtk::static_pointer_cast<SelfType>(SharedPtrBaseType::Ptr(__VA_ARGS__))
324 
325 #endif // smtk_SharedFromThis_h
TypeHierarchy.h
Compute a class hierarchy using developer-provided type-aliases.