From ac77c4444eaeef84809ae95b71721cb39d0f1a6d Mon Sep 17 00:00:00 2001 From: mihacooper Date: Tue, 14 Feb 2017 22:55:34 +0300 Subject: [PATCH] review comments impl --- src/cpp/shared-table.cpp | 38 ++++++++++++++++++++------------------ src/cpp/spin-mutex.h | 2 -- src/cpp/stored-object.cpp | 30 +++++++++++++++++++++++++++--- src/cpp/stored-object.h | 6 ++++++ tests/lua/smoke_test.lua | 1 + 5 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/cpp/shared-table.cpp b/src/cpp/shared-table.cpp index 914ca8b..1baf7dd 100644 --- a/src/cpp/shared-table.cpp +++ b/src/cpp/shared-table.cpp @@ -4,6 +4,7 @@ #include #include +#include namespace effil { @@ -82,29 +83,31 @@ size_t SharedTable::size() const { size_t SharedTable::length() const { std::lock_guard g(data_->lock); - - DataEntries::const_iterator iter; - size_t l = 0u; - while((iter = data_->entries.find(createStoredObject(static_cast(l + 1)))) != data_->entries.end()) { - l++; - }; - return l; + size_t len = 0u; + sol::optional value; + auto iter = data_->entries.find(createStoredObject(static_cast(1))); + if (iter != data_->entries.end()) + { + do + { + ++len; + ++iter; + } + while ((iter != data_->entries.end()) && (value = storedObjectToDouble(iter->first)) && (static_cast(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 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(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(begin->first->unpack(lua), begin->second->unpack(lua)); } @@ -120,9 +123,8 @@ SharedTable::PairsIterator SharedTable::pairs(sol::this_state lua) const { ); } -std::tuple ipairsNext(sol::this_state lua, SharedTable table, sol::optional key) -{ - unsigned long index = key ? key.value() + 1 : 1ul; +std::tuple ipairsNext(sol::this_state lua, SharedTable table, sol::optional key) { + size_t index = key ? key.value() + 1 : 1; auto objKey = createStoredObject(static_cast(index)); sol::object value = table.get(objKey, lua); if (!value.valid()) diff --git a/src/cpp/spin-mutex.h b/src/cpp/spin-mutex.h index 265707a..0f5d2a4 100644 --- a/src/cpp/spin-mutex.h +++ b/src/cpp/spin-mutex.h @@ -2,7 +2,6 @@ #include #include -#include namespace effil { @@ -16,7 +15,6 @@ public: void unlock() noexcept { lock_.clear(std::memory_order_release); - } private: diff --git a/src/cpp/stored-object.cpp b/src/cpp/stored-object.cpp index ffbb014..8d016e3 100644 --- a/src/cpp/stored-object.cpp +++ b/src/cpp/stored-object.cpp @@ -26,13 +26,15 @@ public: : data_(init) {} bool rawCompare(const BaseHolder* other) const noexcept final { - return static_cast*>(other)->data_ < data_; + return data_ < static_cast*>(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(other)->function_ < function_; + return function_ < static_cast(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(other)->handle_ < handle_; + return handle_ < static_cast(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(handle); } +sol::optional storedObjectToBool(const StoredObject& sobj) { + auto ptr = dynamic_cast*>(sobj.get()); + if (ptr) + return ptr->getData(); + return sol::optional(); +} + +sol::optional storedObjectToDouble(const StoredObject& sobj) { + auto ptr = dynamic_cast*>(sobj.get()); + if (ptr) + return ptr->getData(); + return sol::optional(); +} + +sol::optional storedObjectToString(const StoredObject& sobj) { + auto ptr = dynamic_cast*>(sobj.get()); + if (ptr) + return ptr->getData(); + return sol::optional(); +} + } // effil diff --git a/src/cpp/stored-object.h b/src/cpp/stored-object.h index ae7e4b7..b6755be 100644 --- a/src/cpp/stored-object.h +++ b/src/cpp/stored-object.h @@ -1,5 +1,7 @@ #pragma once +#include "utils.h" + #include "garbage-collector.h" #include @@ -42,4 +44,8 @@ StoredObject createStoredObject(GCObjectHandle); StoredObject createStoredObject(const sol::object &); StoredObject createStoredObject(const sol::stack_object &); +sol::optional storedObjectToBool(const StoredObject&); +sol::optional storedObjectToDouble(const StoredObject&); +sol::optional storedObjectToString(const StoredObject&); + } // effil diff --git a/tests/lua/smoke_test.lua b/tests/lua/smoke_test.lua index 90e6926..c08c26d 100644 --- a/tests/lua/smoke_test.lua +++ b/tests/lua/smoke_test.lua @@ -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