Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / nel / src / misc / cdb_branch_observing_handler.cpp
blob8e348a058b0f1cef865a5deb4748f28c8b686ac2
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "stdmisc.h"
21 #include "nel/misc/cdb_branch_observing_handler.h"
23 #ifdef DEBUG_NEW
24 #define new DEBUG_NEW
25 #endif
27 namespace NLMISC{
28 CCDBBranchObservingHandler::CCDBBranchObservingHandler()
30 reset();
33 CCDBBranchObservingHandler::~CCDBBranchObservingHandler()
35 reset();
38 void CCDBBranchObservingHandler::flushObserverCalls()
40 bool flushed = false;
44 flushed = false;
45 uint oldList = currentList;
46 currentList = 1 - currentList;
48 std::list< CCDBNodeBranch::ICDBDBBranchObserverHandle* >::iterator itr
49 = flushableObservers[ oldList ].begin();
51 while( itr != flushableObservers[ oldList ].end() )
53 currentHandle = *itr;
54 ++itr;
56 if( currentHandle->observer() != NULL )
57 currentHandle->observer()->update( currentHandle->owner() );
59 // Update might have removed it
60 if( currentHandle != NULL )
61 currentHandle->removeFromFlushableList( oldList );
63 currentHandle = NULL;
64 flushed = true;
66 triggerFlushObservers();
68 }while( flushed );
70 triggerFlushObservers();
73 void CCDBBranchObservingHandler::reset()
75 currentList = 0;
76 currentHandle = NULL;
78 for( uint i = 0; i < MAX_OBS_LST; i++ )
79 flushableObservers[ i ].clear();
82 void CCDBBranchObservingHandler::addBranchObserver( CCDBNodeBranch *branch, ICDBNode::IPropertyObserver *observer, const std::vector< std::string >& positiveLeafNameFilter )
84 if( branch == NULL )
85 return;
87 CCDBDBBranchObserverHandle *handle = new CCDBDBBranchObserverHandle( observer, branch, this );
88 branch->addBranchObserver( handle, positiveLeafNameFilter );
91 void CCDBBranchObservingHandler::addBranchObserver( CCDBNodeBranch *branch, const char *dbPathFromThisNode, ICDBNode::IPropertyObserver &observer, const char **positiveLeafNameFilter, uint positiveLeafNameFilterSize )
93 if( branch == NULL )
94 return;
96 CCDBDBBranchObserverHandle *handle = new CCDBDBBranchObserverHandle( &observer, branch, this );
97 branch->addBranchObserver( handle, dbPathFromThisNode, positiveLeafNameFilter, positiveLeafNameFilterSize );
100 void CCDBBranchObservingHandler::removeBranchObserver( CCDBNodeBranch *branch, ICDBNode::IPropertyObserver* observer )
102 branch->removeBranchObserver( observer );
105 void CCDBBranchObservingHandler::removeBranchObserver( CCDBNodeBranch *branch, const char *dbPathFromThisNode, ICDBNode::IPropertyObserver &observer )
107 branch->removeBranchObserver( dbPathFromThisNode, observer );
110 void CCDBBranchObservingHandler::triggerFlushObservers()
112 for( std::vector< IBranchObserverCallFlushObserver* >::iterator itr = flushObservers.begin();
113 itr != flushObservers.end(); itr++ )
114 (*itr)->onObserverCallFlush();
117 void CCDBBranchObservingHandler::addFlushObserver( IBranchObserverCallFlushObserver *observer )
119 std::vector< IBranchObserverCallFlushObserver* >::iterator itr
120 = std::find( flushObservers.begin(), flushObservers.end(), observer );
122 if( itr != flushObservers.end() )
123 return;
125 flushObservers.push_back( observer );
128 void CCDBBranchObservingHandler::removeFlushObserver( IBranchObserverCallFlushObserver *observer )
130 std::vector< IBranchObserverCallFlushObserver* >::iterator itr
131 = std::find( flushObservers.begin(), flushObservers.end(), observer );
133 if( itr == flushObservers.end() )
134 return;
136 flushObservers.erase( itr );
139 CCDBBranchObservingHandler::CCDBDBBranchObserverHandle::CCDBDBBranchObserverHandle( NLMISC::ICDBNode::IPropertyObserver *observer, NLMISC::CCDBNodeBranch *owner, CCDBBranchObservingHandler *handler )
141 std::fill( _inList, _inList + MAX_OBS_LST, false );
142 _observer = observer;
143 _owner = owner;
144 _handler = handler;
147 CCDBBranchObservingHandler::CCDBDBBranchObserverHandle::~CCDBDBBranchObserverHandle()
149 _observer = NULL;
152 bool CCDBBranchObservingHandler::CCDBDBBranchObserverHandle::observesLeaf( const std::string &leafName )
154 if( !_observedLeaves.empty() ){
155 std::vector< std::string >::iterator itr
156 = std::find( _observedLeaves.begin(), _observedLeaves.end(), leafName );
158 if( itr == _observedLeaves.end() )
159 return false;
160 else
161 return true;
164 return true;
167 bool CCDBBranchObservingHandler::CCDBDBBranchObserverHandle::inList( uint list )
169 return _inList[ list ];
172 void CCDBBranchObservingHandler::CCDBDBBranchObserverHandle::addToFlushableList()
174 uint list = _handler->currentList;
176 if( _inList[ list ] )
177 return;
179 _handler->flushableObservers[ list ].push_back( this );
180 _inList[ list ] = true;
183 void CCDBBranchObservingHandler::CCDBDBBranchObserverHandle::removeFromFlushableList( uint list )
185 if( !_inList[ list ] )
186 return;
188 std::list< CCDBNodeBranch::ICDBDBBranchObserverHandle* >::iterator itr
189 = std::find( _handler->flushableObservers[ list ].begin(),
190 _handler->flushableObservers[ list ].end(), this );
192 if( itr == _handler->flushableObservers[ list ].end() )
193 return;
195 if( _handler->currentHandle == this )
196 _handler->currentHandle = NULL;
198 _handler->flushableObservers[ list ].erase( itr );
199 _inList[ list ] = false;
203 void CCDBBranchObservingHandler::CCDBDBBranchObserverHandle::removeFromFlushableList()
205 for( uint i = 0; i < MAX_OBS_LST; i++ )
206 removeFromFlushableList( i );