SIRIUS 7.5.0
Electronic structure library and applications
Public Member Functions | Private Attributes | List of all members
sirius::any_ptr Class Reference

Handle deallocation of a poiniter to an object of any type. More...

#include <any_ptr.hpp>

Public Member Functions

template<typename T >
 any_ptr (T *ptr__)
 Constructor. More...
 
 ~any_ptr ()
 Destructor. More...
 
template<typename T >
T & get () const
 Cast pointer to a given type and return a reference. More...
 

Private Attributes

void * ptr_
 Untyped pointer to a stored object. More...
 
std::function< void(void *)> deleter_
 Deleter for the stored object. More...
 

Detailed Description

Handle deallocation of a poiniter to an object of any type.

Example:

class Foo
{
private:
int n_{0};
public:
Foo(int i) : n_(i)
{
std::cout << "in Foo() constructor\n";
}
~Foo()
{
std::cout << "in Foo() destructor\n";
}
void print()
{
std::cout << "the number is: " << n_ << "\n";
}
};
int main(int argn, char** argv)
{
void* ptr = new any_ptr(new Foo(42));
auto& foo = static_cast<any_ptr*>(ptr)->get<Foo>();
foo.print();
delete static_cast<any_ptr*>(ptr);
return 0;
}
any_ptr(T *ptr__)
Constructor.
Definition: any_ptr.hpp:84

And the output is:

in Foo() constructor
the number is: 42
in Foo() destructor

Definition at line 73 of file any_ptr.hpp.

Constructor & Destructor Documentation

◆ any_ptr()

template<typename T >
sirius::any_ptr::any_ptr ( T *  ptr__)
inline

Constructor.

Definition at line 84 of file any_ptr.hpp.

◆ ~any_ptr()

sirius::any_ptr::~any_ptr ( )
inline

Destructor.

Definition at line 90 of file any_ptr.hpp.

Member Function Documentation

◆ get()

template<typename T >
T & sirius::any_ptr::get ( ) const
inline

Cast pointer to a given type and return a reference.

Definition at line 96 of file any_ptr.hpp.

Member Data Documentation

◆ ptr_

void* sirius::any_ptr::ptr_
private

Untyped pointer to a stored object.

Definition at line 77 of file any_ptr.hpp.

◆ deleter_

std::function<void(void*)> sirius::any_ptr::deleter_
private

Deleter for the stored object.

Definition at line 79 of file any_ptr.hpp.


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