CorsixTH engine (the C++ part)
Open source implementation of Theme Hospital
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
load_multi_buffer Class Reference

Public Member Functions

void insert (std::string_view piece, size_t index)
 Insert the given chunk at the given index in the buffer.
 

Static Public Member Functions

static const charload_fn (lua_State *L, void *ud, size_t *size)
 lua_Reader callback function for lua_load or luaT_load
 

Public Attributes

const charpiece [load_multi_buffer_capacity] {nullptr}
 Pieces of the lua code chunk to be loaded.
 
size_t piece_size [load_multi_buffer_capacity] {}
 Lengths of the pieces.
 

Detailed Description

Structure for loading multiple strings as a Lua chunk, avoiding concatenation

luaL_loadbuffer() is a good way to load a string as a Lua chunk. If there are several strings which need to be concatenated before being loaded, then it can be more efficient to call lua_load with a callback function for loading the strings one at a time than to concatenate the strings into a single buffer.

This class provides the data structure and callback function to do this.

ls.piece[0] = lua_tolstring(L, -2, &ls.piece_size[0]);
ls.piece[1] = lua_tolstring(L, -1, &ls.piece_size[1]);
luaT_load(L, load_multi_buffer::load_fn, &ls, "chunk name", "bt");
Definition persist_lua.cpp:102
static const char * load_fn(lua_State *L, void *ud, size_t *size)
lua_Reader callback function for lua_load or luaT_load
Definition persist_lua.cpp:115
const char * piece[load_multi_buffer_capacity]
Pieces of the lua code chunk to be loaded.
Definition persist_lua.cpp:139
int luaT_load(lua_State *L, lua_Reader r, void *d, const char *s, const char *m)
Definition th_lua.h:155

Because of the api of lua_tolstring the API of this class allows direct and independent assignment of the chunk pieces and their sizes.

It is up to the caller to keep track of how many pieces are used and to ensure that no more than load_multi_buffer_capacity pieces are used.

Member Function Documentation

◆ insert()

void load_multi_buffer::insert ( std::string_view  piece,
size_t  index 
)
inline

Insert the given chunk at the given index in the buffer.

◆ load_fn()

static const char * load_multi_buffer::load_fn ( lua_State L,
void ud,
size_t *  size 
)
inlinestatic

lua_Reader callback function for lua_load or luaT_load

Called repeatedly by lua to get the next piece of data to load until a null pointer is returned.

Parameters
LThe Lua state
udPointer to the load_multi_buffer instance
sizePointer to size_t to receive the size of the returned string
See also
https://www.lua.org/manual/5.4/manual.html#lua_Reader

Member Data Documentation

◆ piece

const char* load_multi_buffer::piece[load_multi_buffer_capacity] {nullptr}

Pieces of the lua code chunk to be loaded.

◆ piece_size

size_t load_multi_buffer::piece_size[load_multi_buffer_capacity] {}

Lengths of the pieces.


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