trunk: backport eventing from 1.4.3

This commit is contained in:
Marian Krivos
2012-02-05 12:16:58 +00:00
parent 59fe68edbe
commit 7d7c02c579
412 changed files with 3564 additions and 3634 deletions

View File

@@ -1,7 +1,7 @@
//
// DefaultStrategy.h
//
// $Id: //poco/Main/Foundation/include/Poco/DefaultStrategy.h#5 $
// $Id: //poco/1.4/Foundation/include/Poco/DefaultStrategy.h#3 $
//
// Library: Foundation
// Package: Events
@@ -9,7 +9,7 @@
//
// Implementation of the DefaultStrategy template.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// Copyright (c) 2006-2011, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
@@ -36,124 +36,100 @@
//
#ifndef Foundation_DefaultStrategy_INCLUDED
#define Foundation_DefaultStrategy_INCLUDED
#ifndef Foundation_DefaultStrategy_INCLUDED
#define Foundation_DefaultStrategy_INCLUDED
#include "Poco/NotificationStrategy.h"
#include <memory>
#include <set>
#include "Poco/SharedPtr.h"
#include <vector>
namespace Poco {
template <class TArgs, class TDelegate, class TCompare>
class DefaultStrategy//: public NotificationStrategy<TArgs, TDelegate>
/// Default notification strategy. Allows one observer
/// to register exactly once. The observer must provide an
/// < (less-than) operator.
template <class TArgs, class TDelegate>
class DefaultStrategy: public NotificationStrategy<TArgs, TDelegate>
/// Default notification strategy.
///
/// Internally, a std::vector<> is used to store
/// delegate objects. Delegates are invoked in the
/// order in which they have been registered.
{
public:
typedef std::set<TDelegate*, TCompare> Delegates;
typedef typename Delegates::iterator Iterator;
typedef typename Delegates::const_iterator ConstIterator;
typedef SharedPtr<TDelegate> DelegatePtr;
typedef std::vector<DelegatePtr> Delegates;
typedef typename Delegates::iterator Iterator;
public:
DefaultStrategy()
{
}
DefaultStrategy()
{
}
DefaultStrategy(const DefaultStrategy& s)
{
operator = (s);
}
DefaultStrategy(const DefaultStrategy& s):
_delegates(s._delegates)
{
}
~DefaultStrategy()
{
clear();
}
~DefaultStrategy()
{
}
void notify(const void* sender, TArgs& arguments)
{
std::vector<Iterator> delMe;
void notify(const void* sender, TArgs& arguments)
{
for (Iterator it = _delegates.begin(); it != _delegates.end(); ++it)
{
(*it)->notify(sender, arguments);
}
}
for (Iterator it = _observers.begin(); it != _observers.end(); ++it)
{
if (!(*it)->notify(sender, arguments))
{
// schedule for deletion
delMe.push_back(it);
}
}
while (!delMe.empty())
{
typename std::vector<Iterator>::iterator vit = delMe.end();
--vit;
delete **vit;
_observers.erase(*vit);
delMe.pop_back();
}
}
void add(const TDelegate& delegate)
{
_delegates.push_back(DelegatePtr(static_cast<TDelegate*>(delegate.clone())));
}
void add(const TDelegate& delegate)
{
Iterator it = _observers.find(const_cast<TDelegate*>(&delegate));
if (it != _observers.end())
{
delete *it;
_observers.erase(it);
}
std::auto_ptr<TDelegate> pDelegate(delegate.clone());
bool tmp = _observers.insert(pDelegate.get()).second;
poco_assert (tmp);
pDelegate.release();
}
void remove(const TDelegate& delegate)
{
Iterator it = _observers.find(const_cast<TDelegate*>(&delegate));
if (it != _observers.end())
{
delete *it;
_observers.erase(it);
}
}
void remove(const TDelegate& delegate)
{
for (Iterator it = _delegates.begin(); it != _delegates.end(); ++it)
{
if (delegate.equals(**it))
{
(*it)->disable();
_delegates.erase(it);
return;
}
}
}
DefaultStrategy& operator = (const DefaultStrategy& s)
{
if (this != &s)
{
for (ConstIterator it = s._observers.begin(); it != s._observers.end(); ++it)
{
add(**it);
}
}
return *this;
}
{
if (this != &s)
{
_delegates = s._delegates;
}
return *this;
}
void clear()
{
for (Iterator it = _observers.begin(); it != _observers.end(); ++it)
{
delete *it;
}
_observers.clear();
}
void clear()
{
for (Iterator it = _delegates.begin(); it != _delegates.end(); ++it)
{
(*it)->disable();
}
_delegates.clear();
}
bool empty() const
{
return _observers.empty();
}
bool empty() const
{
return _delegates.empty();
}
protected:
Delegates _observers;
Delegates _delegates;
};
} // namespace Poco
#endif
#endif // Foundation_DefaultStrategy_INCLUDED