2016-12-23 16:06:33 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2023-09-07 01:25:24 +00:00
|
|
|
#include <core/observable.h>
|
2016-12-23 16:06:33 +00:00
|
|
|
#include <algorithm>
|
2016-12-22 21:01:05 +00:00
|
|
|
|
|
|
|
namespace UTIL {
|
|
|
|
|
|
|
|
namespace DETAIL {
|
|
|
|
|
2017-01-31 20:35:52 +00:00
|
|
|
template<typename T>
|
2016-12-22 21:01:05 +00:00
|
|
|
struct equals {
|
|
|
|
equals( const T& val ) : val_( val ) {}
|
|
|
|
|
2020-11-18 19:50:36 +00:00
|
|
|
bool operator()( const T& val ) const
|
2017-01-11 15:19:58 +00:00
|
|
|
{
|
2016-12-22 21:01:05 +00:00
|
|
|
return val == val_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const T& val_;
|
|
|
|
};
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
OBSERVABLE_BASE::IMPL::IMPL( OBSERVABLE_BASE* owned_by )
|
2017-01-11 15:19:58 +00:00
|
|
|
: iteration_count_( 0 ), owned_by_( owned_by )
|
2016-12-22 21:01:05 +00:00
|
|
|
{}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
bool OBSERVABLE_BASE::IMPL::is_shared() const
|
|
|
|
{
|
|
|
|
return owned_by_ == nullptr;
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
void OBSERVABLE_BASE::IMPL::set_shared()
|
|
|
|
{
|
|
|
|
owned_by_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
OBSERVABLE_BASE::IMPL::~IMPL()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2017-01-02 07:17:10 +00:00
|
|
|
void OBSERVABLE_BASE::IMPL::enter_iteration()
|
|
|
|
{
|
|
|
|
++iteration_count_;
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2017-01-02 07:17:10 +00:00
|
|
|
void OBSERVABLE_BASE::IMPL::leave_iteration()
|
|
|
|
{
|
|
|
|
--iteration_count_;
|
2017-01-11 15:19:58 +00:00
|
|
|
|
|
|
|
if( iteration_count_ == 0 )
|
2017-01-02 07:17:10 +00:00
|
|
|
collect();
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2017-01-02 07:17:10 +00:00
|
|
|
bool OBSERVABLE_BASE::IMPL::is_iterating() const
|
|
|
|
{
|
|
|
|
return iteration_count_ != 0;
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2017-01-02 07:17:10 +00:00
|
|
|
void OBSERVABLE_BASE::IMPL::add_observer( void* observer )
|
|
|
|
{
|
|
|
|
assert( !is_iterating() );
|
|
|
|
observers_.push_back( observer );
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
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 );
|
|
|
|
|
2017-01-31 20:35:52 +00:00
|
|
|
if( it == observers_.end() )
|
|
|
|
{
|
|
|
|
assert( false );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
if( is_iterating() )
|
2017-01-02 07:17:10 +00:00
|
|
|
*it = nullptr;
|
2017-01-11 15:19:58 +00:00
|
|
|
else
|
2017-01-02 07:17:10 +00:00
|
|
|
observers_.erase( it );
|
2016-12-22 21:01:05 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
void OBSERVABLE_BASE::IMPL::collect()
|
|
|
|
{
|
|
|
|
auto it = std::remove_if( observers_.begin(), observers_.end(), DETAIL::equals<void*>( nullptr ) );
|
|
|
|
observers_.erase( it, observers_.end() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
LINK::LINK()
|
2017-01-31 20:35:52 +00:00
|
|
|
: observer_( nullptr )
|
2016-12-22 21:01:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
LINK::LINK( std::shared_ptr<DETAIL::OBSERVABLE_BASE::IMPL> token, void* observer )
|
|
|
|
: token_( std::move( token ) ), observer_( observer )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
LINK::LINK( LINK&& other )
|
|
|
|
: token_( std::move( other.token_ ) ), observer_( other.observer_ )
|
|
|
|
{
|
|
|
|
other.token_.reset();
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
LINK& LINK::operator=( LINK&& other )
|
|
|
|
{
|
|
|
|
token_ = std::move( other.token_ );
|
|
|
|
other.token_.reset();
|
|
|
|
observer_ = other.observer_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
|
|
|
LINK::operator bool() const
|
|
|
|
{
|
2016-12-22 21:01:05 +00:00
|
|
|
return token_ ? true : false;
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
LINK::~LINK()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
void LINK::reset()
|
|
|
|
{
|
2017-01-11 15:19:58 +00:00
|
|
|
if(token_)
|
|
|
|
{
|
2016-12-22 21:01:05 +00:00
|
|
|
token_->remove_observer( observer_ );
|
2017-01-02 07:17:10 +00:00
|
|
|
token_.reset();
|
2016-12-22 21:01:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
namespace DETAIL {
|
|
|
|
|
|
|
|
OBSERVABLE_BASE::OBSERVABLE_BASE()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
OBSERVABLE_BASE::OBSERVABLE_BASE( OBSERVABLE_BASE& other )
|
|
|
|
: impl_( other.get_shared_impl() )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
OBSERVABLE_BASE::~OBSERVABLE_BASE()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
void OBSERVABLE_BASE::allocate_impl()
|
|
|
|
{
|
|
|
|
if(!impl_)
|
2016-12-22 21:01:05 +00:00
|
|
|
impl_ = std::make_shared<IMPL>( this );
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
void OBSERVABLE_BASE::allocate_shared_impl()
|
|
|
|
{
|
2017-01-11 15:19:58 +00:00
|
|
|
if(!impl_)
|
2016-12-22 21:01:05 +00:00
|
|
|
impl_ = std::make_shared<IMPL>();
|
2017-01-11 15:19:58 +00:00
|
|
|
else
|
2016-12-22 21:01:05 +00:00
|
|
|
impl_->set_shared();
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
void OBSERVABLE_BASE::deallocate_impl() {
|
|
|
|
impl_.reset();
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2017-01-02 07:17:10 +00:00
|
|
|
std::shared_ptr<OBSERVABLE_BASE::IMPL> OBSERVABLE_BASE::get_shared_impl()
|
|
|
|
{
|
|
|
|
allocate_shared_impl();
|
|
|
|
return impl_;
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
|
|
|
void OBSERVABLE_BASE::add_observer( void* observer )
|
|
|
|
{
|
2017-01-02 07:17:10 +00:00
|
|
|
allocate_impl();
|
|
|
|
impl_->add_observer( observer );
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
|
|
|
void OBSERVABLE_BASE::remove_observer( void* observer )
|
|
|
|
{
|
2017-01-02 07:17:10 +00:00
|
|
|
assert( impl_ );
|
|
|
|
impl_->remove_observer( observer );
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
|
|
|
void OBSERVABLE_BASE::enter_iteration()
|
|
|
|
{
|
|
|
|
if( impl_ )
|
2017-01-02 07:17:10 +00:00
|
|
|
impl_->enter_iteration();
|
2016-12-22 21:01:05 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
|
|
|
void OBSERVABLE_BASE::leave_iteration()
|
|
|
|
{
|
|
|
|
if( impl_)
|
|
|
|
{
|
2017-01-02 07:17:10 +00:00
|
|
|
impl_->leave_iteration();
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
if( !impl_->is_iterating() && !impl_->is_shared() && impl_.use_count() == 1 )
|
2017-01-02 07:17:10 +00:00
|
|
|
impl_.reset();
|
2016-12-22 21:01:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
|
|
|
size_t OBSERVABLE_BASE::size() const
|
|
|
|
{
|
|
|
|
if( impl_ )
|
2017-01-02 07:17:10 +00:00
|
|
|
return impl_->observers_.size();
|
2017-01-11 15:19:58 +00:00
|
|
|
else
|
2017-01-02 07:17:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:19:58 +00:00
|
|
|
|
2016-12-22 21:01:05 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|