mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
object is now more flexible while still supporting the same semantics
indentation an spacing fixes for everything
This commit is contained in:
parent
8e0cc99215
commit
93d532094e
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
#include "reference.hpp"
|
#include "reference.hpp"
|
||||||
#include "stack.hpp"
|
#include "stack.hpp"
|
||||||
|
#include "function.hpp"
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
class object : public reference {
|
class object : public reference {
|
||||||
|
@ -32,10 +33,13 @@ public:
|
||||||
object() = default;
|
object() = default;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
auto as() const -> decltype(stack::get<T>(state())) {
|
auto as() const -> decltype(stack::get<T>(state())) const {
|
||||||
push();
|
push();
|
||||||
type actual = stack::get<type>(state());
|
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());
|
return stack::pop<T>(state());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,25 +50,44 @@ public:
|
||||||
return (expected == actual) || (expected == type::poly);
|
return (expected == actual) || (expected == type::poly);
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit operator bool() const {
|
bool valid() const {
|
||||||
return !is<nil_t>();
|
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&) {
|
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) {
|
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&) {
|
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) {
|
inline bool operator!=(const nil_t&, const object& rhs) {
|
||||||
return !rhs.is<nil_t>();
|
return rhs.valid();
|
||||||
}
|
}
|
||||||
} // sol
|
} // sol
|
||||||
|
|
||||||
|
|
|
@ -31,18 +31,13 @@ template<typename Table, typename Key>
|
||||||
struct proxy {
|
struct proxy {
|
||||||
private:
|
private:
|
||||||
Table& tbl;
|
Table& tbl;
|
||||||
Key& key;
|
If<std::is_array<Unqualified<Key>>, Key&, Unqualified<Key>> key;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
proxy(Table& table, T&& key) : tbl(table), key(std::forward<T>(key)) {}
|
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>
|
template<typename T>
|
||||||
proxy& set(T&& item) {
|
proxy& set(T&& item) {
|
||||||
tbl.set(key, std::forward<T>(item));
|
tbl.set(key, std::forward<T>(item));
|
||||||
|
@ -56,13 +51,20 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename U, EnableIf<Function<Unqualified<U>>> = 0>
|
template<typename U, EnableIf<Function<Unqualified<U>>> = 0>
|
||||||
void operator=(U&& other) {
|
proxy& operator=(U&& other) {
|
||||||
tbl.set_function(key, std::forward<U>(other));
|
tbl.set_function(key, std::forward<U>(other));
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename U, DisableIf<Function<Unqualified<U>>> = 0>
|
template<typename U, DisableIf<Function<Unqualified<U>>> = 0>
|
||||||
void operator=(U&& other) {
|
proxy& operator=(U&& other) {
|
||||||
tbl.set(key, std::forward<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 {
|
operator const char* () const {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user