1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
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/>.
21 #include "nel/misc/cdb_branch_observing_handler.h"
28 CCDBBranchObservingHandler::CCDBBranchObservingHandler()
33 CCDBBranchObservingHandler::~CCDBBranchObservingHandler()
38 void CCDBBranchObservingHandler::flushObserverCalls()
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() )
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
);
66 triggerFlushObservers();
70 triggerFlushObservers();
73 void CCDBBranchObservingHandler::reset()
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
)
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
)
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() )
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() )
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
;
147 CCDBBranchObservingHandler::CCDBDBBranchObserverHandle::~CCDBDBBranchObserverHandle()
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() )
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
] )
179 _handler
->flushableObservers
[ list
].push_back( this );
180 _inList
[ list
] = true;
183 void CCDBBranchObservingHandler::CCDBDBBranchObserverHandle::removeFromFlushableList( uint list
)
185 if( !_inList
[ list
] )
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() )
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
);