All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ReferenceHolder.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
ReferenceHolder.h
Smart pointer like class that dereferences
obtained pointer to a QoreReferenceCounter in its destructor.
Qore Programming Language
Copyright (C) 2006 - 2015 Qore Technologies
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Note that the Qore library is released under a choice of three open-source
licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
information.
*/
#ifndef QORE_REFERENCE_HOLDER_H_
#define QORE_REFERENCE_HOLDER_H_
#include <stdlib.h>
template<typename T = class AbstractQoreNode>
class ReferenceHolder {
private:
DLLLOCAL ReferenceHolder(const ReferenceHolder&); // not implemented
DLLLOCAL ReferenceHolder& operator=(const ReferenceHolder&); // not implemented
DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
T* p;
ExceptionSink* xsink;
public:
DLLLOCAL ReferenceHolder(ExceptionSink* xsink_) : p(0), xsink(xsink_) {}
DLLLOCAL ReferenceHolder(T* p_, ExceptionSink* xsink_) : p(p_), xsink(xsink_) {}
DLLLOCAL ~ReferenceHolder() { if (p) p->deref(xsink);}
DLLLOCAL T* operator->() { return p; }
DLLLOCAL T* operator*() { return p; }
DLLLOCAL const T* operator->() const { return p; }
DLLLOCAL const T* operator*() const { return p; }
DLLLOCAL void operator=(T *nv) {
if (p)
p->deref(xsink);
p = nv;
}
DLLLOCAL T* release() {
T *rv = p;
p = 0;
return rv;
}
DLLLOCAL operator bool() const { return p != 0; }
DLLLOCAL T** getPtrPtr() { return &p; }
DLLLOCAL T*& getRef() { return p; }
};
template<typename T>
class SimpleRefHolder {
private:
DLLLOCAL SimpleRefHolder(const SimpleRefHolder&); // not implemented
DLLLOCAL SimpleRefHolder& operator=(const SimpleRefHolder&); // not implemented
DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
T* p;
public:
DLLLOCAL SimpleRefHolder() : p(0) {}
DLLLOCAL SimpleRefHolder(T* p_) : p(p_) {}
DLLLOCAL ~SimpleRefHolder() { if (p) p->deref(); }
DLLLOCAL T* operator->() { return p; }
DLLLOCAL T* operator*() { return p; }
DLLLOCAL const T* operator->() const { return p; }
DLLLOCAL const T* operator*() const { return p; }
DLLLOCAL void operator=(T *nv) {
if (p)
p->deref();
p = nv;
}
DLLLOCAL T *release() {
T *rv = p;
p = 0;
return rv;
}
DLLLOCAL operator bool() const { return p != 0; }
};
#endif
// EOF
ReferenceHolder::getPtrPtr
DLLLOCAL T ** getPtrPtr()
returns a pointer to the pointer being managed
Definition: ReferenceHolder.h:100
ReferenceHolder::ReferenceHolder
DLLLOCAL ReferenceHolder(T *p_, ExceptionSink *xsink_)
populates with object with data and the ExceptionSink pointer
Definition: ReferenceHolder.h:65
ReferenceHolder::operator->
DLLLOCAL const T * operator->() const
returns the pointer being managed
Definition: ReferenceHolder.h:77
ReferenceHolder::release
DLLLOCAL T * release()
releases the pointer to the caller
Definition: ReferenceHolder.h:90
ReferenceHolder::operator*
DLLLOCAL T * operator*()
returns the pointer being managed
Definition: ReferenceHolder.h:74
ReferenceHolder::~ReferenceHolder
DLLLOCAL ~ReferenceHolder()
calls deref(ExceptionSink *) on the pointer being managed if not 0
Definition: ReferenceHolder.h:68
ReferenceHolder::getRef
DLLLOCAL T *& getRef()
returns a reference to the ptr being managed
Definition: ReferenceHolder.h:103
ExceptionSink
container for holding Qore-language exception information and also for registering a &quot;thread_exit&quot; ca...
Definition: ExceptionSink.h:47
SimpleRefHolder
manages a reference count of a pointer to a class that takes a simple &quot;deref()&quot; call with no argument...
Definition: ReferenceHolder.h:117
ReferenceHolder::operator*
DLLLOCAL const T * operator*() const
returns the pointer being managed
Definition: ReferenceHolder.h:80
ReferenceHolder::operator->
DLLLOCAL T * operator->()
returns the pointer being managed
Definition: ReferenceHolder.h:71
ReferenceHolder::ReferenceHolder
DLLLOCAL ReferenceHolder(ExceptionSink *xsink_)
creates an empty ReferenceHolder
Definition: ReferenceHolder.h:62
ReferenceHolder::operator=
DLLLOCAL void operator=(T *nv)
assigns a new pointer to the holder, dereferences the current pointer if any
Definition: ReferenceHolder.h:83
ReferenceHolder
a templated class to manage a reference count of an object that can throw a Qore-language exception w...
Definition: ReferenceHolder.h:51