kicad/common/observable.cpp

268 lines
5.8 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2016 Kicad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "observable.h"
#include <algorithm>
namespace UTIL {
namespace DETAIL {
template<typename T>
struct equals {
equals( const T& val ) : val_( val ) {}
2020-11-18 19:50:36 +00:00
bool operator()( const T& val ) const
{
return val == val_;
}
private:
const T& val_;
};
OBSERVABLE_BASE::IMPL::IMPL( OBSERVABLE_BASE* owned_by )
: iteration_count_( 0 ), owned_by_( owned_by )
{}
bool OBSERVABLE_BASE::IMPL::is_shared() const
{
return owned_by_ == nullptr;
}
void OBSERVABLE_BASE::IMPL::set_shared()
{
owned_by_ = nullptr;
}
OBSERVABLE_BASE::IMPL::~IMPL()
{
}
2017-01-02 07:17:10 +00:00
void OBSERVABLE_BASE::IMPL::enter_iteration()
{
++iteration_count_;
}
2017-01-02 07:17:10 +00:00
void OBSERVABLE_BASE::IMPL::leave_iteration()
{
--iteration_count_;
if( iteration_count_ == 0 )
2017-01-02 07:17:10 +00:00
collect();
}
2017-01-02 07:17:10 +00:00
bool OBSERVABLE_BASE::IMPL::is_iterating() const
{
return iteration_count_ != 0;
}
2017-01-02 07:17:10 +00:00
void OBSERVABLE_BASE::IMPL::add_observer( void* observer )
{
assert( !is_iterating() );
observers_.push_back( observer );
}
void OBSERVABLE_BASE::IMPL::remove_observer( void* observer )
{
2017-01-02 07:17:10 +00:00
auto it = std::find( observers_.begin(), observers_.end(), observer );
if( it == observers_.end() )
{
assert( false );
return;
}
if( is_iterating() )
2017-01-02 07:17:10 +00:00
*it = nullptr;
else
2017-01-02 07:17:10 +00:00
observers_.erase( it );
}
void OBSERVABLE_BASE::IMPL::collect()
{
auto it = std::remove_if( observers_.begin(), observers_.end(), DETAIL::equals<void*>( nullptr ) );
observers_.erase( it, observers_.end() );
}
}
LINK::LINK()
: observer_( nullptr )
{
}
LINK::LINK( std::shared_ptr<DETAIL::OBSERVABLE_BASE::IMPL> token, void* observer )
: token_( std::move( token ) ), observer_( observer )
{
}
LINK::LINK( LINK&& other )
: token_( std::move( other.token_ ) ), observer_( other.observer_ )
{
other.token_.reset();
}
LINK& LINK::operator=( LINK&& other )
{
token_ = std::move( other.token_ );
other.token_.reset();
observer_ = other.observer_;
return *this;
}
LINK::operator bool() const
{
return token_ ? true : false;
}
LINK::~LINK()
{
reset();
}
void LINK::reset()
{
if(token_)
{
token_->remove_observer( observer_ );
2017-01-02 07:17:10 +00:00
token_.reset();
}
}
namespace DETAIL {
OBSERVABLE_BASE::OBSERVABLE_BASE()
{
}
OBSERVABLE_BASE::OBSERVABLE_BASE( OBSERVABLE_BASE& other )
: impl_( other.get_shared_impl() )
{
}
OBSERVABLE_BASE::~OBSERVABLE_BASE()
{
}
void OBSERVABLE_BASE::allocate_impl()
{
if(!impl_)
impl_ = std::make_shared<IMPL>( this );
}
void OBSERVABLE_BASE::allocate_shared_impl()
{
if(!impl_)
impl_ = std::make_shared<IMPL>();
else
impl_->set_shared();
}
void OBSERVABLE_BASE::deallocate_impl() {
impl_.reset();
}
2017-01-02 07:17:10 +00:00
std::shared_ptr<OBSERVABLE_BASE::IMPL> OBSERVABLE_BASE::get_shared_impl()
{
allocate_shared_impl();
return impl_;
}
void OBSERVABLE_BASE::add_observer( void* observer )
{
2017-01-02 07:17:10 +00:00
allocate_impl();
impl_->add_observer( observer );
}
void OBSERVABLE_BASE::remove_observer( void* observer )
{
2017-01-02 07:17:10 +00:00
assert( impl_ );
impl_->remove_observer( observer );
}
void OBSERVABLE_BASE::enter_iteration()
{
if( impl_ )
2017-01-02 07:17:10 +00:00
impl_->enter_iteration();
}
void OBSERVABLE_BASE::leave_iteration()
{
if( impl_)
{
2017-01-02 07:17:10 +00:00
impl_->leave_iteration();
if( !impl_->is_iterating() && !impl_->is_shared() && impl_.use_count() == 1 )
2017-01-02 07:17:10 +00:00
impl_.reset();
}
}
size_t OBSERVABLE_BASE::size() const
{
if( impl_ )
2017-01-02 07:17:10 +00:00
return impl_->observers_.size();
else
2017-01-02 07:17:10 +00:00
return 0;
}
void OBSERVABLE_BASE::on_observers_empty()
{
// called by an impl that is owned by this, ie. it is a non-shared impl
// also it is not iterating
deallocate_impl();
}
}
}