diff options
author | Julian Eisel <julian@blender.org> | 2022-01-26 18:00:36 +0300 |
---|---|---|
committer | Julian Eisel <julian@blender.org> | 2022-01-26 21:15:57 +0300 |
commit | 08e2885796f79a34cfa7233945d194d382b47d23 (patch) | |
tree | 6c350f651c650ffa8c1b28407e142dc1b7dbd13f /source/blender/editors/space_outliner | |
parent | da1b6c4c02d5390df08f710d01b9d7628478d089 (diff) |
Outliner: Function to "cast" C-style TreeElement to typed C++ pendant
Add function to safely request the type-specific C++ element from a
C-style `TreeElement`. Looks like this:
```
TreeElementFoo *te_foo = tree_element_cast<TreeElementFoo>(te);
```
The "cast" will return null if the tree-element doesn't match the
requested type.
This is useful for the transition from the C-style type to the new ones.
Diffstat (limited to 'source/blender/editors/space_outliner')
-rw-r--r-- | source/blender/editors/space_outliner/outliner_intern.hh | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/source/blender/editors/space_outliner/outliner_intern.hh b/source/blender/editors/space_outliner/outliner_intern.hh index a62d35131ca..0538cd3a4ae 100644 --- a/source/blender/editors/space_outliner/outliner_intern.hh +++ b/source/blender/editors/space_outliner/outliner_intern.hh @@ -27,6 +27,9 @@ #include "RNA_types.h" +/* Needed for `tree_element_cast()`. */ +#include "tree/tree_element.hh" + #ifdef __cplusplus extern "C" { #endif @@ -686,3 +689,19 @@ int outliner_context(const struct bContext *C, #ifdef __cplusplus } #endif + +namespace blender::ed::outliner { + +/** + * Helper to safely "cast" a #TreeElement to its new C++ #AbstractTreeElement, if possible. + * \return nullptr if the tree-element doesn't match the requested type \a TreeElementT or the + * element doesn't hold a C++ #AbstractTreeElement pendant yet. + */ +template<typename TreeElementT> TreeElementT *tree_element_cast(const TreeElement *te) +{ + static_assert(std::is_base_of_v<AbstractTreeElement, TreeElementT>, + "Requested tree-element type must be an AbstractTreeElement"); + return dynamic_cast<TreeElementT *>(te->type.get()); +} + +} // namespace blender::ed::outliner |