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. | |
void | assignNode (SharedObjectNode *node) |
Assign the node to a node class pointer (or null) | |
void | assignNodeNoCount (SharedObjectNode *node) |
Assign the node to a node class pointer without reference counting: inproper use will cause memory leaks! | |
virtual bool | checkNode () const |
Assert that the node is pointing to the right type of object. | |
SharedObject | clone () const |
Deep copy. | |
const SharedObjectNode * | get () const |
Get a const pointer to the node. | |
SharedObjectNode * | get () |
Get a pointer to the node. | |
int | getCount () const |
Get the reference count. | |
void | init () |
Initialize the object: more documentation in the node class (SharedObjectNode and derived classes) | |
bool | isInit () const |
Is initialized? | |
bool | isNull () const |
Is a null pointer? | |
SharedObjectNode * | operator-> () |
Access a member function or object. | |
const SharedObjectNode * | operator-> () const |
Const access a member function or object. | |
SharedObject & | operator= (const SharedObject &ref) |
Assignment operator. | |
virtual void | print (std::ostream &stream=std::cout) const |
Print a destription of the object. | |
virtual void | repr (std::ostream &stream) const |
Print a representation of the object. | |
SharedObject () | |
Default constructor. | |
SharedObject (const SharedObject &ref) | |
Copy constructor (shallow copy) | |
void | swap (SharedObject &other) |
Swap content with another instance. | |
~SharedObject () | |
Destructor. | |
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. | |
void | makeUnique (std::map< SharedObjectNode *, SharedObject > &already_copied, bool clone_members=true) |
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. | |
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) |
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.
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.
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.
bool CasADi::SharedObject::checkNode | ( | ) | const [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 |
void CasADi::SharedObject::count_down | ( | ) | [private] |
Definition at line 95 of file shared_object.cpp.
void CasADi::SharedObject::count_up | ( | ) | [private] |
Definition at line 91 of file shared_object.cpp.
const SharedObjectNode * CasADi::SharedObject::get | ( | ) | const |
Get a const pointer to the node.
Reimplemented in ExportIndex.
Definition at line 79 of file shared_object.cpp.
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.
Reimplemented in ExportVariable, ExportArgument, ExportData, and ExportIndex.
Definition at line 107 of file shared_object.cpp.
const SharedObjectNode * CasADi::SharedObject::operator-> | ( | ) | const |
Const access a member function or object.
Reimplemented in ExportVariable, ExportArgument, ExportData, and ExportIndex.
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.
void CasADi::SharedObject::print | ( | std::ostream & | stream = std::cout | ) | const [virtual] |
Print a destription of the object.
Reimplemented from CasADi::PrintableObject.
Definition at line 144 of file shared_object.cpp.
void CasADi::SharedObject::repr | ( | std::ostream & | stream | ) | const [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.
B shared_cast | ( | SharedObject & | A | ) | [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.
const B shared_cast | ( | const SharedObject & | A | ) | [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.
SharedObjectNode* CasADi::SharedObject::node [private] |
Definition at line 144 of file shared_object.hpp.