MIRA
Public Member Functions | List of all members
TypedVoidPtr Class Reference

Class that allows to maintain type-safety when passing void pointers. More...

#include <utils/TypedVoidPtr.h>

Public Member Functions

 TypedVoidPtr ()
 Creates nullptr. More...
 
template<typename T >
 TypedVoidPtr (T *ptr)
 Creates a typed void pointer from the given pointer. More...
 
bool isNull () const
 Return if underlying pointer is NULL. More...
 
template<typename T >
 operator T* ()
 Safely casts to T*. More...
 
template<typename T >
 operator const T * () const
 Safely casts to const T*. More...
 
template<typename T >
T * cast ()
 Safely casts the object pointer that is stored to T*. More...
 
template<typename T >
const T * cast () const
 Safely casts the object pointer that is stored to const T*. More...
 

Detailed Description

Class that allows to maintain type-safety when passing void pointers.

Sometimes, it is necessary to pass a void* pointer through an interface, while maintaining type safety. This can be achieved using this class. Assume you want to pass different objects of different types through the following single interface, which must be the same for all types and objects (e.g. when you cannot use templates)

struct Interface {
virtual void foo(TypedVoidPtr ptr) = 0;
};

Now assume, you have different classes that implement this interface for different types, then you can use the TypeVoidPtr class as follows:

struct IntHandler : public Interface {
virtual void foo(TypedVoidPtr ptr) {
int* p = ptr; // this cast is safe and will do full type checking!
std::cout << "Integer is: " << *p << std::endl;
}
};
struct StringHandler : public Interface {
virtual void foo(TypedVoidPtr ptr) {
std::string* s = ptr; // this cast is safe and will do full type checking!
std::cout << "String is: " << *s << std::endl;
}
};
Interface* i1 = new IntHandler;
Interface* i2 = new StringHandler;
int val = 123;
i1->foo(&val); // foo will internally cast back to int
std::string s = "Hello world";
i2->foo(&s); // foo will internally cast back to string
i2->foo(&val); // will cause exception, since int cannot be casted into
// string in StringHandler::foo()

Constructor & Destructor Documentation

◆ TypedVoidPtr() [1/2]

TypedVoidPtr ( )
inline

Creates nullptr.

◆ TypedVoidPtr() [2/2]

TypedVoidPtr ( T *  ptr)
inline

Creates a typed void pointer from the given pointer.

Member Function Documentation

◆ isNull()

bool isNull ( ) const
inline

Return if underlying pointer is NULL.

◆ operator T*()

operator T* ( )
inline

Safely casts to T*.

Exceptions
XBadCast,ifthe stored void pointer is not of type T.

◆ operator const T *()

operator const T * ( ) const
inline

Safely casts to const T*.

Exceptions
XBadCast,ifthe stored void pointer is not of type T.

◆ cast() [1/2]

T* cast ( )
inline

Safely casts the object pointer that is stored to T*.

Exceptions
XBadCast,ifthe stored void pointer is not of type T.

◆ cast() [2/2]

const T* cast ( ) const
inline

Safely casts the object pointer that is stored to const T*.

Exceptions
XBadCast,ifthe stored void pointer is not of type T.

The documentation for this class was generated from the following file: