Skip to content

atl/value.hpp

atl::Value: a handle to a Lua value, and the conversions between C++ and Lua types.

#define ATL_OBJECT(T)

Declares the constructors every Object subclass needs.

#define ATL_GLOBAL(path)

A Value for a global path, looked up once and cached, e.g. ATL_GLOBAL("sm.game.getDifficulty").call<int>().

inline String toString(const Value &v);

Lua's tostring() of v, for format().

Parameters

  • v (const Value &)

Returns String

inline constexpr Nil nil{};

The Lua type of a Value. Game objects (Shape, Vec3, ...) are Userdata.

enum class Type : int {
Nil = ATL_TYPE_NIL,
Boolean = ATL_TYPE_BOOLEAN,
Number = ATL_TYPE_NUMBER,
String = ATL_TYPE_STRING,
Table = ATL_TYPE_TABLE,
Function = ATL_TYPE_FUNCTION,
Userdata = ATL_TYPE_USERDATA,
Thread = ATL_TYPE_THREAD,
};

Converts between T and Lua. Specialize it to pass your own types to and from Lua: provide static detail::Arg toArg(const T &) and/or static T from(const Value &).

template <class T, class Enable = void> struct Convert

A handle to any Lua value: tables, functions, game objects, strings... The handle is freed when the Value is destroyed; copies get their own handle.

class Value
Value() = defaul;

Nil.

Value(const Value &o);

Nil.

Parameters

  • o (const Value &)
Value(Value &&o) noexcept;

Nil.

Parameters

  • o (Value &&)
Value &operator=(Value o) noexcept;

Parameters

  • o (Value)

Returns Value &

static Value adopt(atl_value h);

Takes ownership of a raw handle.

Parameters

  • h (atl_value)

Returns Value

static Value borrow(atl_value h);

Wraps a raw handle without taking ownership; it isn't freed.

Parameters

  • h (atl_value)

Returns Value

atl_value release();

Gives up ownership and returns a raw handle the caller must free.

Returns atl_value

static Value global(StringView path);

Looks up a dotted global path, e.g. Value::global("sm.vec3.new").

Parameters

  • path (StringView)

Returns Value

static Value table();

A new empty table.

Returns Value

