QoreValue.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QoreValue.h
Qore Programming Language
Copyright (C) 2003 - 2022 Qore Technologies, s.r.o.
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_QOREVALUE_H
#define _QORE_QOREVALUE_H
#include <cassert>
typedef unsigned char valtype_t;
#define QV_Bool (valtype_t)0
#define QV_Int (valtype_t)1
#define QV_Float (valtype_t)2
#define QV_Node (valtype_t)3
#define QV_Ref (valtype_t)4
// forward references
class AbstractQoreNode;
class QoreString;
struct QoreValue;
union qore_value_u {
bool b;
int64 i;
double f;
AbstractQoreNode* n;
};
namespace detail {
template<typename Type>
struct QoreValueCastHelper {
typedef Type * Result;
template<typename QV>
static Result cast(QV *qv, valtype_t type) {
assert(type == QV_Node);
assert(!qv->v.n || dynamic_cast<Result>(qv->v.n));
return reinterpret_cast<Result>(qv->v.n);
}
};
template<>
struct QoreValueCastHelper<bool> {
typedef bool Result;
template<typename QV>
static bool cast(QV *qv, valtype_t type) {
return qv->getAsBool();
}
};
template<>
struct QoreValueCastHelper<double> {
typedef double Result;
template<typename QV>
static double cast(QV *qv, valtype_t type) {
return qv->getAsFloat();
}
};
template<>
struct QoreValueCastHelper<int64> {
typedef int64 Result;
template<typename QV>
static int64 cast(QV *qv, valtype_t type) {
return qv->getAsBigInt();
}
};
} // namespace detail
class QoreSimpleValue {
public:
qore_value_u v;
valtype_t type;
DLLLOCAL void set(int64 i) {
type = QV_Int;
v.i = i;
}
DLLLOCAL void set(double f) {
type = QV_Float;
v.f = f;
}
DLLLOCAL void set(bool b) {
type = QV_Bool;
v.b = b;
}
DLLEXPORT void set(QoreSimpleValue val);
DLLEXPORT void set(AbstractQoreNode* n);
DLLLOCAL QoreSimpleValue& assign(QoreSimpleValue& val) {
set(val);
return *this;
}
DLLLOCAL QoreSimpleValue& assign(int64 i) {
set(i);
return *this;
}
DLLLOCAL QoreSimpleValue& assign(double f) {
set(f);
return *this;
}
DLLLOCAL QoreSimpleValue& assign(bool b) {
set(b);
return *this;
}
DLLLOCAL QoreSimpleValue& assign(AbstractQoreNode* n) {
set(n);
return *this;
}
DLLEXPORT qore_type_t getType() const;
DLLEXPORT const char* getTypeName() const;
DLLEXPORT AbstractQoreNode* takeNode();
DLLEXPORT AbstractQoreNode* getInternalNode();
DLLEXPORT const AbstractQoreNode* getInternalNode() const;
DLLEXPORT void clear();
DLLEXPORT void discard(ExceptionSink* xsink);
template<typename T>
DLLLOCAL T* take() {
assert(type == QV_Node);
assert(dynamic_cast<T*>(v.n));
T* rv = reinterpret_cast<T*>(v.n);
v.n = 0;
return rv;
}
template<typename T>
DLLLOCAL typename detail::QoreValueCastHelper<T>::Result get() {
return detail::QoreValueCastHelper<T>::cast(this, type);
}
template<typename T>
DLLLOCAL typename detail::QoreValueCastHelper<const T>::Result get() const {
return detail::QoreValueCastHelper<const T>::cast(this, type);
}
DLLEXPORT bool getAsBool() const;
DLLEXPORT int64 getAsBigInt() const;
DLLEXPORT double getAsFloat() const;
DLLEXPORT bool hasEffect() const;
DLLEXPORT bool isNothing() const;
DLLEXPORT bool isNull() const;
DLLEXPORT bool isNullOrNothing() const;
DLLEXPORT bool isValue() const;
DLLEXPORT bool needsEval() const;
DLLEXPORT bool isScalar() const;
DLLEXPORT bool isConstant() const;
DLLEXPORT operator bool() const;
protected:
DLLEXPORT AbstractQoreNode* takeNodeIntern();
};
struct QoreValue : public QoreSimpleValue {
friend class ValueHolder;
friend class ValueOptionalRefHolder;
template<typename> friend struct detail::QoreValueCastHelper;
public:
DLLEXPORT QoreValue();
DLLEXPORT QoreValue(bool b);
DLLEXPORT QoreValue(int i);
DLLEXPORT QoreValue(unsigned int i);
DLLEXPORT QoreValue(long i);
DLLEXPORT QoreValue(unsigned long i);
DLLEXPORT QoreValue(unsigned long long i);
DLLEXPORT QoreValue(int64 i);
DLLEXPORT QoreValue(double f);
DLLEXPORT QoreValue(AbstractQoreNode* n);
DLLEXPORT QoreValue(const AbstractQoreNode* n);
DLLEXPORT QoreValue(const QoreSimpleValue& v);
DLLEXPORT QoreValue(const QoreValue& old);
DLLEXPORT void swap(QoreValue& val);
DLLEXPORT void ref() const;
DLLEXPORT QoreValue refSelf() const;
DLLEXPORT AbstractQoreNode* assign(const QoreValue n);
DLLEXPORT AbstractQoreNode* assign(AbstractQoreNode* n);
DLLEXPORT AbstractQoreNode* assign(int64 n);
DLLEXPORT AbstractQoreNode* assign(double n);
DLLEXPORT AbstractQoreNode* assign(bool n);
DLLEXPORT AbstractQoreNode* assignNothing();
DLLEXPORT bool isEqualSoft(const QoreValue v, ExceptionSink* xsink) const;
DLLEXPORT bool isEqualHard(const QoreValue v) const;
DLLEXPORT bool isEqualValue(const QoreValue v);
DLLEXPORT void sanitize();
DLLEXPORT QoreValue& operator=(const QoreValue& n);
DLLEXPORT QoreValue& operator=(const QoreSimpleValue& n);
DLLEXPORT void discard(ExceptionSink* xsink);
DLLEXPORT int getAsString(QoreString& str, int format_offset, ExceptionSink *xsink) const;
DLLEXPORT QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const;
DLLEXPORT QoreValue eval(ExceptionSink* xsink) const;
DLLEXPORT QoreValue eval(bool& needs_deref, ExceptionSink* xsink) const;
//DLLEXPORT QoreValue refSelf() const;
DLLEXPORT AbstractQoreNode* takeIfNode();
DLLEXPORT const QoreTypeInfo* getTypeInfo() const;
DLLEXPORT const QoreTypeInfo* getFullTypeInfo() const;
DLLEXPORT const char* getFullTypeName() const;
DLLEXPORT const char* getFullTypeName(bool with_namespaces) const;
DLLEXPORT bool hasNode() const;
DLLEXPORT bool isReferenceCounted() const;
DLLEXPORT bool derefCanThrowException() const;
};
class ValueHolderBase {
protected:
QoreValue v;
ExceptionSink* xsink;
public:
DLLLOCAL ValueHolderBase(ExceptionSink* xs) : xsink(xs) {
}
DLLLOCAL ValueHolderBase(QoreValue n_v, ExceptionSink* xs) : v(n_v), xsink(xs) {
}
DLLLOCAL QoreValue* operator->() { return &v; }
DLLLOCAL const QoreValue* operator->() const { return &v; }
DLLLOCAL QoreValue& operator*() { return v; }
DLLLOCAL const QoreValue& operator*() const { return v; }
};
class ValueHolder : public ValueHolderBase {
public:
DLLLOCAL ValueHolder(ExceptionSink* xs) : ValueHolderBase(xs) {
}
DLLLOCAL ValueHolder(QoreValue n_v, ExceptionSink* xs) : ValueHolderBase(n_v, xs) {
}
DLLEXPORT ~ValueHolder();
DLLEXPORT QoreValue getReferencedValue();
DLLEXPORT QoreValue release();
DLLLOCAL QoreValue& operator=(QoreValue nv) {
v.discard(xsink);
v = nv;
return v;
}
DLLLOCAL operator bool() const {
return (bool)v;
}
};
class ValueOptionalRefHolder : public ValueHolderBase {
private:
// not implemented
DLLLOCAL QoreValue& operator=(QoreValue& nv);
protected:
bool needs_deref;
public:
DLLLOCAL ValueOptionalRefHolder(QoreValue n_v, bool nd, ExceptionSink* xs) : ValueHolderBase(n_v, xs), needs_deref(nd) {
}
DLLLOCAL ValueOptionalRefHolder(ExceptionSink* xs) : ValueHolderBase(xs), needs_deref(false) {
}
DLLEXPORT ~ValueOptionalRefHolder();
DLLLOCAL bool isTemp() const { return needs_deref; }
DLLLOCAL void clearTemp() {
if (needs_deref)
needs_deref = false;
}
DLLLOCAL operator bool() const {
return (bool)v;
}
DLLLOCAL void setValue(QoreValue nv) {
if (needs_deref) {
v.discard(xsink);
needs_deref = false;
}
v = nv;
}
DLLLOCAL void setValue(QoreValue nv, bool temp) {
if (needs_deref)
v.discard(xsink);
if (needs_deref != temp)
needs_deref = temp;
v = nv;
}
// ensures that the held value is referenced
DLLEXPORT void ensureReferencedValue();
template<typename T>
DLLLOCAL T* takeReferencedNode() {
T* rv = v.take<T>();
if (needs_deref)
needs_deref = false;
else
rv->ref();
return rv;
}
DLLEXPORT QoreValue getReferencedValue();
DLLLOCAL AbstractQoreNode* takeNode(bool& nd) {
if (v.type == QV_Node) {
nd = needs_deref;
return v.takeNodeIntern();
}
nd = true;
return v.takeNode();
}
DLLLOCAL QoreValue takeValue(bool& nd) {
if (v.type == QV_Node) {
nd = needs_deref;
return v.takeNodeIntern();
}
nd = false;
return v;
}
DLLLOCAL void takeValueFrom(ValueOptionalRefHolder& val) {
if (needs_deref)
v.discard(xsink);
v = val.takeValue(needs_deref);
}
DLLEXPORT QoreValue takeReferencedValue();
};
class ValueEvalRefHolder : public ValueOptionalRefHolder {
public:
DLLEXPORT ValueEvalRefHolder(const AbstractQoreNode* exp, ExceptionSink* xs);
DLLEXPORT ValueEvalRefHolder(const QoreValue exp, ExceptionSink* xs);
DLLEXPORT ValueEvalRefHolder(ExceptionSink* xs);
DLLEXPORT int eval(const AbstractQoreNode* exp);
DLLEXPORT int eval(const QoreValue exp);
protected:
DLLLOCAL int evalIntern(const AbstractQoreNode* exp);
DLLLOCAL int evalIntern(const QoreValue exp);
};
DLLEXPORT extern const char* qoreBoolTypeName;
DLLEXPORT extern const char* qoreIntTypeName;
DLLEXPORT extern const char* qoreFloatTypeName;
#endif
QoreValue::QoreValue
DLLEXPORT QoreValue()
creates with no value (i.e. QoreNothingNode)
QoreValue::swap
DLLEXPORT void swap(QoreValue &val)
exchanges the values
ValueHolder::~ValueHolder
DLLEXPORT ~ValueHolder()
dereferences any contained node
ValueHolder::getReferencedValue
DLLEXPORT QoreValue getReferencedValue()
returns a referenced value; caller owns the reference; the current object is left undisturbed ...
qore_value_u::f
double f
for double values
Definition: QoreValue.h:59
ValueEvalRefHolder::evalIntern
DLLLOCAL int evalIntern(const AbstractQoreNode *exp)
evaluates the argument, returns -1 for error, 0 = OK
QoreValue::isEqualSoft
DLLEXPORT bool isEqualSoft(const QoreValue v, ExceptionSink *xsink) const
returns trus if the argument value is equal to the current value with type conversions ...
ValueHolderBase
base class for holding a QoreValue object
Definition: QoreValue.h:446
QoreValue::isReferenceCounted
DLLEXPORT bool isReferenceCounted() const
returns true if the value holds a referenced-counted node
QV_Int
#define QV_Int
for integer values
Definition: QoreValue.h:44
qore_value_u
this is the union that stores values in QoreValue
Definition: QoreValue.h:56
QoreValue::assignNothing
DLLEXPORT AbstractQoreNode * assignNothing()
sets the value of the object to QoreNothingNode and returns any node value held previously ...
ValueHolder::release
DLLEXPORT QoreValue release()
returns a QoreValue object and leaves the current object empty; the caller owns any reference contain...
ValueHolder::ValueHolder
DLLLOCAL ValueHolder(ExceptionSink *xs)
creates an empty object
Definition: QoreValue.h:479
ValueOptionalRefHolder
allows storing a value and setting a boolean flag that indicates if the value should be dereference i...
Definition: QoreValue.h:509
ValueOptionalRefHolder::takeReferencedNode
DLLLOCAL T * takeReferencedNode()
returns the stored node value and leaves the current object empty
Definition: QoreValue.h:568
ValueEvalRefHolder
evaluates an AbstractQoreNode and dereferences the stored value in the destructor ...
Definition: QoreValue.h:613
QoreValue::getAsString
DLLEXPORT int getAsString(QoreString &str, int format_offset, ExceptionSink *xsink) const
appends the string value of the contained node to the string argument with optional formatting ...
AbstractQoreNode
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:57
QoreValue::getFullTypeName
DLLEXPORT const char * getFullTypeName() const
returns a string type description of the full type of the value contained (ex: "nothing" for a null A...
QoreValue::operator=
DLLEXPORT QoreValue & operator=(const QoreValue &n)
assigns a new value
ValueOptionalRefHolder::isTemp
DLLLOCAL bool isTemp() const
returns true if the value is temporary (needs dereferencing)
Definition: QoreValue.h:530
discard
static void discard(AbstractQoreNode *n, ExceptionSink *xsink)
to deref an AbstractQoreNode (when the pointer may be 0)
Definition: QoreLib.h:324
ValueOptionalRefHolder::ValueOptionalRefHolder
DLLLOCAL ValueOptionalRefHolder(QoreValue n_v, bool nd, ExceptionSink *xs)
creates the object with the given values
Definition: QoreValue.h:520
ValueOptionalRefHolder::takeNode
DLLLOCAL AbstractQoreNode * takeNode(bool &nd)
returns the stored AbstractQoreNode pointer and sets the dereference flag as an output variable ...
Definition: QoreValue.h:582
ValueOptionalRefHolder::needs_deref
bool needs_deref
flag indicating if the value should be dereferenced in the destructor or not
Definition: QoreValue.h:516
QoreValue::ref
DLLEXPORT void ref() const
references the contained value if type == QV_Node
QoreString
Qore&#39;s string type supported by the QoreEncoding class.
Definition: QoreString.h:93
ValueHolderBase::xsink
ExceptionSink * xsink
for possible Qore-language exceptions
Definition: QoreValue.h:451
detail::QoreValueCastHelper
used in QoreValue::get()
Definition: QoreValue.h:67
QV_Node
#define QV_Node
for heap-allocated values
Definition: QoreValue.h:46
detail
namespace for implementation details of QoreValue functions
Definition: QoreValue.h:64
QoreValue::derefCanThrowException
DLLEXPORT bool derefCanThrowException() const
returns true if a dereference could theoretically throw an exception (an object is reachable from thi...
QoreValue::getFullTypeInfo
DLLEXPORT const QoreTypeInfo * getFullTypeInfo() const
returns the exact type of the value; i.e. the class type for classes, hashdecl type for hashdecls ...
QoreValue::takeIfNode
DLLEXPORT AbstractQoreNode * takeIfNode()
returns a referenced value; leaving the "this" untouched; the caller owns the reference returned ...
QoreValue::sanitize
DLLEXPORT void sanitize()
converts any node pointers to efficient representations if possible and dereferences the node value c...
ValueOptionalRefHolder::getReferencedValue
DLLEXPORT QoreValue getReferencedValue()
returns a referenced value; caller owns the reference; the current object is not disturbed ...
ValueOptionalRefHolder::takeValue
DLLLOCAL QoreValue takeValue(bool &nd)
returns the stored value and sets the dereference flag as an output variable
Definition: QoreValue.h:592
QV_Bool
#define QV_Bool
for boolean values
Definition: QoreValue.h:43
ValueOptionalRefHolder::setValue
DLLLOCAL void setValue(QoreValue nv)
assigns a new non-temporary value
Definition: QoreValue.h:544
ValueHolderBase::v
QoreValue v
the value held
Definition: QoreValue.h:449
ValueOptionalRefHolder::takeValueFrom
DLLLOCAL void takeValueFrom(ValueOptionalRefHolder &val)
returns the stored value which must be dereferenced if it is a node object (i.e. type == QV_Node) ...
Definition: QoreValue.h:602
QoreValue::isEqualValue
DLLEXPORT bool isEqualValue(const QoreValue v)
returns true of the argument is exactly the same value as the current value, meaning also that if bot...
QoreValue
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275
qore_value_u::n
AbstractQoreNode * n
for all heap-allocated values
Definition: QoreValue.h:60
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
QoreValue::assign
DLLEXPORT AbstractQoreNode * assign(const QoreValue n)
sets the value of the object and returns any node value held previously
QoreValue::getTypeInfo
DLLEXPORT const QoreTypeInfo * getTypeInfo() const
returns the type of the value
ValueOptionalRefHolder::takeReferencedValue
DLLEXPORT QoreValue takeReferencedValue()
returns a QoreValue after incrementing the reference count of any node value stored if necessary ...
ValueHolderBase::operator*
DLLLOCAL QoreValue & operator*()
returns the value being managed
Definition: QoreValue.h:469
int64
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260
ValueHolder::operator=
DLLLOCAL QoreValue & operator=(QoreValue nv)
assigns the object, any currently-held value is dereferenced before the assignment ...
Definition: QoreValue.h:496
qore_value_u::i
int64 i
for integer values
Definition: QoreValue.h:58
ValueHolderBase::ValueHolderBase
DLLLOCAL ValueHolderBase(ExceptionSink *xs)
creates an ampty object
Definition: QoreValue.h:455
QV_Float
#define QV_Float
for floating-point values
Definition: QoreValue.h:45
qore_type_t
int16_t qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode) ...
Definition: common.h:70
QoreValue::eval
DLLEXPORT QoreValue eval(ExceptionSink *xsink) const
evaluates the node and returns the result
ValueEvalRefHolder::ValueEvalRefHolder
DLLEXPORT ValueEvalRefHolder(const AbstractQoreNode *exp, ExceptionSink *xs)
evaluates the exp argument
qore_value_u::b
bool b
for boolean values
Definition: QoreValue.h:57
ValueHolderBase::operator->
DLLLOCAL QoreValue * operator->()
returns the value being managed
Definition: QoreValue.h:463
ValueEvalRefHolder::eval
DLLEXPORT int eval(const AbstractQoreNode *exp)
evaluates the argument, returns -1 for error, 0 = OK
QoreValue::isEqualHard
DLLEXPORT bool isEqualHard(const QoreValue v) const
returns trus if the argument value is equal to the current value without any type conversions ...
ValueHolder
holds an object and dereferences it in the destructor
Definition: QoreValue.h:476
QoreValue::hasNode
DLLEXPORT bool hasNode() const
returns true if the object contains a non-null AbstractQoreNode pointer (ie type == QV_Node && v...
ValueOptionalRefHolder::clearTemp
DLLLOCAL void clearTemp()
sets needs_deref = false
Definition: QoreValue.h:533
QoreValue::refSelf
DLLEXPORT QoreValue refSelf() const
references the contained value if type == QV_Node, returns itself
ValueOptionalRefHolder::ensureReferencedValue
DLLEXPORT void ensureReferencedValue()
QoreValue::discard
DLLEXPORT void discard(ExceptionSink *xsink)
dereferences any contained AbstractQoreNode pointer and sets to 0; does not modify other values ...