object is now more flexible while still supporting the same semantics

indentation an spacing fixes for everything
This commit is contained in:
ThePhD 2015-10-20 21:38:28 -04:00
parent 8e0cc99215
commit 93d532094e
13 changed files with 72 additions and 47 deletions

View File

@ -24,6 +24,7 @@
#include "reference.hpp"
#include "stack.hpp"
#include "function.hpp"
namespace sol {
class object : public reference {
@ -32,10 +33,13 @@ public:
object() = default;
template<typename T>
auto as() const -> decltype(stack::get<T>(state())) {
auto as() const -> decltype(stack::get<T>(state())) const {
push();
type actual = stack::get<type>(state());
type_assert(state(), -1, type_of<T>(), actual);
// This code is actually present
// in almost all of the type-getters,
// and it thus insanely redundant
// type_assert(state(), -1, type_of<T>(), actual);
return stack::pop<T>(state());
}
@ -46,25 +50,44 @@ public:
return (expected == actual) || (expected == type::poly);
}
explicit operator bool() const {
return !is<nil_t>();
bool valid() const {
return !this->is<nil_t>();
}
operator const char* () const {
return this->as<const char*>();
}
template<typename T, EnableIf<Not<std::is_same<Unqualified<T>, const char*>>, Not<std::is_same<Unqualified<T>, char>>, Not<std::is_same<Unqualified<T>, std::string>>, Not<std::is_same<Unqualified<T>, std::initializer_list<char>>>> = 0>
operator T () const {
return this->as<T>();
}
template<typename... Ret, typename... Args>
stack::get_return_or<function_result, Ret...> call( Args&&... args ) {
return this->as<function>()(types<Ret...>(), std::forward<Args>( args )...);
}
template<typename... Args>
function_result operator()( Args&&... args ) {
return this->as<function>()(std::forward<Args>( args )...);
}
};
inline bool operator==(const object& lhs, const nil_t&) {
return lhs.is<nil_t>();
return !lhs.valid();
}
inline bool operator==(const nil_t&, const object& rhs) {
return rhs.is<nil_t>();
return !rhs.valid();
}
inline bool operator!=(const object& lhs, const nil_t&) {
return !lhs.is<nil_t>();
return lhs.valid();
}
inline bool operator!=(const nil_t&, const object& rhs) {
return !rhs.is<nil_t>();
return rhs.valid();
}
} // sol

View File

@ -31,18 +31,13 @@ template<typename Table, typename Key>
struct proxy {
private:
Table& tbl;
Key& key;
If<std::is_array<Unqualified<Key>>, Key&, Unqualified<Key>> key;
public:
template<typename T>
proxy(Table& table, T&& key) : tbl(table), key(std::forward<T>(key)) {}
template<typename T>
T get() const {
return tbl.template get<T>(key);
}
template<typename T>
proxy& set(T&& item) {
tbl.set(key, std::forward<T>(item));
@ -56,13 +51,20 @@ public:
}
template<typename U, EnableIf<Function<Unqualified<U>>> = 0>
void operator=(U&& other) {
proxy& operator=(U&& other) {
tbl.set_function(key, std::forward<U>(other));
return *this;
}
template<typename U, DisableIf<Function<Unqualified<U>>> = 0>
void operator=(U&& other) {
proxy& operator=(U&& other) {
tbl.set(key, std::forward<U>(other));
return *this;
}
template<typename T>
T get() const {
return tbl.template get<T>( key );
}
operator const char* () const {