Created
November 27, 2012 13:27
-
-
Save vrld/4154228 to your computer and use it in GitHub Desktop.
Less annoying luaL_Buffer handling in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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