review comments impl

This commit is contained in:
mihacooper 2017-02-14 22:55:34 +03:00
parent 8bb6f16db1
commit ac77c4444e
5 changed files with 54 additions and 23 deletions

View File

@ -4,6 +4,7 @@
#include <mutex> #include <mutex>
#include <cassert> #include <cassert>
#include <limits.h>
namespace effil { namespace effil {
@ -82,29 +83,31 @@ size_t SharedTable::size() const {
size_t SharedTable::length() const { size_t SharedTable::length() const {
std::lock_guard<SpinMutex> g(data_->lock); std::lock_guard<SpinMutex> g(data_->lock);
size_t len = 0u;
DataEntries::const_iterator iter; sol::optional<double> value;
size_t l = 0u; auto iter = data_->entries.find(createStoredObject(static_cast<double>(1)));
while((iter = data_->entries.find(createStoredObject(static_cast<double>(l + 1)))) != data_->entries.end()) { if (iter != data_->entries.end())
l++; {
}; do
return l; {
++len;
++iter;
}
while ((iter != data_->entries.end()) && (value = storedObjectToDouble(iter->first)) && (static_cast<size_t>(value.value()) == len + 1));
}
return len;
} }
SharedTable::PairsIterator SharedTable::getNext(const sol::object& key, sol::this_state lua) SharedTable::PairsIterator SharedTable::getNext(const sol::object& key, sol::this_state lua) {
{
std::lock_guard<SpinMutex> g(data_->lock); std::lock_guard<SpinMutex> g(data_->lock);
if (key) if (key) {
{
auto obj = createStoredObject(key); auto obj = createStoredObject(key);
auto upper = data_->entries.upper_bound(obj); auto upper = data_->entries.upper_bound(obj);
if (upper != data_->entries.end()) if (upper != data_->entries.end())
return std::tuple<sol::object, sol::object>(upper->first->unpack(lua), upper->second->unpack(lua)); return std::tuple<sol::object, sol::object>(upper->first->unpack(lua), upper->second->unpack(lua));
} }
else else {
{ if (!data_->entries.empty()) {
if (!data_->entries.empty())
{
const auto& begin = data_->entries.begin(); const auto& begin = data_->entries.begin();
return std::tuple<sol::object, sol::object>(begin->first->unpack(lua), begin->second->unpack(lua)); return std::tuple<sol::object, sol::object>(begin->first->unpack(lua), begin->second->unpack(lua));
} }
@ -120,9 +123,8 @@ SharedTable::PairsIterator SharedTable::pairs(sol::this_state lua) const {
); );
} }
std::tuple<sol::object, sol::object> ipairsNext(sol::this_state lua, SharedTable table, sol::optional<unsigned long> key) std::tuple<sol::object, sol::object> ipairsNext(sol::this_state lua, SharedTable table, sol::optional<unsigned long> key) {
{ size_t index = key ? key.value() + 1 : 1;
unsigned long index = key ? key.value() + 1 : 1ul;
auto objKey = createStoredObject(static_cast<double>(index)); auto objKey = createStoredObject(static_cast<double>(index));
sol::object value = table.get(objKey, lua); sol::object value = table.get(objKey, lua);
if (!value.valid()) if (!value.valid())

View File

@ -2,7 +2,6 @@
#include <atomic> #include <atomic>
#include <thread> #include <thread>
#include <mutex>
namespace effil { namespace effil {
@ -16,7 +15,6 @@ public:
void unlock() noexcept { void unlock() noexcept {
lock_.clear(std::memory_order_release); lock_.clear(std::memory_order_release);
} }
private: private:

View File

@ -26,13 +26,15 @@ public:
: data_(init) {} : data_(init) {}
bool rawCompare(const BaseHolder* other) const noexcept final { bool rawCompare(const BaseHolder* other) const noexcept final {
return static_cast<const PrimitiveHolder<StoredType>*>(other)->data_ < data_; return data_ < static_cast<const PrimitiveHolder<StoredType>*>(other)->data_;
} }
sol::object unpack(sol::this_state state) const final { sol::object unpack(sol::this_state state) const final {
return sol::make_object(state, data_); return sol::make_object(state, data_);
} }
StoredType getData() { return data_; }
private: private:
StoredType data_; StoredType data_;
}; };
@ -48,7 +50,7 @@ public:
} }
bool rawCompare(const BaseHolder* other) const noexcept final { bool rawCompare(const BaseHolder* other) const noexcept final {
return static_cast<const FunctionHolder*>(other)->function_ < function_; return function_ < static_cast<const FunctionHolder*>(other)->function_;
} }
sol::object unpack(sol::this_state state) const final { sol::object unpack(sol::this_state state) const final {
@ -78,7 +80,7 @@ public:
: handle_(handle) {} : handle_(handle) {}
bool rawCompare(const BaseHolder *other) const final { bool rawCompare(const BaseHolder *other) const final {
return static_cast<const TableHolder*>(other)->handle_ < handle_; return handle_ < static_cast<const TableHolder*>(other)->handle_;
} }
sol::object unpack(sol::this_state state) const final { sol::object unpack(sol::this_state state) const final {
@ -86,6 +88,7 @@ public:
} }
GCObjectHandle gcHandle() const override { return handle_; } GCObjectHandle gcHandle() const override { return handle_; }
private: private:
GCObjectHandle handle_; GCObjectHandle handle_;
}; };
@ -185,4 +188,25 @@ StoredObject createStoredObject(GCObjectHandle handle) {
return std::make_unique<TableHolder>(handle); return std::make_unique<TableHolder>(handle);
} }
sol::optional<bool> storedObjectToBool(const StoredObject& sobj) {
auto ptr = dynamic_cast<PrimitiveHolder<bool>*>(sobj.get());
if (ptr)
return ptr->getData();
return sol::optional<bool>();
}
sol::optional<double> storedObjectToDouble(const StoredObject& sobj) {
auto ptr = dynamic_cast<PrimitiveHolder<double>*>(sobj.get());
if (ptr)
return ptr->getData();
return sol::optional<double>();
}
sol::optional<std::string> storedObjectToString(const StoredObject& sobj) {
auto ptr = dynamic_cast<PrimitiveHolder<std::string>*>(sobj.get());
if (ptr)
return ptr->getData();
return sol::optional<std::string>();
}
} // effil } // effil

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "utils.h"
#include "garbage-collector.h" #include "garbage-collector.h"
#include <sol.hpp> #include <sol.hpp>
@ -42,4 +44,8 @@ StoredObject createStoredObject(GCObjectHandle);
StoredObject createStoredObject(const sol::object &); StoredObject createStoredObject(const sol::object &);
StoredObject createStoredObject(const sol::stack_object &); StoredObject createStoredObject(const sol::stack_object &);
sol::optional<bool> storedObjectToBool(const StoredObject&);
sol::optional<double> storedObjectToDouble(const StoredObject&);
sol::optional<std::string> storedObjectToString(const StoredObject&);
} // effil } // effil

View File

@ -257,6 +257,7 @@ function TestSmoke:testCheckLengthOperator()
share[2] = 20 share[2] = 20
share[3] = 30 share[3] = 30
share[4] = 40 share[4] = 40
share["str"] = 50
log "Check values" log "Check values"
test.assertEquals(#share, 4) test.assertEquals(#share, 4)
share[3] = nil share[3] = nil