Assertion that the ObserverManager is empty when it's destroyed and verbosity.

This commit is contained in:
King_DuckZ 2014-07-10 20:01:22 +02:00
parent 9c660caec6
commit 3f79507beb

View file

@ -28,6 +28,14 @@
#include <tree.hh> #include <tree.hh>
#include <boost/iterator/transform_iterator.hpp> #include <boost/iterator/transform_iterator.hpp>
#if defined(__GNUC__) && __GNUC__ >= 2 && !defined(NDEBUG)
# define OBS_MANAGER_LOG
#endif
#if defined(OBS_MANAGER_LOG)
# include <iostream>
#endif
namespace cloonel { namespace cloonel {
template <typename T> template <typename T>
class ObserversManager { class ObserversManager {
@ -61,7 +69,7 @@ namespace cloonel {
typedef boost::transform_iterator<std::function<T&(TicketedWrapper&)>, TreeIteratorType> iterator; typedef boost::transform_iterator<std::function<T&(TicketedWrapper&)>, TreeIteratorType> iterator;
ObserversManager ( void ); ObserversManager ( void );
~ObserversManager ( void ) noexcept = default; ~ObserversManager ( void ) noexcept;
TicketType Add ( T parObserver, TicketType parParent=Ticket_Null ); TicketType Add ( T parObserver, TicketType parParent=Ticket_Null );
void Remove ( TicketType parTicket ) noexcept; void Remove ( TicketType parTicket ) noexcept;
@ -85,6 +93,13 @@ namespace cloonel {
{ {
} }
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T>
ObserversManager<T>::~ObserversManager() noexcept {
assert(m_tree.empty());
}
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
template <typename T> template <typename T>
@ -95,6 +110,9 @@ namespace cloonel {
else else
m_tree.append_child(GetByTicket_AssertPresent(parParent), TicketedWrapper(parObserver, currTicket)); m_tree.append_child(GetByTicket_AssertPresent(parParent), TicketedWrapper(parObserver, currTicket));
#if defined(OBS_MANAGER_LOG)
std::cout << __PRETTY_FUNCTION__ << " registering " << currTicket << " as a child of " << parParent << "\n";
#endif
++m_nextTicket; ++m_nextTicket;
return currTicket; return currTicket;
} }
@ -104,6 +122,11 @@ namespace cloonel {
template <typename T> template <typename T>
void ObserversManager<T>::Remove (TicketType parTicket) noexcept { void ObserversManager<T>::Remove (TicketType parTicket) noexcept {
auto deleme = GetByTicket_AssertPresent(parTicket); auto deleme = GetByTicket_AssertPresent(parTicket);
#if defined(OBS_MANAGER_LOG)
for (auto it(deleme); it != m_tree.end(); ++it) {
std::cout << __PRETTY_FUNCTION__ << " unregistering " << it->ticket << "\n";
}
#endif
m_tree.erase(deleme); m_tree.erase(deleme);
if (parTicket == m_nextTicket - 1) if (parTicket == m_nextTicket - 1)
--m_nextTicket; --m_nextTicket;
@ -126,4 +149,7 @@ namespace cloonel {
} }
} //namespace cloonel } //namespace cloonel
#if defined(OBS_MANAGER_LOG)
# undef OBS_MANAGER_LOG
#endif
#endif #endif