Qore Programming Language 2.3.0
Loading...
Searching...
No Matches
QoreValue Class Reference

The main value class in Qore, designed to be passed by value. More...

#include <QoreValue.h>

Public Member Methods

DLLLOCAL constexpr QoreValue () noexcept
 Default constructor: initializes to NOTHING (safe)
 
DLLEXPORT QoreValue (AbstractQoreNode *n)
 Creates a value from an AbstractQoreNode pointer (takes reference)
 
DLLLOCAL constexpr QoreValue (bool b) noexcept
 Creates a boolean value.
 
DLLEXPORT QoreValue (const AbstractQoreNode *n)
 Creates a value from a const AbstractQoreNode pointer (sanitizes and may take reference)
 
constexpr QoreValue (const QoreValue &other) noexcept=default
 Copy constructor (trivial - just copies bits)
 
DLLEXPORT QoreValue (double f)
 Creates a floating-point value.
 
DLLEXPORT QoreValue (int i)
 Creates an integer value (stores inline if in 48-bit range, otherwise allocates QoreBigIntNode)
 
DLLEXPORT QoreValue (int64 i)
 Creates an integer value.
 
DLLEXPORT QoreValue (long i)
 Creates an integer value.
 
DLLLOCAL constexpr QoreValue (QoreSimpleValue sv) noexcept
 Construct from QoreSimpleValue (implicit conversion)
 
DLLEXPORT QoreValue (unsigned int i)
 Creates an integer value.
 
DLLEXPORT QoreValue (unsigned long i)
 Creates an integer value.
 
DLLEXPORT QoreValue (unsigned long long i)
 Creates an integer value.
 
DLLEXPORT AbstractQoreNodeassign (AbstractQoreNode *n)
 Assigns a node value and returns any previous node value.
 
DLLEXPORT AbstractQoreNodeassign (bool n)
 Assigns a boolean and returns any previous node value.
 
DLLEXPORT AbstractQoreNodeassign (const QoreValue &n)
 Assigns a new value and returns any previous node value.
 
DLLEXPORT AbstractQoreNodeassign (double n)
 Assigns a double and returns any previous node value.
 
DLLEXPORT AbstractQoreNodeassign (int64 n)
 Assigns an integer and returns any previous node value.
 
DLLEXPORT AbstractQoreNodeassignNothing ()
 Assigns NOTHING and returns any previous node value.
 
DLLEXPORT void clear ()
 Sets the value to NOTHING (does not dereference any current node)
 
DLLEXPORT bool derefCanThrowException () const
 Returns true if dereferencing could throw an exception.
 
DLLEXPORT void discard (ExceptionSink *xsink)
 Dereferences any contained node and sets to NOTHING.
 
DLLEXPORT QoreValue eval (bool &needs_deref, ExceptionSink *xsink) const
 Evaluates the value and returns the result with deref flag.
 
DLLEXPORT QoreValue eval (ExceptionSink *xsink) const
 Evaluates the value and returns the result.
 
template<typename T >
DLLLOCAL std::enable_if< std::is_pointer< T >::value, T >::type get ()
 
template<typename T >
DLLLOCAL std::enable_if<!std::is_pointer< T >::value &&std::is_class< T >::value, T * >::type get ()
 
template<typename T >
DLLLOCAL std::enable_if<!std::is_pointer< T >::value &&std::is_class< T >::value, T * >::type get () const
 
template<typename T >
DLLLOCAL std::enable_if< std::is_same< T, int64 >::value, T >::type get () const
 Returns the value as the given type with conversion (specialization for int64)
 
template<typename T >
DLLLOCAL std::enable_if< std::is_same< T, double >::value, T >::type get () const
 Returns the value as the given type with conversion (specialization for double)
 
template<typename T >
DLLLOCAL std::enable_if< std::is_same< T, bool >::value, T >::type get () const
 Returns the value as the given type with conversion (specialization for bool)
 
DLLEXPORT int64 getAsBigInt () const
 Returns the value as a 64-bit integer with type conversion.
 
DLLEXPORT bool getAsBool () const
 Returns the value as a boolean with type conversion.
 
DLLEXPORT double getAsFloat () const
 Returns the value as a double with type conversion.
 
DLLEXPORT QoreStringgetAsString (bool &del, int foff, ExceptionSink *xsink) const
 Returns string representation.
 
DLLEXPORT int getAsString (QoreString &str, int format_offset, ExceptionSink *xsink) const
 Appends string representation to the given QoreString.
 
DLLLOCAL bool getBool () const
 Extracts boolean value (asserts if not a bool)
 
