Class behaving as a std::shared_ptr whose type is not templated.
Instances of this class is that it behaves as a share_ptr, meaning that it contains a pointer to an object that is freed automatically when the last copy of the UntypedSharedPtr associated to this pointer is deleted. The deleter that can be passed as a parameter when building an instance is used to delete the object. By giving an empty function to the constructor, the deletion of the pointer can thus be prevented, and the UntypedSharedPtr can be used to store a regular pointer.
The main difference with the classical std::shared_ptr<T> is that no template parameter specifies what is stored inside the UntypedSharedPtr. Hence, a std:vector<UntypedSharedPtr> can contain shared pointer to any type of data (including primitive types), whereas a std::vector< std::shared_ptr<Base>> can contain only pointers to types derived from a given Base class, which notably prevent such a vector from containing shared pointer to several distinct primitive types.
The code of this class is based on the Type Erasure patterns, and is directly inspired by this example/
template<typename T , class Deleter = std::default_delete<T>>
Data::UntypedSharedPtr::UntypedSharedPtr |
( |
T * |
obj, |
|
|
Deleter |
func = Deleter() |
|
) |
| |
|
inline |
Main constructor of the UntypedSharedPtr class.
Constructs an instance of the UntypedSharedPtr class whose type is given as a template parameter T that is deduced at function call.
For example, the following codes creates an UntypedSharedPtr for an int value:
UntypedSharedPtr()=delete
Deleted default constructor.
The default deleter for the given pointer type is used by default, unless a deleter is explicitly given to the constructor. For example, a deleter doing nothing can be given to prevent the shared pointer from deallocating the memory of a variable on the stack.
auto del = [](const double *){};
double a{2.5};
{
}
nothing.
As it is the case with std::shared_ptr, when misused, these capacity may result in invalid data access.
- Template Parameters
-
T | type of the equivalent std::shared_pointer<T>. Beware, const qualifier matters. |
Deleter | type of the deletion function. Default value is std::default_delete<T> |
- Parameters
-
[in] | obj | Pointer to the memory managed by the UntypedSharedPtr. |
[in] | func | The function used as Deleter when the last copy of the UntypedSharedPtr disappears. Default value is std::default_delete<T>(). |
template<typename T >
std::shared_ptr< std::remove_all_extents_t< T > > Data::UntypedSharedPtr::getSharedPointer |
( |
| ) |
const |
|
inline |
Get the shared_ptr store in the UntypedSharedPtr.
This templated function returns the std::shared_ptr hidden in the UntypedSharedPtr. For the function to work, the given template parameter must be the same as the one given during construction of the UntypedSharedPtr. If a Derived class was given at construction, a Base type can not be given to this function. The opposite (Base at construction and Derived to this function) also does not work, even if the actual object associated to the pointer is of type Derived.
If the type given at construction was const, the type given to this function must be const. If the type given at construction was non-const, the type given to this function can either be const or non- const.
class Base {};
class Derived : public Base {};
Base * ptrA = new Derived();
std::shared_ptr<Base> spA1 = uspA.getSharedPtr<Base>();
std::shared_ptr<const Base> spA2 = uspA.getSharedPtr<const Base>();
KO
const Base * ptrB = new Derived();
std::shared_ptr<const Base> spB2 = uspB.getSharedPtr<const Base>();
KO
- Template Parameters
-
T | Type of the retrieved std::shared_ptr<T>. |
- Returns
- the std::shared_ptr<T> of the pointer stored in the UntypedSharedPtr.
- Exceptions
-
std::runtime_exception | if the template parameter differs from the type given at construction of the UntypedSharedPtr. |