From b33c5bf86f2bce23f2b8dd569a509ab4b9f0fd51 Mon Sep 17 00:00:00 2001 From: Anton Shkinder Date: Mon, 11 Feb 2019 11:46:15 +0300 Subject: [PATCH] Fixed thread_safe_index::remove and thread_safe_index::modify --- .../include/sidechain/thread_safe_index.hpp | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/libraries/sidechain/include/sidechain/thread_safe_index.hpp b/libraries/sidechain/include/sidechain/thread_safe_index.hpp index 4e45ebe1..1742695b 100644 --- a/libraries/sidechain/include/sidechain/thread_safe_index.hpp +++ b/libraries/sidechain/include/sidechain/thread_safe_index.hpp @@ -6,7 +6,8 @@ namespace sidechain { template -class thread_safe_index { +class thread_safe_index +{ public: @@ -18,17 +19,23 @@ public: } template - void modify( const Key _key, const std::function& func ) { + bool modify( const Key _key, const std::function& func ) { std::lock_guard locker( lock ); - const auto& obj = *find_iterator( _key ); - data.modify( data.iterator_to( obj ), [&func]( typename T1::value_type& obj ) { func( obj ); } ); + const auto option = find_iterator( _key ); + if( !option ) + return false; + data.modify( data.iterator_to( **option ), [&func]( typename T1::value_type& obj ) { func( obj ); } ); + return true; } template - void remove( const Key _key ) { + bool remove( const Key _key ) { std::lock_guard locker( lock ); - const auto& obj = *find_iterator( _key ); - data.erase( data.iterator_to( obj ) ); + const auto option = find_iterator( _key ); + if( !option ) + return false; + data.erase( data.iterator_to( **option ) ); + return true; } size_t size() { @@ -58,13 +65,14 @@ public: private: template - typename T1::template index::type::iterator find_iterator( const Key _key ) { + fc::optional::type::iterator> find_iterator( const Key _key ) { auto& index = data.template get(); auto it = index.find( _key ); + if( it == index.end() ) + return fc::optional::type::iterator>(); return it; } - std::recursive_mutex lock; T1 data;