Skip to content

Instantly share code, notes, and snippets.

@vrld
Created November 27, 2012 13:27
Show Gist options
  • Save vrld/4154228 to your computer and use it in GitHub Desktop.
Save vrld/4154228 to your computer and use it in GitHub Desktop.
Less annoying luaL_Buffer handling in C++
int l_imwrite(lua_State *L)
{
// see https://gist.github.com/4154182
cv::Mat *img = LuaProxy<cv::Mat>::get(L, 1);
const char *path = luaL_checkstring(L, 2);
if (cv::imwrite(path, *img))
{
lua_pushboolean(L, true);
return 1;
}
// else
lua_pushnil(L);
LuaBuffer(L).add("Cannot write image to `").add(path).add("'.").finish();
return 2;
}
#pragma once
#include <lua.hpp>
// Note: This is incomplete, as it misses luaL_addchar, luaL_addlstring, etc.
// but still covers 100% of my use cases ;)
struct LuaBuffer
{
luaL_Buffer b;
LuaBuffer(lua_State *L)
{
luaL_buffinit(L, &b);
}
inline LuaBuffer &add(const char *s)
{
luaL_addstring(&b, s);
return *this;
}
inline LuaBuffer &addvalue()
{
luaL_addvalue(&b);
return *this;
}
inline void finish()
{
luaL_pushresult(&b);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment