minilua::Value class

Represents a value in lua.

You can use most normal C++ operators on these value (+, -, *, /, [], ...). If the operation can't be performed on the actual value type an exception will be thrown. Logical operators are implemented like they work in lua. I.e.

-- Lua Code
"hi" && true == "hi"

There are also variants of all the operators (as methods) that track the origin of the values.

You can get the underlying value either via std::visit(lambdas, value.raw()) or using std::get<T>(value) where T is any of the underlying types.

Most values can not be changed (i.e. if you add two numbers you create a new value). But you can manually change the underlying variant type.

Function and Table are different in that they act like reference (or act like they are behind a std::shared_ptr). I.e. two variables can refer to the same table and function. In the case of function this is irrelevant because (from the outside) they are immutable. However tables can be mutated from multiple variables.

Constructors, destructors, conversion operators

Value()
Creates a Nil value.
Value(Type val)
Creates a value using the underlying variant type.
Value(Nil val)
Creates a value using the given Nil value.
Value(Bool val)
Creates a value using the given Bool value.
Value(bool val)
Creates a value using the given bool value.
Value(Number val)
Creates a value using the given Number value.
Value(int val)
Creates a value using the given int value.
Value(double val)
Creates a value using the given double value.
Value(String val)
Creates a value using the given String value.
Value(std::string val)
Creates a value using the given std::string value.
Value(const char* val)
Creates a value using the given string pointer value.
Value(Table val)
Creates a value using the given Table value.
Value(Function val)
Creates a value using the given Function value.
template<typename Fn, typename = std::enable_if_t<std::is_invocable_v<Fn, CallContext>>>
Value(Fn val)
Overloaded constructor usable with references to lambdas and function pointers.
Value(const Value&, MemoryAllocator* allocator)
Copies the value to another allocator.
Value(const Value&)
Copy constructor.
Value(Value&&)
Move constructor.
operator bool() const explicit
Convertes the value to a bool according to the lua rules.

Public functions

