mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-16 18:56:52 +02:00
trunk: backport eventing from 1.4.3
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// FunctionDelegate.h
|
||||
//
|
||||
// $Id: //poco/svn/Foundation/include/Poco/FunctionDelegate.h#2 $
|
||||
// $Id: //poco/1.4/Foundation/include/Poco/FunctionDelegate.h#4 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Events
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
// Implementation of the FunctionDelegate 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,12 +36,13 @@
|
||||
//
|
||||
|
||||
|
||||
#ifndef Foundation_FunctionDelegate_INCLUDED
|
||||
#define Foundation_FunctionDelegate_INCLUDED
|
||||
#ifndef Foundation_FunctionDelegate_INCLUDED
|
||||
#define Foundation_FunctionDelegate_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/AbstractDelegate.h"
|
||||
#include "Poco/Mutex.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@@ -49,17 +50,16 @@ namespace Poco {
|
||||
|
||||
template <class TArgs, bool hasSender = true, bool senderIsConst = true>
|
||||
class FunctionDelegate: public AbstractDelegate<TArgs>
|
||||
/// Wraps a C style function (or a C++ static function) to be used as
|
||||
/// a delegate
|
||||
/// Wraps a freestanding function or static member function
|
||||
/// for use as a Delegate.
|
||||
{
|
||||
public:
|
||||
typedef void (*NotifyMethod)(const void*, TArgs&);
|
||||
typedef void (*NotifyMethod)(const void*, TArgs&);
|
||||
|
||||
FunctionDelegate(NotifyMethod method):
|
||||
AbstractDelegate<TArgs>(*reinterpret_cast<void**>(&method)),
|
||||
_receiverMethod(method)
|
||||
{
|
||||
}
|
||||
FunctionDelegate(NotifyMethod method):
|
||||
_receiverMethod(method)
|
||||
{
|
||||
}
|
||||
|
||||
FunctionDelegate(const FunctionDelegate& delegate):
|
||||
AbstractDelegate<TArgs>(delegate),
|
||||
@@ -81,22 +81,40 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool notify(const void* sender, TArgs& arguments)
|
||||
{
|
||||
(*_receiverMethod)(sender, arguments);
|
||||
return true; // a "standard" delegate never expires
|
||||
}
|
||||
bool notify(const void* sender, TArgs& arguments)
|
||||
{
|
||||
Mutex::ScopedLock lock(_mutex);
|
||||
if (_receiverMethod)
|
||||
{
|
||||
(*_receiverMethod)(sender, arguments);
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
AbstractDelegate<TArgs>* clone() const
|
||||
bool equals(const AbstractDelegate<TArgs>& other) const
|
||||
{
|
||||
const FunctionDelegate* pOtherDelegate = dynamic_cast<const FunctionDelegate*>(other.unwrap());
|
||||
return pOtherDelegate && _receiverMethod == pOtherDelegate->_receiverMethod;
|
||||
}
|
||||
|
||||
AbstractDelegate<TArgs>* clone() const
|
||||
{
|
||||
return new FunctionDelegate(*this);
|
||||
}
|
||||
return new FunctionDelegate(*this);
|
||||
}
|
||||
|
||||
void disable()
|
||||
{
|
||||
Mutex::ScopedLock lock(_mutex);
|
||||
_receiverMethod = 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
NotifyMethod _receiverMethod;
|
||||
NotifyMethod _receiverMethod;
|
||||
Mutex _mutex;
|
||||
|
||||
private:
|
||||
FunctionDelegate();
|
||||
FunctionDelegate();
|
||||
};
|
||||
|
||||
|
||||
@@ -104,13 +122,12 @@ template <class TArgs>
|
||||
class FunctionDelegate<TArgs, true, false>: public AbstractDelegate<TArgs>
|
||||
{
|
||||
public:
|
||||
typedef void (*NotifyMethod)(void*, TArgs&);
|
||||
typedef void (*NotifyMethod)(void*, TArgs&);
|
||||
|
||||
FunctionDelegate(NotifyMethod method):
|
||||
AbstractDelegate<TArgs>(*reinterpret_cast<void**>(&method)),
|
||||
_receiverMethod(method)
|
||||
{
|
||||
}
|
||||
FunctionDelegate(NotifyMethod method):
|
||||
_receiverMethod(method)
|
||||
{
|
||||
}
|
||||
|
||||
FunctionDelegate(const FunctionDelegate& delegate):
|
||||
AbstractDelegate<TArgs>(delegate),
|
||||
@@ -132,22 +149,40 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool notify(const void* sender, TArgs& arguments)
|
||||
{
|
||||
(*_receiverMethod)(const_cast<void*>(sender), arguments);
|
||||
return true; // a "standard" delegate never expires
|
||||
}
|
||||
bool notify(const void* sender, TArgs& arguments)
|
||||
{
|
||||
Mutex::ScopedLock lock(_mutex);
|
||||
if (_receiverMethod)
|
||||
{
|
||||
(*_receiverMethod)(const_cast<void*>(sender), arguments);
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
AbstractDelegate<TArgs>* clone() const
|
||||
bool equals(const AbstractDelegate<TArgs>& other) const
|
||||
{
|
||||
const FunctionDelegate* pOtherDelegate = dynamic_cast<const FunctionDelegate*>(other.unwrap());
|
||||
return pOtherDelegate && _receiverMethod == pOtherDelegate->_receiverMethod;
|
||||
}
|
||||
|
||||
AbstractDelegate<TArgs>* clone() const
|
||||
{
|
||||
return new FunctionDelegate(*this);
|
||||
}
|
||||
return new FunctionDelegate(*this);
|
||||
}
|
||||
|
||||
void disable()
|
||||
{
|
||||
Mutex::ScopedLock lock(_mutex);
|
||||
_receiverMethod = 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
NotifyMethod _receiverMethod;
|
||||
NotifyMethod _receiverMethod;
|
||||
Mutex _mutex;
|
||||
|
||||
private:
|
||||
FunctionDelegate();
|
||||
FunctionDelegate();
|
||||
};
|
||||
|
||||
|
||||
@@ -155,13 +190,12 @@ template <class TArgs, bool senderIsConst>
|
||||
class FunctionDelegate<TArgs, false, senderIsConst>: public AbstractDelegate<TArgs>
|
||||
{
|
||||
public:
|
||||
typedef void (*NotifyMethod)(TArgs&);
|
||||
typedef void (*NotifyMethod)(TArgs&);
|
||||
|
||||
FunctionDelegate(NotifyMethod method):
|
||||
AbstractDelegate<TArgs>(*reinterpret_cast<void**>(&method)),
|
||||
_receiverMethod(method)
|
||||
{
|
||||
}
|
||||
FunctionDelegate(NotifyMethod method):
|
||||
_receiverMethod(method)
|
||||
{
|
||||
}
|
||||
|
||||
FunctionDelegate(const FunctionDelegate& delegate):
|
||||
AbstractDelegate<TArgs>(delegate),
|
||||
@@ -183,26 +217,44 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool notify(const void* sender, TArgs& arguments)
|
||||
{
|
||||
(*_receiverMethod)(arguments);
|
||||
return true; // a "standard" delegate never expires
|
||||
}
|
||||
bool notify(const void* sender, TArgs& arguments)
|
||||
{
|
||||
Mutex::ScopedLock lock(_mutex);
|
||||
if (_receiverMethod)
|
||||
{
|
||||
(*_receiverMethod)(arguments);
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
AbstractDelegate<TArgs>* clone() const
|
||||
bool equals(const AbstractDelegate<TArgs>& other) const
|
||||
{
|
||||
const FunctionDelegate* pOtherDelegate = dynamic_cast<const FunctionDelegate*>(other.unwrap());
|
||||
return pOtherDelegate && _receiverMethod == pOtherDelegate->_receiverMethod;
|
||||
}
|
||||
|
||||
AbstractDelegate<TArgs>* clone() const
|
||||
{
|
||||
return new FunctionDelegate(*this);
|
||||
}
|
||||
return new FunctionDelegate(*this);
|
||||
}
|
||||
|
||||
void disable()
|
||||
{
|
||||
Mutex::ScopedLock lock(_mutex);
|
||||
_receiverMethod = 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
NotifyMethod _receiverMethod;
|
||||
NotifyMethod _receiverMethod;
|
||||
Mutex _mutex;
|
||||
|
||||
private:
|
||||
FunctionDelegate();
|
||||
FunctionDelegate();
|
||||
};
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
|
||||
#endif
|
||||
#endif // Foundation_FunctionDelegate_INCLUDED
|
||||
|
Reference in New Issue
Block a user