osrm-backend/include/util/lua_util.hpp

46 lines
1.4 KiB
C++
Raw Normal View History

2015-01-27 11:44:46 -05:00
#ifndef LUA_UTIL_HPP
#define LUA_UTIL_HPP
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <boost/filesystem/convenience.hpp>
#include <luabind/luabind.hpp>
#include <iostream>
#include <string>
2016-01-05 10:51:13 -05:00
namespace osrm
{
namespace util
{
2015-01-27 11:44:46 -05:00
template <typename T> void LUA_print(T output) { std::cout << "[LUA] " << output << std::endl; }
// Check if the lua function <name> is defined
inline bool lua_function_exists(lua_State *lua_state, const char *name)
{
luabind::object globals_table = luabind::globals(lua_state);
luabind::object lua_function = globals_table[name];
return lua_function && (luabind::type(lua_function) == LUA_TFUNCTION);
}
// Add the folder contain the script to the lua load path, so script can easily require() other lua
// scripts inside that folder, or subfolders.
// See http://lua-users.org/wiki/PackagePath for details on the package.path syntax.
inline void luaAddScriptFolderToLoadPath(lua_State *lua_state, const char *file_name)
{
2015-05-15 09:30:41 -04:00
boost::filesystem::path profile_path = boost::filesystem::canonical(file_name);
2015-01-27 11:44:46 -05:00
std::string folder = profile_path.parent_path().string();
// TODO: This code is most probably not Windows safe since it uses UNIX'ish path delimiters
2015-05-15 09:30:41 -04:00
const std::string lua_code = "package.path = \"" + folder + "/?.lua;\" .. package.path";
2015-01-27 11:44:46 -05:00
luaL_dostring(lua_state, lua_code.c_str());
}
2016-01-05 10:51:13 -05:00
}
}
2015-01-27 11:44:46 -05:00
#endif // LUA_UTIL_HPP