23 #ifndef SHARED_OBJECT_HPP 24 #define SHARED_OBJECT_HPP 34 class SharedObjectNode;
114 virtual void repr(std::ostream &stream)
const;
117 virtual void print(std::ostream &stream=std::cout)
const;
140 void makeUnique(std::map<SharedObjectNode*,SharedObject>& already_copied,
bool clone_members=
true);
173 virtual void deepCopyMembers(std::map<SharedObjectNode*,SharedObject>& already_copied);
188 virtual void repr(std::ostream &stream)
const;
191 virtual void print(std::ostream &stream)
const;
196 B shared_from_this();
200 const B shared_from_this()
const;
224 if(ptr && !ret.checkNode())
254 A deepcopy(
const A& a, std::map<SharedObjectNode*,SharedObject>& already_copied){
256 ret.makeUnique(already_copied);
261 std::vector<A>
deepcopy(
const std::vector<A>& a, std::map<SharedObjectNode*,SharedObject>& already_copied){
262 std::vector<A> ret = a;
263 for(
typename std::vector<A>::iterator it=ret.begin(); it!=ret.end(); ++it){
264 it->makeUnique(already_copied);
274 ret.assignNode(
this);
285 ret.assignNode(const_cast<SharedObjectNode*>(
this));
300 #endif // SHARED_OBJECT_HPP Internal class for the reference counting framework, see comments on the public class.
void assignNode(SharedObjectNode *node)
Assign the node to a node class pointer (or null)
bool isInit() const
Is initialized?
void assertInit() const
Assert that it is initialized.
SharedObject()
Default constructor.
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...
B shared_from_this()
Get a shared object from the current internal object.
SharedObject implements a reference counting framework simular for effient and easily-maintained memo...
const SharedObjectNode * get() const
Get a const pointer to the node.
bool is_a(const SharedObject &A)
Check if a shared object is of a certain type.
unsigned int count
Number of references pointing to the object.
virtual bool checkNode() const
Assert that the node is pointing to the right type of object.
SharedObject & operator=(const SharedObject &ref)
Assignment operator.
void assignNodeNoCount(SharedObjectNode *node)
Assign the node to a node class pointer without reference counting: inproper use will cause memory le...
friend B shared_cast(SharedObject &A)
Typecast a shared object to a base class to a shared object to a derived class, cf. dynamic_cast.
int getCount() const
Get the reference count.
SharedObjectNode * operator->()
Access a member function or object.
Base class for objects that have a natural string representation.
bool isNull() const
Is a null pointer?
void swap(SharedObject &other)
Swap content with another instance.
A deepcopy(const A &a)
Make a deep copy of an object (Note: default is a shallow copy!)
SharedObject clone() const
Deep copy.
void init()
Initialize the object: more documentation in the node class (SharedObjectNode and derived classes) ...
virtual void repr(std::ostream &stream) const
Print a representation of the object.
bool is_init_
Has the function been initialized?
~SharedObject()
Destructor.
virtual void print(std::ostream &stream=std::cout) const
Print a destription of the object.