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

View File

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

View File

@ -26,13 +26,15 @@ public:
: data_(init) {}
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 {
return sol::make_object(state, data_);
}
StoredType getData() { return data_; }
private:
StoredType data_;
};
@ -48,7 +50,7 @@ public:
}
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 {
@ -78,7 +80,7 @@ public:
: handle_(handle) {}
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 {
@ -86,6 +88,7 @@ public:
}
GCObjectHandle gcHandle() const override { return handle_; }
private:
GCObjectHandle handle_;
};
@ -185,4 +188,25 @@ StoredObject createStoredObject(GCObjectHandle 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

View File

@ -1,5 +1,7 @@
#pragma once
#include "utils.h"
#include "garbage-collector.h"
#include <sol.hpp>
@ -42,4 +44,8 @@ StoredObject createStoredObject(GCObjectHandle);
StoredObject createStoredObject(const sol::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

View File

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