Lua-API++  2015-02-12-3
Lua-API++ library
Valref Class Reference

Non-owning reference to Lua value. More...

Inheritance diagram for Valref:
Value

Public Member Functions

Life cycle
 Valref (const Valref &) noexcept=default
 Copy constructor. More...
 
template<typename ValueType >
Valrefoperator= (ValueType &&val)
 Assignment. More...
 
Type info and conversion
template<typename T >
cast () const
 Explicit checked conversion to supported types. More...
 
template<typename T >
optcast (const T &backupValue=T()) const noexcept
 Safe conversion to supported types [Lua 5.2- only]. More...
 
template<typename T >
to () const noexcept
 Unchecked conversion to supported types. More...
 
template<typename T >
to (const T &backupValue) const noexcept
 Safe conversion to supported types. More...
 
 operator bool () const
 
 operator int () const
 
 operator unsigned int () const
 
 operator long long () const
 
 operator unsigned long long () const
 
 operator float () const
 
 operator double () const
 
 operator CFunction () const
 
 operator LightUserData () const
 
 operator const char * () const
 
 operator std::string () const
 
template<typename T >
bool is () const noexcept
 Check if the value convertible to given type. More...
 
bool isInteger () const noexcept
 Check if the value is an integer number [Lua 5.3+ only] More...
 
ValueType type () const noexcept
 Actual type of referenced value.
 
Comparisons (made through Lua)
bool operator== (Valobj &&rhs) const
 
bool operator!= (Valobj &&rhs) const
 
bool operator< (Valobj &&rhs) const
 
bool operator> (Valobj &&rhs) const
 
bool operator<= (Valobj &&rhs) const
 
bool operator>= (Valobj &&rhs) const
 
Indexation
Temporary operator[] (Valobj &&index_) const noexcept
 Indexation.
 
Function calls
template<typename... Args>
Temporary operator() (Args &&...args) const noexcept
 Natural call form. More...
 
template<typename... Args>
Temporary call (Args &&...args) const noexcept
 Call method. More...
 
template<typename... Args>
Temporary pcall (Args &&...args) const noexcept
 Protected call method. More...
 
Closure handling
Temporary upvalue (size_t index) const noexcept
 Upvalue access inside closures. More...
 
Valset getUpvalues () const noexcept
 Retrieve upvalues and their names. More...
 
ClosureInfo getClosureInfo () const noexcept
 Retrieve information about closure [Lua 5.2+ only]. More...
 
size_t getClosureInfo () const noexcept
 Retrieve information about closure [Lua 5.1 only]. More...
 
Miscellaneous operations
Temporary len () const noexcept
 Length [Lua 5.2+ only].
 
size_t rawlen () const noexcept
 Raw length (no metamethods) [Lua 5.2+ only].
 
size_t len () const noexcept
 Length [Lua 5.1 only].
 
Temporary mt () const noexcept
 Metatable. More...
 
Temporary linked () const noexcept
 Linked value [Lua 5.3+ only]. More...
 

Related Functions

(Note that these are not member functions.)

Comparisons (made through Lua)
bool operator== (Valobj &&lhs, const Valref &rhs)
 
bool operator!= (Valobj &&lhs, const Valref &rhs)
 
bool operator< (Valobj &&lhs, const Valref &rhs)
 
bool operator> (Valobj &&lhs, const Valref &rhs)
 
bool operator<= (Valobj &&lhs, const Valref &rhs)
 
bool operator>= (Valobj &&lhs, const Valref &rhs)
 
Concatenation
Temporary operator& (Valobj &&lhs, Valobj &&rhs) const noexcept
 Concatenation. More...
 
Arithmetics (Lua 5.2+ only).
Temporary operator- (Valobj &&lhs)
 [Lua 5.2+ only] More...
 
Temporary operator+ (Valobj &&lhs, const Valobj &rhs) noexcept
 [Lua 5.2+ only] More...
 
Temporary operator- (Valobj &&lhs, const Valobj &rhs) noexcept
 [Lua 5.2+ only] More...
 
Temporary operator* (Valobj &&lhs, const Valobj &rhs) noexcept
 [Lua 5.2+ only] More...
 
Temporary operator/ (Valobj &&lhs, const Valobj &rhs) noexcept
 [Lua 5.2+ only] More...
 
Temporary operator% (Valobj &&lhs, const Valobj &rhs) noexcept
 [Lua 5.2+ only] More...
 
Temporary operator^ (Valobj &&lhs, const Valobj &rhs) noexcept
 Power [Lua 5.2+ only]. More...
 
Temporary idiv (Valobj &&lhs, Valobj &&rhs)
 Integer division [Lua 5.3+ only]. More...
 