DLLLOCAL double getDouble () const
 Extracts double value (asserts if not a double)
 
DLLEXPORT const QoreTypeInfo * getFullTypeInfo () const
 Returns the full QoreTypeInfo for the value.
 
DLLEXPORT const char * getFullTypeName () const
 Returns the full type name.
 
DLLEXPORT const char * getFullTypeName (bool with_namespaces) const
 Returns the full type name with optional namespace paths.
 
DLLEXPORT const char * getFullTypeName (bool with_namespaces, QoreString &scratch) const
 Returns the full type name with optional namespace paths and scratch buffer.
 
DLLLOCAL int64 getInt () const
 Extracts inline 48-bit integer value (asserts if not an int)
 
DLLEXPORT AbstractQoreNodegetInternalNode ()
 Returns the internal AbstractQoreNode pointer (nullptr if not a node type)
 
DLLEXPORT const AbstractQoreNodegetInternalNode () const
 Returns the internal AbstractQoreNode pointer (nullptr if not a node type)
 
DLLLOCAL AbstractQoreNodegetPtr () const
 Extracts pointer value (asserts if not a pointer)
 
DLLEXPORT void getShortString (char *buf) const
 Extract short string into buffer (must have space for at least 7 bytes)
 
DLLEXPORT qore_type_t getType () const
 Returns the type code of the value.
 
DLLEXPORT const QoreTypeInfo * getTypeInfo () const
 Returns the QoreTypeInfo for the value.
 
DLLEXPORT const char * getTypeName () const
 Returns the type name as a string.
 
DLLEXPORT bool hasEffect () const
 Returns true if the value has side effects.
 
DLLEXPORT bool hasNode () const
 Returns true if the value contains a non-null AbstractQoreNode pointer.
 
DLLLOCAL bool isBool () const
 Returns true if the value is a boolean (true or false)
 
DLLEXPORT bool isConstant () const
 Returns true if the value is a constant (does not require evaluation)
 
DLLEXPORT bool isEqualHard (const QoreValue &other) const
 Hard comparison (no type conversion)
 
DLLEXPORT bool isEqualSoft (const QoreValue &other, ExceptionSink *xsink) const
 Soft comparison (with type conversion)
 
DLLEXPORT bool isEqualValue (const QoreValue &other)
 Value comparison (checks if same pointer for nodes)
 
DLLLOCAL bool isFalse () const
 Returns true if the value is boolean false.
 
DLLLOCAL bool isFloat () const
 Returns true if the value is a double.
 
DLLLOCAL bool isInt () const
 Returns true if the value is an inline 48-bit integer.
 
DLLEXPORT bool isNothing () const
 Returns true if the value is NOTHING.
 
DLLEXPORT bool isNull () const
 Returns true if the value is NULL.
 
DLLEXPORT bool isNullOrNothing () const
 Returns true if the value is NOTHING or NULL.
 
DLLLOCAL bool isPointer () const
 Returns true if the value is a pointer to AbstractQoreNode.
 
DLLEXPORT bool isReferenceCounted () const
 Returns true if the value is reference-counted.
 
DLLEXPORT bool isScalar () const
 Returns true if the value is a scalar (int, bool, float, number, string)
 
DLLLOCAL bool isShortString () const
 Returns true if the value is a short string stored inline.
 
DLLLOCAL bool isSpecial () const
 Returns true if the value is a special value (nothing, null, true, false)
 
DLLLOCAL bool isTrue () const
 Returns true if the value is boolean true.
 
DLLEXPORT bool isValue () const
 Returns true if the object holds a value (not a parse expression)
 
DLLEXPORT bool needsEval () const
 Returns true if the value needs evaluation.
 
DLLLOCAL operator bool () const
 Returns true if the value is not NOTHING.
 
QoreValueoperator= (const QoreValue &n) noexcept=default
 Assignment operator (trivial - just copies bits)
 
DLLLOCAL uint64_t rawBits () const
 Returns the raw 64-bit representation.
 
DLLEXPORT void ref () const
 Increments reference count if the value is a node.
 
DLLEXPORT QoreValue refSelf () const
 Increments reference count if the value is a node and returns *this.
 
DLLEXPORT void sanitize ()
 Converts node values to efficient inline representation if possible.
 
DLLEXPORT void set (AbstractQoreNode *n)
 Sets a node value (any current value is overwritten without dereferencing)
 
DLLLOCAL void set (bool b)
 Sets a boolean value (any current value is overwritten without dereferencing)
 
DLLEXPORT void set (const QoreValue &val)
 Sets a value from another QoreValue (any current value is overwritten without dereferencing)
 
