32 #ifndef _QORE_INTERN_QORELVALUE_H 34 #define _QORE_INTERN_QORELVALUE_H 38 template <
typename U = qore_value_u>
42 template <
class T,
typename t,
int nt>
49 return reinterpret_cast<T*
>(v.n =
new T);
51 if (v.n->getType() != nt) {
52 t i = T::getValue(v.n);
54 return reinterpret_cast<T*
>(v.n =
new T(i));
58 return reinterpret_cast<T*>(v.n);
61 return reinterpret_cast<T*
>((v.n = old->
realCopy()));
64 DLLLOCAL
void reset() {
65 assert(!assigned || type !=
QV_Node || !v.n);
68 assert(!static_assignment);
80 bool static_assignment : 1;
85 DLLLOCAL QoreLValue() : type(
QV_Node), fixed_type(false), assigned(false), static_assignment(false), is_closure(false) {
92 DLLLOCAL QoreLValue(valtype_t t) : type(t), fixed_type(t !=
QV_Node), assigned(false), static_assignment(false), is_closure(false) {
101 DLLLOCAL QoreLValue(
const QoreTypeInfo* typeInfo) : assigned(false), static_assignment(false), is_closure(false) {
108 DLLLOCAL QoreLValue(
const QoreLValue<U>& old) : type(old.type), fixed_type(old.fixed_type), assigned(old.assigned), static_assignment(false), is_closure(old.is_closure) {
112 case QV_Bool: v.b = old.v.b;
break;
113 case QV_Int: v.i = old.v.i;
break;
114 case QV_Float: v.f = old.v.f;
break;
116 v.n = old.v.n ? old.v.n->refSelf() :
nullptr;
118 check_lvalue_object_in_out(v.n, 0);
120 default: assert(
false);
126 DLLLOCAL ~QoreLValue() {
127 assert(!assigned || type !=
QV_Node || !v.n);
131 DLLLOCAL
void setClosure() {
136 DLLLOCAL valtype_t getOptimizedType()
const {
137 return fixed_type ? type :
QV_Node;
140 DLLLOCAL
bool optimized()
const {
149 DLLLOCAL
typename detail::QoreValueCastHelper<T>::Result
get() {
158 DLLLOCAL
typename detail::QoreValueCastHelper<const T>::Result
get()
const {
162 DLLLOCAL
const char* getFixedTypeName()
const {
166 case QV_Int:
return "int";
174 DLLLOCAL
bool isInt() {
190 DLLLOCAL
bool isFloat() {
207 assert(!fixed_type || type ==
QV_Int);
231 int64 i = v.n ? v.n->getAsBigInt() : 0;
236 check_lvalue_object_in_out(0, rv);
251 assert(!fixed_type || type ==
QV_Float);
275 double f = v.n ? v.n->getAsFloat() : 0.0;
280 check_lvalue_object_in_out(0, rv);
333 check_lvalue_object_in_out(0, rv);
349 assert(!static_assignment);
357 check_lvalue_object_in_out(0, v.n);
359 default: assert(
false);
365 DLLLOCAL
const QoreValue getValue()
const {
374 default: assert(
false);
389 default: assert(
false);
395 DLLLOCAL
QoreValue getReferencedValue()
const {
399 DLLLOCAL
bool needsScan()
const {
400 if (!assigned || type !=
QV_Node || !v.n)
403 return needs_scan(v.n);
407 DLLLOCAL
QoreValue getReferencedValue(
bool& needs_deref)
const {
424 default: assert(
false);
436 check_lvalue_object_in_out(0, v.n);
441 DLLLOCAL
bool hasValue()
const {
446 case QV_Int:
return (
bool)v.i;
449 default: assert(
false);
455 DLLLOCAL
void set(
const QoreTypeInfo* typeInfo) {
456 if (typeInfo == bigIntTypeInfo || typeInfo == softBigIntTypeInfo)
458 else if (typeInfo == floatTypeInfo || typeInfo == softFloatTypeInfo)
460 else if (typeInfo == boolTypeInfo || typeInfo == softBoolTypeInfo)
466 DLLLOCAL
void set(valtype_t t) {
467 assert(!assigned || type !=
QV_Node || !v.n);
482 case QV_Bool: v.b =
false;
return 0;
484 case QV_Float: v.f = (double)b;
return 0;
485 default: assert(
false);
494 check_lvalue_object_in_out(0, v.n);
516 case QV_Bool: v.b = (bool)i;
return 0;
517 case QV_Int: v.i = i;
return 0;
518 case QV_Float: v.f = (double)i;
return 0;
519 default: assert(
false);
528 check_lvalue_object_in_out(0, v.n);
550 case QV_Bool: v.b = (bool)f;
return 0;
553 default: assert(
false);
562 check_lvalue_object_in_out(0, v.n);
583 DLLLOCAL
void assignSetTakeInitial(QoreLValue<U>& n,
QoreValue val) {
587 if (n.static_assignment) {
588 static_assignment = n.static_assignment;
589 n.static_assignment =
false;
600 assert(val.getType() ==
NT_INT);
616 n.v.n = val.takeNode();
625 DLLLOCAL
void assignSetTakeInitial(QoreLValue<U>& n) {
631 if (n.static_assignment) {
632 static_assignment = n.static_assignment;
633 n.static_assignment =
false;
636 case QV_Bool: v.b = n.v.b; n.v.b =
false;
break;
637 case QV_Int: v.i = n.v.i; n.v.i = 0;
break;
638 case QV_Float: v.f = n.v.f; n.v.f = 0;
break;
647 default: assert(
false);
657 assert(!static_assignment);
659 if (is_static_assignment)
660 static_assignment =
true;
661 return assignAssume(n);
664 DLLLOCAL
void assignInitial(
bool n) {
671 case QV_Float: v.f = (double)n;
return;
672 default: assert(
false);
680 DLLLOCAL
void assignInitial(
int64 n) {
685 case QV_Bool: v.b = (bool)n;
return;
686 case QV_Int: v.i = n;
return;
687 case QV_Float: v.f = (double)n;
return;
688 default: assert(
false);
696 DLLLOCAL
void assignInitial(
double n) {
701 case QV_Bool: v.b = (bool)n;
return;
704 default: assert(
false);
718 v.b = n ? n->
getAsBool() :
false;
return n;
721 default: assert(
false);
728 check_lvalue_object_in_out(v.n, 0);
734 return assignAssume(n);
743 case QV_Bool: v.b = val.getAsBool();
break;
744 case QV_Int: v.i = val.getAsBigInt();
break;
745 case QV_Float: v.f = val.getAsFloat();
break;
746 default: assert(
false);
756 check_lvalue_object_in_out(0, v.n);
772 v.n = val.takeNode();
776 check_lvalue_object_in_out(v.n, 0);
778 default: assert(
false);
818 check_lvalue_object_in_out(n, v.n);
834 check_lvalue_object_in_out(0, v.n);
847 check_lvalue_object_in_out(v.n, 0);
854 DLLLOCAL
bool exists()
const {
858 DLLLOCAL
bool getAsBool()
const {
862 case QV_Int:
return (
bool)v.i;
864 case QV_Node:
return v.n ? v.n->getAsBool() :
false;
865 default: assert(
false);
872 DLLLOCAL
int64 getAsBigInt()
const {
878 case QV_Node:
return v.n ? v.n->getAsBigInt() : 0;
879 default: assert(
false);
886 DLLLOCAL
double getAsFloat()
const {
889 case QV_Bool:
return (
double)v.b;
890 case QV_Int:
return (
double)v.i;
892 case QV_Node:
return v.n ? v.n->getAsFloat() : 0.0;
893 default: assert(
false);
901 return assigned && type ==
QV_Node ? v.n :
nullptr;
911 default: assert(
false);
917 DLLLOCAL
const char* getTypeName()
const {
920 case QV_Bool:
return qoreBoolTypeName;
921 case QV_Int:
return qoreIntTypeName;
922 case QV_Float:
return qoreFloatTypeName;
923 case QV_Node:
return get_type_name(v.n);
924 default: assert(
false);
1060 return i ? v.i %= i : v.i = 0;
1378 DLLLOCAL
QoreValue remove(
bool& was_static_assignment) {
1379 assert(!was_static_assignment);
1384 if (static_assignment) {
1385 static_assignment =
false;
1386 was_static_assignment =
true;
1398 check_lvalue_object_in_out(0, v.n);
1408 DLLLOCAL
void unassignIgnore() {
1411 if (static_assignment)
1412 static_assignment =
false;
1413 if (type ==
QV_Node && !is_closure)
1414 check_lvalue_object_in_out(0, v.n);
1418 DLLLOCAL
QoreValue removeValue(
bool for_del) {
1422 assert(!static_assignment);
1426 return for_del ?
QoreValue() : QoreValue(v.b);
1428 return for_del ? QoreValue() : QoreValue(v.i);
1430 return for_del ? QoreValue() : QoreValue(v.f);
1433 check_lvalue_object_in_out(0, v.n);
1443 typedef QoreLValue<> QoreLValueGeneric;
Qore's arbitrary-precision number value type, dynamically-allocated only, reference counted...
Definition: QoreNumberNode.h:51
#define QV_Int
for integer values
Definition: QoreValue.h:44
DLLEXPORT bool getAsBool() const
returns the boolean value of the object
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:54
const qore_type_t NT_NOTHING
type value for QoreNothingNode
Definition: node_types.h:42
static void discard(AbstractQoreNode *n, ExceptionSink *xsink)
to deref an AbstractQoreNode (when the pointer may be 0)
Definition: QoreLib.h:323
used in QoreValue::get()
Definition: QoreValue.h:67
static bool is_nothing(const AbstractQoreNode *n)
to check if an AbstractQoreNode object is NOTHING
Definition: QoreLib.h:315
#define QV_Node
for heap-allocated values
Definition: QoreValue.h:46
DLLEXPORT AbstractQoreNode * takeIfNode()
returns a referenced value; leaving the "this" untouched; the caller owns the reference returned ...
#define QV_Bool
for boolean values
Definition: QoreValue.h:43
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
virtual DLLEXPORT AbstractQoreNode * realCopy() const =0
returns a copy of the object; the caller owns the reference count
DLLEXPORT int64 getAsBigInt() const
returns the 64-bit integer value of the object
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
const qore_type_t NT_FLOAT
type value for floating-point values (QoreValue only)
Definition: node_types.h:44
const qore_type_t NT_BOOLEAN
type value for bools (QoreValue only)
Definition: node_types.h:47
const qore_type_t NT_INT
type value for integers (QoreValue only)
Definition: node_types.h:43
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
DLLEXPORT double getAsFloat() const
returns the float value of the object
#define QV_Float
for floating-point values
Definition: QoreValue.h:45
int16_t qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode) ...
Definition: common.h:70
#define QV_Ref
for references (when used with lvalues)
Definition: QoreValue.h:47
const qore_type_t NT_NUMBER
type value for QoreNumberNode
Definition: node_types.h:53
DLLEXPORT QoreValue refSelf() const
references the contained value if type == QV_Node, returns itself