template <class T> static Value from(const T &v); atl_value handle() const { return h_; } bool isNil() const { return h_ == 0; } explicit operator bool() const { return h_ != 0; } Type type() const { return static_cast<Type>(atl_type(h_)); } bool is(Type t) const { return type() == t; } bool truthy() const { return h_ && atl_to_bool(h_); } double toNumber() const { return atl_to_number(h_); } String toString() const { char buf[256]; int n = atl_to_string(h_, buf, sizeof buf); if (n <= int(sizeof buf)) return String(buf, n); String s; s.reserve(n); char *big = static_cast<char *>(malloc(n)); atl_to_string(h_, big, n); s.append(StringView(big, n)); free(big); return s; } template <class T> T as() const { return Convert<T>::from(*this); } Value operator[](StringView field) const { return adopt(atl_raw_get_field(h_, field.data(), static_cast<int>(field.size()))); } Value operator[](int index) const { return adopt(atl_get_index(h_, index)); } Value get(const Value &key) const { return adopt(atl_get(h_, key.h_)); } template <class T> T get(StringView field) const { return (*this)[field].template as<T>(); } double number(StringView field) const { return atl_raw_get_number(h_, field.data(), static_cast<int>(field.size())); } template <class T> Value &set(StringView field, const T &v) { Value val = Value::from(v); atl_raw_set_field(h_, field.data(), static_cast<int>(field.size()), val.h_); return *this; } template <class T> Value &set(int index, const T &v) { Value val = Value::from(v); atl_set_index(h_, index, val.h_); return *this; } int length() const { return atl_len(h_); } template <class T> Value &append(const T &v) { return set(length() + 1, v); } class PairRange pairs() const; template <class R = Value, class... A> R call(const A &...args) const { int n = detail::invoke(h_, args...); if (n < 0) detail::luaError("atl: call failed"); return detail::firstResult<R>(n); } template <class R = Value, class... A> R method(StringView name, const A &...args) const { int n = detail::invokeMethod(h_, name, args...); if (n < 0) detail::luaError(String(":") + name); return detail::firstResult<R>(n); } template <class... A> Optional<Vector<Value>> tryCall(const A &...args) const { int n = detail::invoke(h_, args...); if (n < 0) return none; Vector<Value> out; for (int i = 0; i < n; i++) out.push(adopt(atl_result(i))); return out; } template <class... A> Vector<Value> methodAll(StringView name, const A &...args) const { int n = detail::invokeMethod(h_, name, args...); if (n < 0) detail::luaError(String(":") + name); Vector<Value> out; for (int i = 0; i < n; i++) out.push(adopt(atl_result(i))); return out; } template <class... A> Vector<Value> callAll(const A &...args) const { int n = detail::invoke(h_, args...); if (n < 0) detail::luaError("atl: call failed"); Vector<Value> out; for (int i = 0; i < n; i++) out.push(adopt(atl_result(i))); return out; } friend bool operator==(const Value &a, const Value &b) { return atl_equals(a.h_, b.h_) != 0; } private: atl_value h_ = 0; bool owned_ = true; }; inline String toString(const Value &v) { return v.toString(); } struct Pair { Value key, value; }; class PairIterator { public: PairIterator(const Value *t, Value key) : t_(t), key_(move(key)) { load(); } bool operator!=(const PairIterator &) const { return !key_.isNil(); } void operator++() { key_ = Value::adopt(atl_next(t_->handle(), key_.handle())); load(); } const Pair &operator*() const { return pair_; } private: void load() { if (!key_.isNil()) pair_ = {key_, t_->get(key_)}; } const Value *t_; Value key_; Pair pair_; }; class PairRange { public: explicit PairRange(const Value *t) : t_(t) {} PairIterator begin() const { return PairIterator(t_, Value::adopt(atl_next(t_->handle(), 0))); } PairIterator end() const { return PairIterator(t_, Value()); } private: const Value *t_; }; inline PairRange Value::pairs() const { return PairRange(this); } struct Nil {}; inline constexpr Nil nil{}; template <> struct Convert<Value> { static detail::Arg toArg(const Value &v) { return detail::handleArg(v.handle()); } static Value from(const Value &v) { return v; } }; template <> struct Convert<Nil> { static detail::Arg toArg(const Nil &) { return detail::Arg(); } }; template <> struct Convert<bool> { static detail::Arg toArg(bool b) { return detail::boolArg(b); } static bool from(const Value &v) { return v.truthy(); } }; template <class T> inline constexpr bool is_number_v = is_same_v<T, double> || is_same_v<T, float> || is_same_v<T, int> || is_same_v<T, unsigned> || is_same_v<T, long> || is_same_v<T, unsigned long> || is_same_v<T, long long> || is_same_v<T, int64_t> || is_same_v<T, uint64_t> || is_same_v<T, int16_t> || is_same_v<T, uint16_t> || is_same_v<T, int8_t> || is_same_v<T, uint8_t>; template <class T> struct Convert<T, enable_if_t<is_number_v<T>>> { static detail::Arg toArg(T x) { return detail::numberArg(static_cast<double>(x)); } static T from(const Value &v) { return static_cast<T>(v.toNumber()); } }; template <class T> struct Convert<T, enable_if_t<is_enum_v<T>>> { static detail::Arg toArg(T x) { return detail::numberArg(static_cast<double>(static_cast<long long>(x))); } static T from(const Value &v) { return static_cast<T>(static_cast<long long>(v.toNumber())); } }; template <> struct Convert<StringView> { static detail::Arg toArg(StringView s) { return detail::stringArg(s); } }; template <> struct Convert<String> { static detail::Arg toArg(const String &s) { return detail::stringArg(s.view()); } static String from(const Value &v) { return v.isNil() ? String() : v.toString(); } }; template <size_t N> struct Convert<char[N]> { static detail::Arg toArg(const char (&s)[N]) { return detail::stringArg(StringView(s, N - 1)); } }; template <> struct Convert<const char *> { static detail::Arg toArg(const char *s) { return detail::stringArg(StringView(s)); } }; template <class T> struct Convert<Optional<T>> { static detail::Arg toArg(const Optional<T> &o) { return o ? detail::toArg(*o) : detail::Arg(); } static Optional<T> from(const Value &v) { if (v.isNil()) return none; return Convert<T>::from(v); } }; template <class T> struct Convert<Vector<T>> { static detail::Arg toArg(const Vector<T> &vec) { Value t = Value::table(); for (size_t i = 0; i < vec.size(); i++) t.set(static_cast<int>(i + 1), vec[i]); return detail::ownedArg(t.release()); } static Vector<T> from(const Value &v) { Vector<T> out; if (!v.is(Type::Table)) return out; int n = v.length(); out.reserve(n); for (int i = 1; i <= n; i++) out.push(Convert<T>::from(v[i])); return out; } }; template <class T> Value Value::from(const T &v);

Converts a C++ value to Lua.

Parameters

  • v (const atl::Value &)