Binary operations (Lua 5.3+ only)
Temporary band (Valobj &&lhs, Valobj &&rhs)
 Bitwise AND [Lua 5.3+ only]. More...
 
Temporary bor (Valobj &&lhs, Valobj &&rhs)
 Bitwise OR [Lua 5.3+ only]. More...
 
Temporary bxor (Valobj &&lhs, Valobj &&rhs)
 Bitwise XOR [Lua 5.3+ only]. More...
 
Temporary bneg (Valobj &&lhs)
 Bitwise NOT [Lua 5.3+ only]. More...
 
Temporary shl (Valobj &&lhs, Valobj &&rhs)
 Bit shift left [Lua 5.3+ only]. More...
 
Temporary shr (Valobj &&lhs, Valobj &&rhs)
 Bit shift right [Lua 5.3+ only]. More...
 

Detailed Description

Non-owning reference to Lua value.

This is a reference to a specific slot on Lua stack. The operations with Lua value inside that slot are performed through this object.

Note
This class also covers values that are not actually on the stack but are addressed with pseudo-indices.

Constructor & Destructor Documentation

Valref ( const Valref )
defaultnoexcept

Copy constructor.

Copies of Valref point to the same stack slot. Be careful with copying Valref objects just as you would be with normal references. This copy constructor is enabled so it is possible to pass references as function arguments.

Note
Obviously, this operation is not matched in Temporary interface.

Member Function Documentation

Valref& operator= ( ValueType &&  val)

Assignment.

Lua values, supported native types or registered userdata can be assigned to a referenced stack slot.

Note
Some types of Temporary objects may be assigned to (indexers, metatables and so on). Assignment to other kinds (like arithmetic results or length query) has no sense and thus cannot be done.
T cast ( ) const

Explicit checked conversion to supported types.

Call this function with type to be converted to.

Exceptions
std::runtime_errorif the value type is incompatible.
Template Parameters
Tthe type to cast to, one of those:
  • bool (NB: ANY value can be converted to boolean)
  • int
  • unsigned int
  • float
  • double
  • const char*
  • CFunction
  • LightUserData
  • any user data type (see this section), returned by reference
T optcast ( const T &  backupValue = T()) const
noexcept

Safe conversion to supported types [Lua 5.2- only].

Deprecated:
use to function instead.
Returns
Converted value or fallback value if the conversion cannot be made.
Template Parameters
Tthe type to cast to. Converts to all types supported by cast except user data.
T to ( ) const
noexcept

Unchecked conversion to supported types.

Returns
Conversion result or unspecified value if the conversion cannot be made. Use when you are sure what the type of value is and don't want additional checks during conversion.
Template Parameters
Tthe type to cast to. Converts to all types supported by cast including user data.
Note
For userdata type a reference is returned.
T to ( const T &  backupValue) const
noexcept

Safe conversion to supported types.

Returns
Converted value or fallback value if the conversion cannot be made.
Template Parameters
Tthe type to cast to. Converts to all types supported by cast except user data.
operator bool ( ) const

Never fails.

operator int ( ) const
Exceptions
std::runtime_errorif the value type is incompatible.
Note
Strings may be converted to numbers.
operator unsigned int ( ) const
Exceptions
std::runtime_errorif the value type is incompatible.
Note
Strings may be converted to numbers.
operator long long ( ) const
Exceptions
std::runtime_errorif the value type is incompatible.
Note
Strings may be converted to numbers.
operator unsigned long long ( ) const
Exceptions
std::runtime_errorif the value type is incompatible.
Note
Strings may be converted to numbers.
operator float ( ) const
Exceptions
std::runtime_errorif the value type is incompatible.
Note
Strings may be converted to numbers.
operator double ( ) const
Exceptions
std::runtime_errorif the value type is incompatible.
Note
Strings may be converted to numbers.
operator CFunction ( ) const
Exceptions
std::runtime_errorif the value type is incompatible.
operator LightUserData ( ) const
Exceptions
std::runtime_errorif the value type is incompatible.
operator const char * ( ) const
Exceptions
std::runtime_errorif the value type is incompatible.
Note
Numbers are convertible to strings.
operator std::string ( ) const
inline
Exceptions
std::runtime_errorif the value type is incompatible.
Note
Numbers are convertible to strings.
bool is ( ) const
noexcept

Check if the value convertible to given type.

Exception: is<bool> checks if value type is actually boolean, because any value can be converted to bool.

Template Parameters
Tthe type to check compatibility with, one of those:
Note
All numeric types are interchangeable for this function and check only for numeric format. Use isInteger to check if the number is actually of integer subtype.
bool isInteger ( ) const
noexcept

Check if the value is an integer number [Lua 5.3+ only]

Returns
true if the value is a number and has integer subtype, false otherwise (i.e. the value is floating-point or not a number at all).
Temporary operator() ( Args &&...  args) const
noexcept

