2014-06-07 10:54:45 +08:00
|
|
|
#ifndef SOL_CONTAINER_HPP
|
|
|
|
#define SOL_CONTAINER_HPP
|
|
|
|
|
|
|
|
#include "userdata.hpp"
|
|
|
|
|
|
|
|
namespace sol {
|
|
|
|
|
2014-06-08 04:33:39 +08:00
|
|
|
template <typename Tc, typename = void>
|
2014-06-07 10:54:45 +08:00
|
|
|
struct container {
|
|
|
|
typedef typename std::conditional<std::is_lvalue_reference<Tc>::value, Tc, Decay<Tc>>::type T;
|
2014-06-08 04:33:39 +08:00
|
|
|
typedef Unqualified<decltype(*(std::declval<T>().begin()))> value_type;
|
2014-06-07 10:54:45 +08:00
|
|
|
T cont;
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
container (Args&&... args) : cont(std::forward<Args>(args)...){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T& () const {
|
|
|
|
return cont;
|
|
|
|
}
|
|
|
|
|
2014-06-08 04:33:39 +08:00
|
|
|
void set(std::ptrdiff_t i, const value_type& value) {
|
2014-06-07 10:54:45 +08:00
|
|
|
cont[ i ] = value;
|
|
|
|
}
|
|
|
|
|
2014-06-08 04:33:39 +08:00
|
|
|
value_type& get(std::ptrdiff_t i) {
|
2014-06-07 10:54:45 +08:00
|
|
|
return cont[ i ];
|
|
|
|
}
|
|
|
|
|
2014-06-08 04:33:39 +08:00
|
|
|
std::size_t size() const {
|
2014-06-07 10:54:45 +08:00
|
|
|
return cont.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-06-08 04:33:39 +08:00
|
|
|
template <typename Tc>
|
|
|
|
struct container<Tc, typename std::enable_if<has_key_value_pair<Unqualified<Tc>>::value>::type> {
|
|
|
|
typedef typename std::conditional<std::is_lvalue_reference<Tc>::value, Tc, Decay<Tc>>::type T;
|
|
|
|
typedef Unqualified<decltype((*(std::declval<T>().begin())).first)> key_type;
|
|
|
|
typedef Unqualified<decltype((*(std::declval<T>().begin())).second)> value_type;
|
|
|
|
T cont;
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
container (Args&&... args) : cont(std::forward<Args>(args)...){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T& () const {
|
|
|
|
return cont;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(key_type i, const value_type& value) {
|
|
|
|
cont[ i ] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_type& get(key_type i) {
|
|
|
|
return cont.at(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t size() const {
|
|
|
|
return cont.size();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-07 10:54:45 +08:00
|
|
|
namespace stack {
|
|
|
|
template<typename T>
|
|
|
|
struct pusher<T, typename std::enable_if<has_begin_end<T>::value>::type> {
|
2014-06-07 23:00:27 +08:00
|
|
|
static void push(lua_State* L, const T& cont) {
|
2014-06-08 04:33:39 +08:00
|
|
|
typedef container<T> container_t;
|
2014-06-07 10:54:45 +08:00
|
|
|
// todo: NEED to find a different way of handling this...
|
|
|
|
static std::vector<std::shared_ptr<void>> classes{};
|
|
|
|
userdata<container_t> classdata(default_constructor,
|
|
|
|
"__index", &container_t::get,
|
|
|
|
"__newindex", &container_t::set,
|
|
|
|
"__len", &container_t::size);
|
|
|
|
classes.emplace_back(std::make_shared<userdata<container_t>>(std::move(classdata)));
|
|
|
|
auto&& ptr = classes.back();
|
|
|
|
auto udata = std::static_pointer_cast<userdata<container_t>>(ptr);
|
|
|
|
stack::push(L, *udata);
|
|
|
|
|
|
|
|
container_t* c = static_cast<container_t*>(lua_newuserdata(L, sizeof(container_t)));
|
|
|
|
std::allocator<container_t> alloc{};
|
2014-06-07 23:00:27 +08:00
|
|
|
alloc.construct(c, cont);
|
2014-06-07 10:54:45 +08:00
|
|
|
|
2014-06-08 04:33:39 +08:00
|
|
|
auto&& meta = userdata_traits<container_t>::metatable;
|
|
|
|
if (luaL_newmetatable(L, std::addressof(meta[0])) == 1) {
|
|
|
|
std::string err("metatable not defined for ");
|
|
|
|
err += meta;
|
|
|
|
throw error(err);
|
|
|
|
}
|
2014-06-07 10:54:45 +08:00
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
}
|
2014-06-08 04:33:39 +08:00
|
|
|
};
|
2014-06-07 10:54:45 +08:00
|
|
|
|
2014-06-08 04:33:39 +08:00
|
|
|
template<typename T>
|
|
|
|
struct getter<T, typename std::enable_if<has_begin_end<T>::value>::type> {
|
|
|
|
static Unqualified<T>& get(lua_State* L, int index = -1) {
|
|
|
|
typedef container<Unqualified<T>> container_t;
|
|
|
|
container_t& data = stack::get<container_t>(L, index);
|
|
|
|
return data.cont;
|
2014-06-07 10:54:45 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // stack
|
|
|
|
} // sol
|
|
|
|
#endif // SOL_CONTAINER_HPP
|