Skip to content

Yeah I did #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions luacpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@

// doxygen index
/**
@mainpage
@section hello_sec Hello, world!
@code
@mainpage
@section hello_sec Hello, world!
@code
#include <luacpp.hpp>

int main()
{
lua::state lua;
lua.openLibs();
lua::state lua;
lua.openLibs();

lua::function print = lua["print"];
print("Hello, world!");
lua::function print = lua["print"];
print("Hello, world!");
}
@endcode
@endcode

@section intro_sec Introduction
@section intro_sec Introduction

LuaC++ is a bridge between Lua and C++ which aims to remove the need to use the C API,
including the Lua stack. It currently supports Lua version 5.1.
LuaC++ is a bridge between Lua and C++ which aims to remove the need to use the C API,
including the Lua stack. It currently supports Lua version 5.1.

@section download_sec Download
Full source code can be found at http://github.com/JakobOvrum/LuaCpp
@section download_sec Download
Full source code can be found at http://github.com/JakobOvrum/LuaCpp
*/

//doxygen examples
/**
@example hello.cpp
@example config.cpp
@example hello.cpp
@example config.cpp
*/

#include "luacpp/error.hpp"
Expand Down
38 changes: 19 additions & 19 deletions luacpp/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@

namespace lua
{
///Lua error exception
/**
Thrown when an unhandled Lua error is raised.
Keep in mind that the actual C++ exception is only thrown if Lua
doesn't know what to do with one of its errors (Lua panic). This
means that all cases of lua::error being thrown (as referenced in documentation)
can, and will, be caught by protected Lua calls before being propagated as
this exception.
*/
class error : public std::runtime_error
{
public:
///Basic constructor
/**
@param what error message
\internal
*/
error(const char* what);
};
///Lua error exception
/**
Thrown when an unhandled Lua error is raised.
Keep in mind that the actual C++ exception is only thrown if Lua
doesn't know what to do with one of its errors (Lua panic). This
means that all cases of lua::error being thrown (as referenced in documentation)
can, and will, be caught by protected Lua calls before being propagated as
this exception.
*/
class error : public std::runtime_error
{
public:
///Basic constructor
/**
@param what error message
\internal
*/
error(const char* what);
};
}

#endif
117 changes: 60 additions & 57 deletions luacpp/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,74 @@

namespace lua
{
///Lua function
/**
Object representing a Lua function.
*/
class function : virtual public reference
{
public:
///Create empty reference to function
/** \internal */
function() : reference(){}
///Lua function
/**
Object representing a Lua function.
*/
class function : virtual public reference
{
public:
///Create empty reference to function
/** \internal */
function() : reference(){}

///Create reference to function on stack
/**
@param L stack containing function
@param index position on stack
\internal
*/
function(lua_State* L, int index = -1) : reference(L, index)
{
assertType(L, index, type::function);
}
///Create reference to function on stack
/**
@param L stack containing function
@param index position on stack
\internal
*/
function(lua_State* L, int index = -1) : reference(L, index)
{
assertType(L, index, type::function);
}

///Call this function
/**
@param args arguments to pass to function
*/
template<typename... Args>
void operator()(Args&&... args)
{
push();
pushArg(state(), args...);
lua_call(state(), sizeof...(args), 0);
}
///Call this function
/**
@param args arguments to pass to function
*/
template<typename... Args>
void operator()(Args&&... args)
{
push();
pushArg(state(), args...);
lua_call(state(), sizeof...(args), 0);
}

///Call this function and get return value(s)
/**
@tparam Rets types of expected return values, in order
///Call this function and get return value(s)
/**
@tparam Rets types of expected return values, in order

@param args arguments to pass to function
@return tuple of return values
*/
// with return values
template<typename... Rets, typename... Args>
std::tuple<Rets...> call(Args&&... args)
{
push();
pushArg(state(), args...);
lua_call(state(), sizeof...(args), sizeof...(Rets));
@param args arguments to pass to function
@return tuple of return values
*/
// with return values
template<typename... Rets, typename... Args>
std::tuple<Rets...> call(Args&&... args)
{
push();
pushArg(state(), args...);
lua_call(state(), sizeof...(args), sizeof...(Rets));

std::tuple<Rets...> ret;
luaToTuple<sizeof...(Rets), Rets...>::fill(state(), ret);
std::tuple<Rets...> ret;
luaToTuple<sizeof...(Rets), Rets...>::fill(state(), ret);

return ret;
}
return ret;
}

private:
template<typename T, typename... Args>
void pushArg(lua_State* L, T&& arg, Args&&... args)
{
pushValue(L, arg);
pushArg(L, args...);
}
private:
template<typename T, typename... Args>
void pushArg(lua_State* L, T&& arg, Args&&... args)
{
pushValue(L, arg);
pushArg(L, args...);
}

void pushArg(lua_State* L){}
};
void pushArg(lua_State* L)
{
(void)(L); // Unused.
}
};
}

#endif
26 changes: 13 additions & 13 deletions luacpp/nil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@

namespace lua
{
///Unique type representing Lua nil
/**
Use the constant lua::nil.
*/
struct nil_t
{
};
///Unique type representing Lua nil
/**
Use the constant lua::nil.
*/
class nil_t
{
};

///Object instance representing Lua nil
/**
Useful for clearing keys in tables.
@see table::index
*/
extern nil_t nil;
///Object instance representing Lua nil
/**
Useful for clearing keys in tables.
@see table::index
*/
extern nil_t nil;
}

#endif
38 changes: 19 additions & 19 deletions luacpp/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@

namespace lua
{
class object : virtual public reference, public table, public function
{
public:
object(lua_State* L, int index) : reference(L, index)
{
}
class object : virtual public reference, public table, public function
{
public:
object(lua_State* L, int index) : reference(L, index)
{
}

object(){}
object(){}

template<typename T>
operator T()
{
push();
assertType(state(), -1, typeOf<T>(state()));
template<typename T>
operator T()
{
push();
assertType(state(), -1, typeOf<T>(state()));

T t;
T t;

getValue(state(), -1, t);
getValue(state(), -1, t);

return t;
}
return t;
}

bool operator==(const reference& r);
bool operator==(const nil_t& t);
};
bool operator==(const reference& r);
bool operator==(const nil_t& t);
};
}

#endif
48 changes: 24 additions & 24 deletions luacpp/reference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,37 @@ struct lua_State;

namespace lua
{
class object;
class object;

class reference
{
public:
reference(lua_State* L, int index = -1);
reference(const reference& c);
virtual ~reference();
class reference
{
public:
reference(lua_State* L, int index = -1);
reference(const reference& c);
virtual ~reference();

type::luatype type() const;
const char* typeName() const;
type::luatype type() const;
const char* typeName() const;

protected:
reference();
const reference& operator=(const reference& c);
protected:
reference();
const reference& operator=(const reference& c);

void push() const;
inline lua_State* state() const
{
return L;
}
void push() const;
inline lua_State* state() const
{
return L;
}

private:
int copyRef() const;
private:
int copyRef() const;

lua_State* L;
int ref;
lua_State* L;
int ref;

friend void pushValue(lua_State* L, reference& ref);
friend class object;
};
friend void pushValue(lua_State* L, reference& ref);
friend class object;
};
}

#endif
Loading