2012-12-19 09:53:05 -05:00
|
|
|
/*
|
2013-10-14 07:42:28 -04:00
|
|
|
|
|
|
|
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
2012-12-19 09:53:05 -05:00
|
|
|
|
2012-12-28 16:05:28 -05:00
|
|
|
#ifndef LUAUTIL_H_
|
|
|
|
#define LUAUTIL_H_
|
|
|
|
|
2013-06-24 16:11:33 -04:00
|
|
|
extern "C" {
|
|
|
|
#include <lua.h>
|
|
|
|
#include <lauxlib.h>
|
|
|
|
#include <lualib.h>
|
|
|
|
}
|
|
|
|
|
2013-06-24 17:03:24 -04:00
|
|
|
#include <boost/filesystem/convenience.hpp>
|
2013-06-26 09:49:00 -04:00
|
|
|
#include <luabind/luabind.hpp>
|
2012-12-28 16:05:28 -05:00
|
|
|
#include <iostream>
|
2012-12-19 09:53:05 -05:00
|
|
|
#include <string>
|
|
|
|
|
2012-12-28 16:05:28 -05:00
|
|
|
template<typename T>
|
|
|
|
void LUA_print(T number) {
|
|
|
|
std::cout << "[LUA] " << number << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the lua function <name> is defined
|
2013-01-05 11:27:10 -05:00
|
|
|
inline bool lua_function_exists(lua_State* lua_state, const char* name) {
|
2012-12-28 16:05:28 -05:00
|
|
|
luabind::object g = luabind::globals(lua_state);
|
|
|
|
luabind::object func = g[name];
|
|
|
|
return func && (luabind::type(func) == LUA_TFUNCTION);
|
2012-12-19 09:53:05 -05:00
|
|
|
}
|
|
|
|
|
2012-12-28 16:05:28 -05:00
|
|
|
// 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.
|
2013-01-05 11:27:10 -05:00
|
|
|
inline void luaAddScriptFolderToLoadPath(lua_State* myLuaState, const char* fileName) {
|
2012-12-19 09:53:05 -05:00
|
|
|
const boost::filesystem::path profilePath( fileName );
|
2013-01-05 11:27:10 -05:00
|
|
|
std::string folder = profilePath.parent_path().string();
|
|
|
|
//TODO: This code is most probably not Windows safe since it uses UNIX'ish path delimiters
|
|
|
|
const std::string luaCode = "package.path = \"" + folder + "/?.lua;profiles/?.lua;\" .. package.path";
|
|
|
|
luaL_dostring( myLuaState, luaCode.c_str() );
|
2012-12-19 09:53:05 -05:00
|
|
|
}
|
|
|
|
|
2012-12-28 16:05:28 -05:00
|
|
|
#endif /* LUAUTIL_H_ */
|