diff options
Diffstat (limited to 'source/blender/functions/FN_generic_pointer.hh')
-rw-r--r-- | source/blender/functions/FN_generic_pointer.hh | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/source/blender/functions/FN_generic_pointer.hh b/source/blender/functions/FN_generic_pointer.hh index 5c2b611c614..2bd66daa7fe 100644 --- a/source/blender/functions/FN_generic_pointer.hh +++ b/source/blender/functions/FN_generic_pointer.hh @@ -21,7 +21,7 @@ namespace blender::fn { /** - * A generic pointer whose type is only known at runtime. + * A generic non-const pointer whose type is only known at runtime. */ class GMutablePointer { private: @@ -58,7 +58,7 @@ class GMutablePointer { template<typename T> T *get() const { BLI_assert(this->is_type<T>()); - return reinterpret_cast<T *>(data_); + return static_cast<T *>(data_); } template<typename T> bool is_type() const @@ -73,4 +73,55 @@ class GMutablePointer { } }; +/** + * A generic const pointer whose type is only known at runtime. + */ +class GPointer { + private: + const CPPType *type_ = nullptr; + const void *data_ = nullptr; + + public: + GPointer() = default; + + GPointer(GMutablePointer ptr) : type_(ptr.type()), data_(ptr.get()) + { + } + + GPointer(const CPPType *type, const void *data = nullptr) : type_(type), data_(data) + { + /* If there is data, there has to be a type. */ + BLI_assert(data_ == nullptr || type_ != nullptr); + } + + GPointer(const CPPType &type, const void *data = nullptr) : type_(&type), data_(data) + { + } + + template<typename T> GPointer(T *data) : GPointer(&CPPType::get<T>(), data) + { + } + + const void *get() const + { + return data_; + } + + const CPPType *type() const + { + return type_; + } + + template<typename T> const T *get() const + { + BLI_assert(this->is_type<T>()); + return static_cast<const T *>(data_); + } + + template<typename T> bool is_type() const + { + return type_ != nullptr && type_->is<T>(); + } +}; + } // namespace blender::fn |