The Battle for Wesnoth  1.19.20+dev
Public Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
lua_named_tuple_builder Class Reference

Efficiently creates new LUA "named tuples" with the specified field names. More...

#include <lua_common.hpp>

Public Member Functions

 lua_named_tuple_builder (const std::vector< std::string > &names)
 
void push (lua_State *L) const
 Push an empty "named tuple" onto the stack. More...
 

Static Private Member Functions

static int impl_get (lua_State *L)
 
static int impl_set (lua_State *L)
 
static int impl_dir (lua_State *L)
 
static int impl_tostring (lua_State *L)
 
static int impl_compare (lua_State *L)
 
static std::string build_key (const std::vector< std::string > &names)
 

Private Attributes

const std::vector< std::string > names_
 
const std::string key_
 

Static Private Attributes

static constexpr std::string_view base_name_ = "named tuple"
 

Detailed Description

Efficiently creates new LUA "named tuples" with the specified field names.

Use the push method to create a new empty tuple instance at the top of the stack.

All tuples have some fixed layout and the total set of layouts is small (10-ish). So, we simply store the meta table the LUA registry once and then keep reusing it. Saves tons of construction effort. For maximum reuse, you should create static instances of this class.

Note
It is perfectly fine to have multiple instances of this class produce the same named tuple structure; the registry mechanism will ensure that there is a single common metatable not all of them.

Definition at line 84 of file lua_common.hpp.

Constructor & Destructor Documentation

◆ lua_named_tuple_builder()

lua_named_tuple_builder::lua_named_tuple_builder ( const std::vector< std::string > &  names)

Definition at line 727 of file lua_common.cpp.

Member Function Documentation

◆ build_key()

std::string lua_named_tuple_builder::build_key ( const std::vector< std::string > &  names)
staticprivate

Definition at line 758 of file lua_common.cpp.

References base_name_, i, and names.

◆ impl_compare()

int lua_named_tuple_builder::impl_compare ( lua_State *  L)
staticprivate

Definition at line 696 of file lua_common.cpp.

References i.

Referenced by push().

◆ impl_dir()

int lua_named_tuple_builder::impl_dir ( lua_State *  L)
staticprivate

Definition at line 677 of file lua_common.cpp.

Referenced by push().

◆ impl_get()

int lua_named_tuple_builder::impl_get ( lua_State *  L)
staticprivate

Definition at line 641 of file lua_common.cpp.

References utils::find(), i, and names.

Referenced by push().

◆ impl_set()

int lua_named_tuple_builder::impl_set ( lua_State *  L)
staticprivate

Definition at line 657 of file lua_common.cpp.

References utils::find(), i, and names.

Referenced by push().

◆ impl_tostring()

int lua_named_tuple_builder::impl_tostring ( lua_State *  L)
staticprivate

Definition at line 683 of file lua_common.cpp.

References i, utils::join(), and lua_push().

Referenced by push().

◆ push()

void lua_named_tuple_builder::push ( lua_State *  L) const

Push an empty "named tuple" onto the stack.

A named tuple is an array where each index can also be accessed by name. Once it's pushed, you can set the elements, eg with lua_rawseti.

Definition at line 733 of file lua_common.cpp.

References impl_compare(), impl_dir(), impl_get(), impl_set(), impl_tostring(), key_, lua_push(), and names_.

Referenced by luaW_filltable(), luaW_pushcubeloc(), and luaW_pushlocation().

Member Data Documentation

◆ base_name_

constexpr std::string_view lua_named_tuple_builder::base_name_ = "named tuple"
staticconstexprprivate

Definition at line 107 of file lua_common.hpp.

Referenced by build_key().

◆ key_

const std::string lua_named_tuple_builder::key_
private

Definition at line 110 of file lua_common.hpp.

Referenced by push().

◆ names_

const std::vector<std::string> lua_named_tuple_builder::names_
private

Definition at line 109 of file lua_common.hpp.

Referenced by push().


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