Returns Value

atl_value handle() const;

The raw handle.

Returns atl_value

bool isNil() const;

True for nil.

Returns bool

explicit operator bool() const;

True for anything but nil.

Type type() const;

The Lua type.

Returns Type

bool is(Type t) const;

True when the type is t.

Parameters

  • t (Type)

Returns bool

bool truthy() const;

Lua truthiness: false only for nil and false.

Returns bool

double toNumber() const;

The value as a number (0 if it isn't one).

Returns double

String toString() const;

Lua's tostring() of the value.

Returns String

template <class T> T as() const;

Converts the value to T.

Returns sm::ClientScriptableObject

Value operator[](StringView field) const;

t[field].

Parameters

  • field (StringView)

Returns Value

Value operator[](int index) const;

t[index]. Lua indices start at 1.

Parameters

  • index (int)

Returns Value

Value get(const Value &key) const;

t[key] with any key.

Parameters

  • key (const Value &)

Returns Value

template <class T> T get(StringView field) const;

t[field] converted to T.

Parameters

  • field (StringView)

Returns sm::ClientScriptableObject

double number(StringView field) const;

t[field] as a number, without creating a handle.

Parameters

  • field (StringView)

Returns double

template <class T> Value &set(StringView field, const T &v);

t[field] = v. Returns *this for chaining.

Parameters

  • field (StringView)
  • v (const atl::Value &)

Returns Value &

template <class T> Value &set(int index, const T &v);

t[index] = v. Returns *this for chaining.

Parameters

  • index (int)
  • v (const sm::Vec3 &)

Returns Value &

int length() const;

The length operator #t.

Returns int

template <class T> Value &append(const T &v);

Appends v at t[#t + 1].

Parameters

  • v (const atl::StringView &)

Returns Value &

class PairRange pairs() const;

Iterates the table's key/value pairs: for (auto &[k, v] : t.pairs()).

Returns class PairRange

template <class R = Value, class... A> R call(const A &...args) const;

Calls the value as a function and returns its first result as R. A Lua error traps the mod.

Parameters

  • args (const sm::Uuid &)
  • args (const atl::Value &)
  • args (const sm::World &)

Returns sm::ClientScriptableObject

template <class R = Value, class... A> R method(StringView name, const A &...args) const;

Calls value:name(args...) and returns its first result as R. A Lua error traps the mod.

Parameters

  • name (StringView)

Returns atl::Optional<sm::Vec3>

template <class... A> Optional<Vector<Value>> tryCall(const A &...args) const;

Calls the value as a function and returns every result, or none if it raised an error, so the mod can handle the failure itself.

Parameters

  • args (const atl::Value &)

Returns Optional<Vector<Value>>

template <class... A> Vector<Value> methodAll(StringView name, const A &...args) const;

Calls value:name(args...) and returns every result.

Parameters

  • name (StringView)
  • args (const atl::StringView &)

Returns Vector<Value>

template <class... A> Vector<Value> callAll(const A &...args) const;

Calls the value as a function and returns every result.

Parameters

  • args (const atl::Vector<sm::Body> &)
  • args (const sm::Vec3 &)
  • args (const int &)

Returns Vector<Value>

One key/value pair produced by pairs().

struct Pair
Value key, value;

Iterator over a table with Lua's next(); used by Value::pairs().

class PairIterator
PairIterator(const Value *t, Value key);

Parameters

  • t (const Value *)
  • key (Value)
bool operator!=(const PairIterator &) const;

Returns bool

void operator++();
const Pair &operator*() const;

Returns const Pair &

The range returned by Value::pairs().

class PairRange
explicit PairRange(const Value *t);

Parameters

  • t (const Value *)
PairIterator begin() const;

Returns PairIterator

PairIterator end() const;

Returns PairIterator

Converts to Lua nil; pass atl::nil as an argument.

struct Nil

Base class for game object wrappers (Shape, Body, Player, ...). Holds the Lua object and calls its methods.

class Object
Object() = defaul;

A null object.

explicit Object(Value v);

Wraps a Lua object.

Parameters

  • v (Value)
const Value &value() const;

The wrapped Lua value.

Returns const Value &

explicit operator bool() const;

True when the wrapper holds an object (it may still have been destroyed).

bool exists() const;

True when the object still exists in the game (sm.exists).

Returns bool

Lua: sm.exists

template <class R = void, class... A> R call(StringView name, const A &...args) const;

Calls the Lua method name on the object.

Parameters

  • name (StringView)

Returns atl::Optional<sm::Vec3>

Protected: available to subclasses.

Value v_;

Protected: available to subclasses.