SMTK
@SMTK_VERSION@
Simulation Modeling Tool Kit
|
Macros for dealing with shared-pointer classes. More...
#include "smtk/SharedPtr.h"
#include "smtk/common/TypeHierarchy.h"
#include <string>
#include <unordered_set>
#include <vector>
Go to the source code of this file.
Macros | |
#define | smtkTypedefs(...) |
Used by smtkTypeMacro() More... | |
#define | smtkInheritanceHierarchyBase(...) |
Used by smtkTypeMacro and smtkTypeMacroBase to provide access to the inheritance hierarchy. | |
#define | smtkInheritanceHierarchy(...) |
#define | smtkSuperclassMacro(...) |
Add a typedef to the superclass of this class. More... | |
#define | smtkCreateMacro(...) |
Add static create() methods to a class. More... | |
#define | smtkEnableSharedPtr(...) |
An abbreviation for enabling shared pointers. More... | |
#define | smtkSharedFromThisMacro(...) |
A macro to help with derived classes whose bases enable shared_from_this(). More... | |
#define | smtkSharedPtrCreateMacro(...) |
A convenience macro for declaring shared_from_this and create methods. More... | |
#define | smtkSharedPtrHelper(...) smtk::static_pointer_cast<SelfType>(SharedPtrBaseType::Ptr(__VA_ARGS__)) |
A convenience macro to use in the body of create methods that take arguments. More... | |
#define | smtkTypenameMacroBase(...) |
Add typedefs to a class for identifcation. More... | |
#define | smtkTypenameMacro(...) |
Add typedefs to a class for identifcation. More... | |
#define | smtkTypeMacro(...) |
Add typedefs to a class for identifcation. More... | |
#define | smtkTypeMacroBase(...) |
Add typedefs to a class for identifcation. More... | |
Macros for dealing with shared-pointer classes.
#define smtkCreateMacro | ( | ... | ) |
Add static create() methods to a class.
This macro takes a single parameter naming either the class of interest (if no ancestor classes use enable_shared_from_this() or smtk::EnableSharedPtr()) or the first – and only – ancestor class that uses smtkEnableSharedPtr() (i.e. inherits enable_shared_from_this() or smtk::EnableSharedPtr()).
This macro also requires the use of smtkTypeMacro() as it needs SelfType defined.
Two static class functions are declared: both return a shared pointer to a newly created class instance but one takes no arguments and the other takes one. The 1-argument version also sets the passed shared pointer to refer to the newly created instance. This is useful in declarative APIs for referring back to an instance created as part of a statement whose value is transformed before the returned value can be assigned to a variable.
#define smtkEnableSharedPtr | ( | ... | ) |
An abbreviation for enabling shared pointers.
Use on the base class like so:
class X : smtkEnableSharedPtr(X) { public: smtkTypeMacro(X); smtkCreateMacro(X); };
Don't use on derived classes, use smtkSharedFromThisMacro() or smtkSharedPtrCreateMacro() instead.
Note that this may be complicated on some systems by the C preprocessor's inability to handle macros whose arguments include a multiple-parameter template. (The comma separating the template parameters is taken as an additional macro argument.) However, in general shared pointers will be enabled on non-templated base classes from which templated classes may be derived.
It is recommended that you make constructors protected or private to avoid heap allocation of objects that may return shared pointers.
For non-abstract classes, it is recommended that you call smtkCreateMacro() as a safe way to expose public construction.
#define smtkInheritanceHierarchy | ( | ... | ) |
#define smtkSharedFromThisMacro | ( | ... | ) |
A macro to help with derived classes whose bases enable shared_from_this().
Use like so:
class X : smtkEnableSharedPtr(X) { public: smtkTypeMacro(X); };
class Y : public X { public: smtkTypeMacro(Y); smtkSharedFromThisMacro(X); // smtkSharedPtrCreateMacro(X); // preferred convenience, see below. ... Y::Ptr method() { return shared_from_this(); } };
Note that the macro argument is the base class on which shared pointers are enabled (or another inherited class in between which also defines a shared_from_this() method).
This macro implements a shared_from_this() method in the derived class that returns a shared pointer of the proper type.
#define smtkSharedPtrCreateMacro | ( | ... | ) |
A convenience macro for declaring shared_from_this and create methods.
Same as smtkSharedFromThisMacro(), the argument is the base class and this macro should not be used on base classes. The usage can be found in smtkSharedFromThisMacro().
#define smtkSharedPtrHelper | ( | ... | ) | smtk::static_pointer_cast<SelfType>(SharedPtrBaseType::Ptr(__VA_ARGS__)) |
A convenience macro to use in the body of create methods that take arguments.
This macro acts like a function that takes a pointer to an instance of your class and returns a shared pointer to the instance, even when shared_from_this is inherited from a base class. For example:
class Y : smtkEnabledSharedPtr(X) { public: smtkTypeMacro(Y); static Ptr create(int a, double b); protected: Y(int a, double b); };
Y::Ptr create(int a, double b) { return smtkSharedPtrHelper( new Y(a, b)); }
It is important to use this method in classes derived from those that use smtkEnabledSharedPtr() rather than naively constructing a shared pointer of the proper type, since that will likely result in an exception being thrown; the path must be from Y* to shared_ptr<X> to shared_ptr<Y>, not straight from Y* to shared_ptr<Y>, or the pointer will be inserted into the wrong pool of shared pointers leading to premature deletion or multiple-deletion.
#define smtkSuperclassMacro | ( | ... | ) |
Add a typedef to the superclass of this class.
This adds typedefs named Superclass
and SuperclassPtr
(i.e., a shared pointer to the superclass) to the class.
Unlike VTK's type macro, it is separate from smtkTypeMacro() in order to support classes with multiple template parameters, since preprocessor macros do not properly handle commas inside template parameter lists.
#define smtkTypedefs | ( | ... | ) |
Used by smtkTypeMacro()
#define smtkTypeMacro | ( | ... | ) |
Add typedefs to a class for identifcation.
This macro takes a single parameter naming the class in which the macro is placed. It defines the following types inside the class scope:
SelfType
(the type of the class itself),Ptr
(a shared pointer to an instance of the class), andConstPtr
(a shared pointer to a constant instance of the class).WeakPtr
(a weak pointer to an instance of the class), andWeakConstPtr
(a weak pointer to a constant instance of the class).Use it like so:
class X { public: smtkTypeMacro(X); ... };
These types are useful when dealing with shared pointers in a class hierarchy.
#define smtkTypeMacroBase | ( | ... | ) |
Add typedefs to a class for identifcation.
This macro takes a single parameter naming the class in which the macro is placed. It defines the following types inside the class scope:
SelfType
(the type of the class itself),Ptr
(a shared pointer to an instance of the class), andConstPtr
(a shared pointer to a constant instance of the class).WeakPtr
(a weak pointer to an instance of the class), andWeakConstPtr
(a weak pointer to a constant instance of the class).Use it like so:
class X { public: smtkTypeMacro(X); ... };
These types are useful when dealing with shared pointers in a class hierarchy.
#define smtkTypenameMacro | ( | ... | ) |
Add typedefs to a class for identifcation.
Similar to smtkTypeMacro(), but only adds a typeName, and no shared_ptr definitions. Useful for e.g. Qt classes, that have their own shared pointer definition.
#define smtkTypenameMacroBase | ( | ... | ) |
Add typedefs to a class for identifcation.
Similar to smtkTypeMacro(), but only adds a typeName, and no shared_ptr definitions. Useful for e.g. Qt classes, that have their own shared pointer definition.