|
GEGELATI
|
Class behaving as a std::shared_ptr whose type is not templated. More...
#include <untypedSharedPtr.h>
Classes | |
| struct | Concept |
| Internal structure of the type erasure pattern. More... | |
| struct | Model |
| Internal templated structure of the type erasure pattern. More... | |
Public Member Functions | |
| UntypedSharedPtr ()=delete | |
| Deleted default constructor. | |
| template<typename T , class Deleter = std::default_delete<T>> | |
| UntypedSharedPtr (T *obj, Deleter func=Deleter()) | |
| Main constructor of the UntypedSharedPtr class. | |
| UntypedSharedPtr (std::shared_ptr< Concept > concept) | |
| Constructor from an existing Concept. | |
| const std::type_info & | getType () const |
| Accessor to the type of data stored in the UntypedSharedPtr. | |
| const std::type_info & | getPtrType () const |
| Accessor to the pointer type of data stored in the UntypedSharedPtr. | |
| template<typename T > | |
| std::shared_ptr< std::remove_all_extents_t< T > > | getSharedPointer () const |
| Get the shared_ptr store in the UntypedSharedPtr. | |
Public Attributes | |
| std::shared_ptr< const Concept > | sharedPtrContainer |
| Shared container containing the data structure containing the actual std::shared_ptr. | |
Static Public Attributes | |
| template<typename T > | |
| static std::function< void(T *) | emptyDestructor )() |
| Get an empty destructor function for any type. | |
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/
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:
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.
As it is the case with std::shared_ptr, when misused, these capacity may result in invalid data access.
| 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> |
| [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>(). |
|
inline |
Constructor from an existing Concept.
This constructor is needed when the pointer type passed to the classical constructor decays automatically into something else. For example, any pointer to a C-style array allocated with 'new T[]' automatically decays to 'T*' when passed to a function. By constructing a Model<W> with the desired template type (eg. W = T[]), it is possible to force the 'getPtr()' method to return this 'T[]' type.
| [in] | concept | the instance of the Model<T> class for building the UntypedSharedPointer. |
|
inline |
Accessor to the pointer type of data stored in the UntypedSharedPtr.
|
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.
| T | Type of the retrieved std::shared_ptr<T>. |
| std::runtime_exception | if the template parameter differs from the type given at construction of the UntypedSharedPtr. |
|
inline |
Accessor to the type of data stored in the UntypedSharedPtr.
Get an empty destructor function for any type.
When building an UntypedSharedPtr, this static method can be used to provide an empty destructor for the second argument of the constructor. The template param should be the same as the type of the pointer given to the UntypedSharetPtr constructor.
| T | the type of the pointer to deallocate. |