10 #ifndef _STRUS_REFERENCE_HPP_INCLUDED
11 #define _STRUS_REFERENCE_HPP_INCLUDED
21 template <
class Object>
27 :m_obj(0),m_refcnt(0){}
34 m_refcnt = newRefCnt();
37 catch (
const std::bad_alloc&)
40 if (doThrow)
throw std::bad_alloc();
45 :m_obj(o.m_obj),m_refcnt(o.m_refcnt)
47 if (m_refcnt) ++*m_refcnt;
60 m_refcnt = o.m_refcnt;
61 if (m_refcnt) ++*m_refcnt;
74 m_refcnt = newRefCnt();
76 catch (
const std::bad_alloc&)
83 else if (*m_refcnt == 1)
89 int* rc = newRefCnt();
111 const Object*
get()
const {
return m_obj;}
113 Object*
get() {
return m_obj;}
119 if (m_refcnt && *m_refcnt == 1)
123 std::free( m_refcnt);
130 throw std::logic_error(
"cannot release shared object (having more than one reference)");
140 return m_refcnt?(*m_refcnt):0;
146 int* rt = (
int*)std::malloc(
sizeof(
int));
147 if (!rt)
throw std::bad_alloc();
153 if (m_refcnt && --*m_refcnt == 0)
156 std::free( m_refcnt);
164 mutable int* m_refcnt;
Reference()
Default constructor.
Definition: reference.hpp:26
Object * operator->()
Object access operator.
Definition: reference.hpp:102
void reset(Object *obj_=0)
Reinitialize the local value of the reference and dispose the old value if not referenced by others...
Definition: reference.hpp:66
Object & operator*()
Object access operator.
Definition: reference.hpp:106
Object * release()
Release object reference and return the pointer to the object (with ownership) if this is possible...
Definition: reference.hpp:117
Shared pointer template with non thread-safe reference counting.
Definition: reference.hpp:22
~Reference()
Destructor.
Definition: reference.hpp:51
Reference & operator=(const Reference &o)
Assignment operator.
Definition: reference.hpp:57
Reference(const Reference &o)
Copy constructor.
Definition: reference.hpp:44
unsigned int refcnt() const
Definition: reference.hpp:138
const Object & operator*() const
Object access operator.
Definition: reference.hpp:108
const Object * operator->() const
Object access operator.
Definition: reference.hpp:104
Reference(Object *obj_, bool doThrow=true)
Constructor.
Definition: reference.hpp:29