+
+//================================================================================
+/*!
+ * \brief Sets an event listener and its data to a submesh
+ * \param listener - the listener to store
+ * \param data - the listener data to store
+ * \param where - the submesh to store the listener and it's data
+ * \param deleteListener - if true then the listener will be deleted as
+ * it is removed from where submesh
+ *
+ * It remembers the submesh where it puts the listener in order to delete
+ * them when HYP_OK algo_state is lost
+ * After being set, event listener is notified on each event of where submesh.
+ */
+//================================================================================
+
+void SMESH_subMesh::SetEventListener(EventListener* listener,
+ EventListenerData* data,
+ SMESH_subMesh* where)
+{
+ if ( listener && where ) {
+ where->SetEventListener( listener, data );
+ myOwnListeners.push_back( make_pair( where, listener ));
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Sets an event listener and its data to a submesh
+ * \param listener - the listener to store
+ * \param data - the listener data to store
+ *
+ * After being set, event listener is notified on each event of a submesh.
+ */
+//================================================================================
+
+void SMESH_subMesh::SetEventListener(EventListener* listener, EventListenerData* data)
+{
+ map< EventListener*, EventListenerData* >::iterator l_d =
+ myEventListeners.find( listener );
+ if ( l_d != myEventListeners.end() ) {
+ EventListenerData* curData = l_d->second;
+ if ( curData && curData != data && curData->IsDeletable() )
+ delete curData;
+ l_d->second = data;
+ }
+ else
+ myEventListeners.insert( make_pair( listener, data ));
+}
+
+//================================================================================
+/*!
+ * \brief Return an event listener data
+ * \param listener - the listener whose data is
+ * \retval EventListenerData* - found data, maybe NULL
+ */
+//================================================================================
+
+EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
+{
+ map< EventListener*, EventListenerData* >::const_iterator l_d =
+ myEventListeners.find( listener );
+ if ( l_d != myEventListeners.end() )
+ return l_d->second;
+ return 0;
+}
+
+//================================================================================
+/*!
+ * \brief Notify stored event listeners on the occured event
+ * \param event - algo_event or compute_event itself
+ * \param eventType - algo_event or compute_event
+ * \param hyp - hypothesis, if eventType is algo_event
+ */
+//================================================================================
+
+void SMESH_subMesh::NotifyListenersOnEvent( const int event,
+ const event_type eventType,
+ SMESH_Hypothesis* hyp)
+{
+ map< EventListener*, EventListenerData* >::iterator l_d = myEventListeners.begin();
+ for ( ; l_d != myEventListeners.end(); ++l_d )
+ if ( (*l_d).first->myBusySM.insert( this ).second )
+ {
+ l_d->first->ProcessEvent( event, eventType, this, l_d->second, hyp );
+ l_d->first->myBusySM.erase( this );
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Unregister the listener and delete listener's data
+ * \param listener - the event listener
+ */
+//================================================================================
+
+void SMESH_subMesh::DeleteEventListener(EventListener* listener)
+{
+ map< EventListener*, EventListenerData* >::iterator l_d =
+ myEventListeners.find( listener );
+ if ( l_d != myEventListeners.end() ) {
+ if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
+ if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
+ myEventListeners.erase( l_d );
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Delete event listeners depending on algo of this submesh
+ */
+//================================================================================
+
+void SMESH_subMesh::DeleteOwnListeners()
+{
+ list< pair< SMESH_subMesh*, EventListener* > >::iterator sm_l;
+ for ( sm_l = myOwnListeners.begin(); sm_l != myOwnListeners.end(); ++sm_l)
+ sm_l->first->DeleteEventListener( sm_l->second );
+ myOwnListeners.clear();
+}
+
+//================================================================================
+/*!
+ * \brief Do something on a certain event
+ * \param event - algo_event or compute_event itself
+ * \param eventType - algo_event or compute_event
+ * \param subMesh - the submesh where the event occures
+ * \param data - listener data stored in the subMesh
+ * \param hyp - hypothesis, if eventType is algo_event
+ *
+ * The base implementation translates CLEAN event to the subMesh
+ * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
+ * successful COMPUTE event.
+ */
+//================================================================================
+
+void SMESH_subMeshEventListener::ProcessEvent(const int event,
+ const int eventType,
+ SMESH_subMesh* subMesh,
+ EventListenerData* data,
+ const SMESH_Hypothesis* /*hyp*/)
+{
+ if ( data && !data->mySubMeshes.empty() &&
+ eventType == SMESH_subMesh::COMPUTE_EVENT)
+ {
+ ASSERT( data->mySubMeshes.front() != subMesh );
+ list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
+ list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
+ switch ( event ) {
+ case SMESH_subMesh::CLEAN:
+ for ( ; smIt != smEnd; ++ smIt)
+ (*smIt)->ComputeStateEngine( event );
+ break;
+ case SMESH_subMesh::COMPUTE:
+ if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
+ for ( ; smIt != smEnd; ++ smIt)
+ (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
+ break;
+ default:;
+ }
+ }
+}
+
+namespace {
+
+ //================================================================================
+ /*!
+ * \brief Iterator over submeshes and optionally prepended or appended one
+ */
+ //================================================================================
+
+ struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
+ {
+ _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
+ SMESH_subMesh* prepend,
+ SMESH_subMesh* append): myIt(subIt),myAppend(append)
+ {
+ myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
+ if ( myCur == append ) append = 0;
+ }
+ /// Return true if and only if there are other object in this iterator
+ virtual bool more()
+ {
+ return myCur;
+ }
+ /// Return the current object and step to the next one
+ virtual SMESH_subMesh* next()
+ {
+ SMESH_subMesh* res = myCur;
+ if ( myIt->more() ) { myCur = myIt->next(); }
+ else { myCur = myAppend; myAppend = 0; }
+ return res;
+ }
+ /// ~
+ ~_Iterator()
+ { delete myIt; }
+ ///
+ SMESH_subMesh *myAppend, *myCur;
+ SMDS_Iterator<SMESH_subMesh*> *myIt;
+ };
+}
+
+//================================================================================
+/*!
+ * \brief Return iterator on the submeshes this one depends on
+ * \param includeSelf - this submesh to be returned also
+ * \param reverse - if true, complex shape submeshes go first
+ */
+//================================================================================
+
+SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
+ const bool reverse)
+{
+ SMESH_subMesh *prepend=0, *append=0;
+ if ( includeSelf ) {
+ if ( reverse ) prepend = this;
+ else append = this;
+ }
+ typedef map < int, SMESH_subMesh * > TMap;
+ if ( reverse )
+ {
+ return SMESH_subMeshIteratorPtr
+ ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
+ }
+ {
+ return SMESH_subMeshIteratorPtr
+ ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Find common submeshes (based on shared subshapes with other
+ * \param theOther submesh to check
+ * \param theSetOfCommon set of common submesh
+ */
+//================================================================================
+
+bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
+ std::set<const SMESH_subMesh*>& theSetOfCommon ) const
+{
+ int oldNb = theSetOfCommon.size();
+ // check main submeshes
+ const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
+ if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
+ theSetOfCommon.insert( this );
+ if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
+ theSetOfCommon.insert( theOther );
+ // check common submeshes
+ map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
+ for( ; mapIt != _mapDepend.end(); mapIt++ )
+ if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
+ theSetOfCommon.insert( (*mapIt).second );
+ return oldNb < theSetOfCommon.size();
+}