Natural call form.

Any suitable value is accepted as an argument.

See also
basic_state_multiret_call
Temporary call ( Args &&...  args) const
noexcept

Call method.

Any suitable value is accepted as an argument.

See also
basic_state_multiret_call
Temporary pcall ( Args &&...  args) const
noexcept

Protected call method.

Any suitable value is accepted as an argument.

See also
basic_state_multiret_call
Temporary upvalue ( size_t  index) const
noexcept

Upvalue access inside closures.

You can read and write upvalues inside closures with this function.

Parameters
index1-based index of accessed upvalue.
Precondition
this->is<LFunction>()
Exceptions
std::runtime_erroron wrong index.
Valset getUpvalues ( ) const
noexcept

Retrieve upvalues and their names.

This function returns values and names of all closure upvalues. Upvalues are always named "" in C closures. Upvalue ordering in Lua functions is undefined.

Precondition
this->is<LFunction>()
Returns
Value-name pairs for each upvalue.
ClosureInfo getClosureInfo ( ) const
noexcept

Retrieve information about closure [Lua 5.2+ only].

This function is applicable only to closures. It returns number of upvalues, parameters and whether function is variadic (accepts variable number of arguments).

Precondition
this->is<LFunction>()
Note
For closures made from C functions nParameters is always 0 and variadic is always true.
size_t getClosureInfo ( ) const
noexcept

Retrieve information about closure [Lua 5.1 only].

This function is applicable only to closures.

Returns
Number of upvalues.
Precondition
this->is<LFunction>()
Temporary mt ( ) const
noexcept

Metatable.

Returns
value's metatable or nil.
Temporary linked ( ) const
noexcept

Linked value [Lua 5.3+ only].

Returns
value's linked value or nil.

This function allows to get and set associated value for userdata. For other types it will produce nil (also for userdata that doesn't have anything associated with it) on read attempts and silently ignore write attempts.

Friends And Related Function Documentation

bool operator== ( Valobj &&  lhs,
const Valref rhs 
)
related
bool operator!= ( Valobj &&  lhs,
const Valref rhs 
)
related
bool operator< ( Valobj &&  lhs,
const Valref rhs 
)
related
bool operator> ( Valobj &&  lhs,
const Valref rhs 
)
related
bool operator<= ( Valobj &&  lhs,
const Valref rhs 
)
related
bool operator>= ( Valobj &&  lhs,
const Valref rhs 
)
related
Temporary operator& ( Valobj &&  lhs,
Valobj &&  rhs 
) const
related

Concatenation.

Note
  • Chained concatenations are optimized into a single multi-value concatenation;
  • multiple return values and Valsets are expanded;
  • the operation symbol '&' has lower priority than '+' and '-' because in C/C++ it means "bitwise AND", which matches priority of concatenation in Lua.
Temporary operator- ( Valobj &&  lhs)
related

[Lua 5.2+ only]

Temporary operator+ ( Valobj &&  lhs,
const Valobj rhs 
)
related

[Lua 5.2+ only]

Temporary operator- ( Valobj &&  lhs,
const Valobj rhs 
)
related

[Lua 5.2+ only]

Temporary operator* ( Valobj &&  lhs,
const Valobj rhs 
)
related

[Lua 5.2+ only]

Temporary operator/ ( Valobj &&  lhs,
const Valobj rhs 
)
related

[Lua 5.2+ only]

Temporary operator% ( Valobj &&  lhs,
const Valobj rhs 
)
related

[Lua 5.2+ only]

Temporary operator^ ( Valobj &&  lhs,
const Valobj rhs 
)
related

Power [Lua 5.2+ only].

Warning
The operation symbol '^' has lower priority than '+' and '-' because in C/C++ it means "bitwise XOR", not "power". Use parentheses to group expressions properly.
Temporary idiv ( Valobj &&  lhs,
Valobj &&  rhs 
)
related

Integer division [Lua 5.3+ only].

Temporary band ( Valobj &&  lhs,
Valobj &&  rhs 
)
related

Bitwise AND [Lua 5.3+ only].

Temporary bor ( Valobj &&  lhs,
Valobj &&  rhs 
)
related

Bitwise OR [Lua 5.3+ only].

Temporary bxor ( Valobj &&  lhs,
Valobj &&  rhs 
)
related

Bitwise XOR [Lua 5.3+ only].

Temporary bneg ( Valobj &&  lhs)
related

Bitwise NOT [Lua 5.3+ only].

Temporary shl ( Valobj &&  lhs,
Valobj &&  rhs 
)
related

Bit shift left [Lua 5.3+ only].

Temporary shr ( Valobj &&  lhs,
Valobj &&  rhs 
)
related

Bit shift right [Lua 5.3+ only].