auto operator=(const Value& other) -> Value & -> auto
Copy assignment operator.
auto operator=(Value&& other) -> Value & -> auto
Move assignment operator.
auto raw() -> Type & -> auto
Returns the underlying variant type.
auto raw() const -> const Type & -> auto
Returns the underlying variant type.
auto to_literal() const -> std::string -> auto
Returns the Value as a literal string that can be directly inserted in lua code.
auto is_valid_identifier() const -> bool -> auto
Checks if the value is a string a valid identifier.
auto is_nil() const -> bool -> auto
Check if the value is Nil.
auto is_bool() const -> bool -> auto
Check if the value is a Bool.
auto is_number() const -> bool -> auto
Check if the value is a Number.
auto is_string() const -> bool -> auto
Check if the value is a String.
auto is_table() const -> bool -> auto
Check if the value is a Table.
auto is_function() const -> bool -> auto
Check if the value is a Function.
auto contains_function() const -> bool -> auto
Check if the value is or contains a Function.
auto has_origin() const -> bool -> auto
Check if the value has an Origin.
auto origin() const -> const Origin & -> auto
Return the Origin.
auto remove_origin() const -> Value -> auto
Remove the Origin (i.e. set it to NoOrigin).
auto with_origin(Origin new_origin) const -> Value -> auto
Sets the Origin.
auto type() const -> std::string -> auto
The type of this value as a string.
auto force(const Value& new_value, const std::string& origin = "") const -> std::optional< SourceChangeTree > -> auto
Tries to force this value to become new_value.
auto call(CallContext) const -> CallResult -> auto
Call the value with the given CallContext.
auto bind(CallContext) > -> auto
Prepare to call the value.
template<typename... Args>
auto call(const CallContext& ctx, Args... args) const -> auto
Call the value with the given CallContext and the args.
auto operator[](const Value&) -> Value & -> auto
Access the value of a Table.
auto operator[](const Value&) const -> const Value & -> auto
Access the value of a Table.
auto to_number(const Value& base = Nil(), std::optional<Range> location = std::nullopt) const -> Value -> auto
Converts the value to a Number.
auto to_string(std::optional<Range> location = std::nullopt) const -> Value -> auto
Converts the value to a String.
auto to_bool(std::optional<Range> location = std::nullopt) const -> Value -> auto
Converts the value to a Bool.
auto negate(std::optional<Range> location = std::nullopt) const -> Value -> auto
unary - operator
auto add(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary + operator
auto sub(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary - operator
auto mul(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary * operator
auto div(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary / operator
auto int_div(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
integer division
auto pow(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary ^ operator
auto mod(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary % operator
auto bit_and(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary & operator
auto bit_or(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary | operator
auto bit_xor(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
Bitwise xor.
auto bit_shl(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
Bitwise shift left.
auto bit_shr(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
Bitwise shift right.
auto bit_not(std::optional<Range> location = std::nullopt) const -> Value -> auto
Bitwise not operator.
auto logic_and(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary and operator
auto logic_or(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary - operator
auto invert(std::optional<Range> location = std::nullopt) const -> Value -> auto
unary not operator
auto len(std::optional<Range> location = std::nullopt) const -> Value -> auto
unary # operator
auto equals(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary == operator
auto unequals(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary != operator
auto less_than(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary < operator
auto less_than_or_equal(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary <= operator
auto greater_than(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary > operator
auto greater_than_or_equal(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary >= operator
auto concat(const Value& rhs, std::optional<Range> location = std::nullopt) const -> Value -> auto
binary .. operator

Friends

void swap(Value& self, Value& other)
Swap function.

Function documentation

template<typename Fn, typename = std::enable_if_t<std::is_invocable_v<Fn, CallContext>>>
minilua::Value::Value(Fn val)

Overloaded constructor usable with references to lambdas and function pointers.

See Function.

minilua::Value::Value(const Value&, MemoryAllocator* allocator)

Copies the value to another allocator.

This only has an effect for Tables. Other values are simply copied. Table will be deep copied to the given allocators. That means all nested tables will also be copied.

minilua::Value::operator bool() const explicit

Convertes the value to a bool according to the lua rules.

Nil and false is converted to false. Everything else is converted to true.

auto minilua::Value::raw() -> Type &

Returns the underlying variant type.

Use this with std::visit(lambdas, value.raw()).

auto minilua::Value::raw() const -> const Type &

Returns the underlying variant type.

Use this with std::visit(lambdas, value.raw()).

auto minilua::Value::to_literal() const -> std::string

Returns the Value as a literal string that can be directly inserted in lua code.

Throws a std::runtime_error if the value is a Function.

auto minilua::Value::remove_origin() const -> Value

Remove the Origin (i.e. set it to NoOrigin).

This is a builder style method and creates a new Value.

auto minilua::Value::with_origin(Origin new_origin) const -> Value

Sets the Origin.

This is a builder style method and creates a new Value.

auto minilua::Value::force(const Value& new_value, const std::string& origin = "") const -> std::optional< SourceChangeTree >

Tries to force this value to become new_value.

Does not actually change the value. This will only return a SourceChange that (when applied) would result in the this value being changed to new_value.

The return value should be returned in Functions otherwise this does not have any effect.

This throws an exception if the types of the values didn't match.

auto minilua::Value::call(CallContext) const -> CallResult

Call the value with the given CallContext.

The CallContext should already contain the arguments.

If this value is not a function or a callable table the method will throw an exception.

auto minilua::Value::bind(CallContext) >

Prepare to call the value.

Binds the CallContext and returns a function that can be called with a Vallist.

If this value is not a function or a callable table the method will throw an exception.

template<typename... Args>
auto minilua::Value::call(const CallContext& ctx, Args... args) const

Call the value with the given CallContext and the args.

The arguments will be added to the CallContext before calling the function.

If this value is not a function or a callable table the method will throw an exception.

auto minilua::Value::operator[](const Value&) -> Value &

Access the value of a Table.

If the value is not a Table this throws an exception.

auto minilua::Value::operator[](const Value&) const -> const Value &

Access the value of a Table.

If the value is not a Table this throws an exception.

auto minilua::Value::to_number(const Value& base = Nil(), std::optional<Range> location = std::nullopt) const -> Value

Converts the value to a Number.

If the value is already a number we return it. If it is a string we try to parse it. In all other cases and if parsing fails we return Nil.

If you provide a base the value has to be a string representing an integer in that base. Otherwise Nil is returned.