DLLEXPORT void set (double f)
 Sets a double value (any current value is overwritten without dereferencing)
 
DLLLOCAL void set (int64 i)
 Sets an integer value (any current value is overwritten without dereferencing)
 
DLLLOCAL bool shortStringEquals (const QoreValue &other) const
 Fast short string comparison (single uint64 compare)
 
DLLLOCAL size_t shortStringLen () const
 Get length of short string (asserts if not a short string)
 
DLLEXPORT void swap (QoreValue &val)
 Swaps values with another QoreValue.
 
template<typename T >
DLLLOCAL T * take ()
 Returns a pointer to an object of the given class (takes the pointer)
 
DLLEXPORT AbstractQoreNodetakeIfNode ()
 Takes and returns the node value if present, otherwise returns nullptr.
 
DLLEXPORT AbstractQoreNodetakeNode ()
 Takes and returns the node value, leaving this empty (asserts if not a node)
 

Static Public Member Methods

static DLLLOCAL bool fitsInline (int64 i)
 Returns true if the given integer fits in inline storage.
 
static DLLLOCAL QoreValue makeFalse ()
 Creates a FALSE value.
 
static DLLLOCAL QoreValue makeNothing ()
 Creates a NOTHING value.
 
static DLLLOCAL QoreValue makeNull ()
 Creates a NULL value.
 
static DLLEXPORT QoreValue makeShortString (const char *str, size_t len)
 Create a short string (asserts if too long)
 
static DLLLOCAL QoreValue makeTrue ()
 Creates a TRUE value.
 
static DLLEXPORT bool tryMakeShortString (QoreValue &out, const char *str, size_t len)
 Try to create a short string inline. Returns false if string is too long.
 

Static Public Attributes

static constexpr int64 InlineIntMax = INT48_MAX
 Maximum value that can be stored inline (larger values use QoreBigIntNode)
 
static constexpr int64 InlineIntMin = INT48_MIN
 Minimum value that can be stored inline (larger values use QoreBigIntNode)
 

Friends

class ValueEvalRefHolder
 
class ValueHolder
 
class ValueOptionalRefHolder
 

Detailed Description

The main value class in Qore, designed to be passed by value.

This class uses NaN-boxing to store multiple types in a single 64-bit word:

  • doubles stored with offset encoding
  • 48-bit signed integers stored inline
  • short strings (up to 6 bytes) stored inline
  • pointers to AbstractQoreNode for heap-allocated values
  • special values (nothing, null, true, false)

Constructor & Destructor Documentation

◆ QoreValue()

DLLLOCAL constexpr QoreValue::QoreValue ( )
inlineconstexprnoexcept

Default constructor: initializes to NOTHING (safe)

Unlike QoreSimpleValue, QoreValue's default constructor is safe and initializes the value to NOTHING. Use QoreSimpleValue only in unions or varargs where trivial construction is required.

Member Function Documentation

◆ get() [1/3]

template<typename T >
DLLLOCAL std::enable_if< std::is_pointer< T >::value, T >::type QoreValue::get ( )
inline

Returns the value as the given type (for pointer types, returns T directly) Returns nullptr if the value is not a pointer (e.g., NOTHING, inline int/float/bool)

References isPointer().

Referenced by AbstractQoreThreadResource::cleanup(), get_hard_value_or_nothing_param(), ValueHolder::releaseAs(), and test_reference_param().

◆ get() [2/3]

template<typename T >
DLLLOCAL std::enable_if<!std::is_pointer< T >::value &&std::is_class< T >::value, T * >::type QoreValue::get ( )
inline

Returns the value as T* (for non-pointer types like const ReferenceNode, returns pointer to T) Returns nullptr if the value is not a pointer (e.g., NOTHING, inline int/float/bool)

References isPointer().

◆ get() [3/3]

template<typename T >
DLLLOCAL std::enable_if<!std::is_pointer< T >::value &&std::is_class< T >::value, T * >::type QoreValue::get ( ) const
inline

Returns the value as T* const version (for non-pointer types like const ReferenceNode) Returns nullptr if the value is not a pointer (e.g., NOTHING, inline int/float/bool)

References isPointer().

◆ set()

DLLEXPORT void QoreValue::set ( double  f)

Sets a double value (any current value is overwritten without dereferencing)

For most doubles, stores them inline using offset encoding. However, doubles with bit patterns >= 0xFFF8000000000000 (including negative NaN values) would collide with internal NaN-boxing tags after encoding, so they are stored as QoreFloatNode instead.


The documentation for this class was generated from the following file: