SharedObject implements a reference counting framework simular for effient and easily-maintained memory management. More...
#include <shared_object.hpp>
Public Member Functions | |
void | assertInit () const |
Assert that it is initialized. More... | |
void | assignNode (SharedObjectNode *node) |
Assign the node to a node class pointer (or null) More... | |
void | assignNodeNoCount (SharedObjectNode *node) |
Assign the node to a node class pointer without reference counting: inproper use will cause memory leaks! More... | |
virtual bool | checkNode () const |
Assert that the node is pointing to the right type of object. More... | |
SharedObject | clone () const |
Deep copy. More... | |
const SharedObjectNode * | get () const |
Get a const pointer to the node. More... | |
SharedObjectNode * | get () |
Get a pointer to the node. More... | |
int | getCount () const |
Get the reference count. More... | |
void | init () |
Initialize the object: more documentation in the node class (SharedObjectNode and derived classes) More... | |
bool | isInit () const |
Is initialized? More... | |
bool | isNull () const |
Is a null pointer? More... | |
SharedObjectNode * | operator-> () |
Access a member function or object. More... | |
const SharedObjectNode * | operator-> () const |
Const access a member function or object. More... | |
SharedObject & | operator= (const SharedObject &ref) |
Assignment operator. More... | |
virtual void | print (std::ostream &stream=std::cout) const |
Print a destription of the object. More... | |
virtual void | repr (std::ostream &stream) const |
Print a representation of the object. More... | |
SharedObject () | |
Default constructor. More... | |
SharedObject (const SharedObject &ref) | |
Copy constructor (shallow copy) More... | |
void | swap (SharedObject &other) |
Swap content with another instance. More... | |
~SharedObject () | |
Destructor. More... | |
void | makeUnique (bool clone_members=true) |
If there are other references to the object, then make a deep copy of it and point to this new object. More... | |
void | makeUnique (std::map< SharedObjectNode *, SharedObject > &already_copied, bool clone_members=true) |
Public Member Functions inherited from CasADi::PrintableObject | |
std::string | getDescription () const |
Return a string with a destription (for SWIG) More... | |
std::string | getRepresentation () const |
Return a string with a representation (for SWIG) More... | |
Private Member Functions | |
void | count_down () |
void | count_up () |
Private Attributes | |
SharedObjectNode * | node |
Friends | |
template<class B > | |
B | shared_cast (SharedObject &A) |
Typecast a shared object to a base class to a shared object to a derived class, cf. dynamic_cast. More... | |
template<class B > | |
const B | shared_cast (const SharedObject &A) |
Typecast a shared object to a base class to a shared object to a derived class, cf. dynamic_cast (const) More... | |
SharedObject implements a reference counting framework simular for effient and easily-maintained memory management.
To use the class, both the SharedObject class (the public class), and the SharedObjectNode class (the internal class) must be inherited from. It can be done in two different files and together with memory management, this approach provides a clear destinction of which methods of the class are to be considered "public", i.e. methods for public use that can be considered to remain over time with small changes, and the internal memory.
When interfacing a software, which typically includes including some header file, this is best done only in the file where the internal class is defined, to avoid polluting the global namespace and other side effects.
The default constructor always means creating a null pointer to an internal class only. To allocate an internal class (this works only when the internal class isn't abstract), use the constructor with arguments.
The copy constructor and the assignment operator perform shallow copies only, to make a deep copy you must use the clone method explictly. This will give a shared pointer instance.
In an inheritance hierarchy, you can cast down automatically, e.g. (SXFunction is a child class of FX): SXFunction derived(...); FX base = derived;
To cast up, use the shared_cast template function, which works analogously to dynamic_cast, static_cast, const_cast etc, e.g.: SXFunction derived(...); FX base = derived; SXFunction derived_from_base = shared_cast<SXFunction>(base);
A failed shared_cast will result in a null pointer (cf. dynamic_cast)
Definition at line 66 of file shared_object.hpp.
CasADi::SharedObject::SharedObject | ( | ) |
Default constructor.
Definition at line 32 of file shared_object.cpp.
CasADi::SharedObject::SharedObject | ( | const SharedObject & | ref | ) |
Copy constructor (shallow copy)
Definition at line 47 of file shared_object.cpp.
CasADi::SharedObject::~SharedObject | ( | ) |
Destructor.
Definition at line 52 of file shared_object.cpp.
void CasADi::SharedObject::assertInit | ( | ) | const |
Assert that it is initialized.
Definition at line 214 of file shared_object.cpp.
void CasADi::SharedObject::assignNode | ( | SharedObjectNode * | node | ) |
Assign the node to a node class pointer (or null)
Definition at line 56 of file shared_object.cpp.
void CasADi::SharedObject::assignNodeNoCount | ( | SharedObjectNode * | node | ) |
Assign the node to a node class pointer without reference counting: inproper use will cause memory leaks!
Definition at line 62 of file shared_object.cpp.
|
virtual |
Assert that the node is pointing to the right type of object.
Definition at line 128 of file shared_object.cpp.
SharedObject CasADi::SharedObject::clone | ( | ) | const |
Deep copy.
Definition at line 185 of file shared_object.cpp.
|
private |
Definition at line 95 of file shared_object.cpp.
|
private |
Definition at line 91 of file shared_object.cpp.
const SharedObjectNode * CasADi::SharedObject::get | ( | ) | const |
Get a const pointer to the node.
Definition at line 79 of file shared_object.cpp.
SharedObjectNode * CasADi::SharedObject::get | ( | ) |
Get a pointer to the node.
Definition at line 83 of file shared_object.cpp.
int CasADi::SharedObject::getCount | ( | ) | const |
Get the reference count.
Definition at line 199 of file shared_object.cpp.
void CasADi::SharedObject::init | ( | ) |
Initialize the object: more documentation in the node class (SharedObjectNode and derived classes)
Definition at line 121 of file shared_object.cpp.
bool CasADi::SharedObject::isInit | ( | ) | const |
Is initialized?
Definition at line 210 of file shared_object.cpp.
bool CasADi::SharedObject::isNull | ( | ) | const |
Is a null pointer?
Definition at line 87 of file shared_object.cpp.
void CasADi::SharedObject::makeUnique | ( | bool | clone_members = true | ) |
If there are other references to the object, then make a deep copy of it and point to this new object.
Definition at line 154 of file shared_object.cpp.
void CasADi::SharedObject::makeUnique | ( | std::map< SharedObjectNode *, SharedObject > & | already_copied, |
bool | clone_members = true |
||
) |
Definition at line 159 of file shared_object.cpp.
SharedObjectNode * CasADi::SharedObject::operator-> | ( | ) |
Access a member function or object.
Definition at line 107 of file shared_object.cpp.
const SharedObjectNode * CasADi::SharedObject::operator-> | ( | ) | const |
Const access a member function or object.
Definition at line 102 of file shared_object.cpp.
SharedObject & CasADi::SharedObject::operator= | ( | const SharedObject & | ref | ) |
Assignment operator.
Definition at line 66 of file shared_object.cpp.
|
virtual |
Print a destription of the object.
Reimplemented from CasADi::PrintableObject.
Definition at line 144 of file shared_object.cpp.
|
virtual |
Print a representation of the object.
Reimplemented from CasADi::PrintableObject.
Definition at line 132 of file shared_object.cpp.
void CasADi::SharedObject::swap | ( | SharedObject & | other | ) |
Swap content with another instance.
Definition at line 193 of file shared_object.cpp.
|
friend |
Typecast a shared object to a base class to a shared object to a derived class, cf. dynamic_cast.
Get a pointer to the node
Create a return object
Assign node of B and return
Null pointer if not pointing towards the right type of object
Definition at line 212 of file shared_object.hpp.
|
friend |
Typecast a shared object to a base class to a shared object to a derived class, cf. dynamic_cast (const)
Definition at line 232 of file shared_object.hpp.
|
private |
Definition at line 144 of file shared_object.hpp.