Functions: add generic pointer class for const pointers
This adds a GPointer class, which is mostly the same as GMutablePointer. The main difference is that GPointer references const data, while GMutablePointer references non-const data.
This commit is contained in:
parent
f43561eae6
commit
79d6bd9a22
|
@ -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
|
||||
|
|
|
@ -66,7 +66,7 @@ template<typename Key> class GValueMap {
|
|||
|
||||
/* Add a value to the container that is copy constructed from the given value. The caller remains
|
||||
* responsible for destructing and freeing the given value. */
|
||||
template<typename ForwardKey> void add_new_by_copy(ForwardKey &&key, GMutablePointer value)
|
||||
template<typename ForwardKey> void add_new_by_copy(ForwardKey &&key, GPointer value)
|
||||
{
|
||||
const CPPType &type = *value.type();
|
||||
void *buffer = allocator_.allocate(type.size(), type.alignment());
|
||||
|
|
Loading…
Reference in New Issue