#include <ReferenceHelper.h>
Public Member Functions | |
DLLEXPORT | ReferenceHelper (const ReferenceNode *ref, AutoVLock &vl, ExceptionSink *xsink) |
initializes the object and tries to get the pointer to the pointer of the lvalue expression target | |
DLLEXPORT | ~ReferenceHelper () |
destroys the object | |
DLLLOCAL | operator bool () const |
returns true if the reference is valid, false if not | |
DLLLOCAL qore_type_t | getType () const |
returns the type of the reference's value | |
DLLEXPORT AbstractQoreNode * | getUnique (ExceptionSink *xsink) |
returns a pointer to the value with a unique reference count (so it can be updated in place), assumes the reference is valid | |
DLLEXPORT int | assign (AbstractQoreNode *val, ExceptionSink *xsink) |
assigns a value to the reference, assumes the reference is valid | |
DLLEXPORT const AbstractQoreNode * | getValue () const |
returns a constant pointer to the reference's value | |
DLLEXPORT void | swap (ReferenceHelper &other) |
swaps the values of two references | |
Protected Attributes | |
AbstractQoreNode ** | vp |
pointer to the pointer to the lvalue expression target of the reference |
Takes care of safely accessing ReferenceNode objects, for example when they are passed as arguments to a builtin function or method. Locks are automatically acquired in the constructor if necessary and released in the destructor. The constructor could raise a Qore-language exception if there is a deadlock acquiring any locks to access the ReferenceNode's value as given by the lvalue expression, so the object should be checked for this state right after the constructor as in the following example:
const AbstractQoreNode *p = get_param(params, 0); if (p && p->getType() == NT_REFERENCE) { const ReferenceNode *r = reinterpret_cast<const ReferenceNode *>(p); AutoVLock vl; ReferenceHelper ref(r, vl, xsink); // a deadlock exception occurred accessing the reference's value pointer if (!ref) return 0; // more code to access the reference }
DLLEXPORT ReferenceHelper::ReferenceHelper | ( | const ReferenceNode * | ref, | |
AutoVLock & | vl, | |||
ExceptionSink * | xsink | |||
) |
initializes the object and tries to get the pointer to the pointer of the lvalue expression target
ref | the ReferenceNode to use | |
vl | the reference to the AutoVLock structure for managing nested locks | |
xsink | Qore-language exceptions raised will be added here (for example, a deadlock accessing the object) |
DLLLOCAL ReferenceHelper::operator bool | ( | ) | const [inline] |
returns true if the reference is valid, false if not
false will only be returned if a Qore-language exception was raised in the constructor
References vp.
DLLLOCAL qore_type_t ReferenceHelper::getType | ( | ) | const [inline] |
returns the type of the reference's value
References AbstractQoreNode::getType(), NT_NOTHING, and vp.
DLLEXPORT AbstractQoreNode* ReferenceHelper::getUnique | ( | ExceptionSink * | xsink | ) |
returns a pointer to the value with a unique reference count (so it can be updated in place), assumes the reference is valid
xsink | required for the call to AbstractQoreNode::deref() |
take care to only call this function on types where the AbstractQoreNode::realCopy() function has a valid implementation (on all value types suitable for in-place modification this function has a valid implementation), as in debugging builds other types will abort(); in non-debugging builds this function will simply do nothing
AutoVLock vl; ReferenceHelper rh(ref, vl, xsink); // if the reference is not valid, then return if (!rh) return; // get the unique value AbstractQoreNode *val = rh.getUnique(xsink); // if a Qore-language exception was raised, then return if (*xsink) return;
DLLEXPORT int ReferenceHelper::assign | ( | AbstractQoreNode * | val, | |
ExceptionSink * | xsink | |||
) |
assigns a value to the reference, assumes the reference is valid
val | the value to assign (must be already referenced for the assignment) | |
xsink | required for the call to AbstractQoreNode::deref() of the current value |
AutoVLock vl; ReferenceHelper rh(ref, vl, xsink); // if the reference is not valid, then return if (!rh) return; // make the assignment (if the assignment fails, the value will be dereferenced automatically) rh.assign(val->refSelf(), xsink);
DLLEXPORT const AbstractQoreNode* ReferenceHelper::getValue | ( | ) | const |
returns a constant pointer to the reference's value