Generic watching support classes.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1573 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2002-10-18 14:01:09 +00:00
parent f4eaaa86d5
commit 6d8943122a
6 changed files with 424 additions and 0 deletions

View File

@ -0,0 +1,64 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// File Name: EventMaskWatcher.cpp
// Author: Ingo Weinhold (bonefish@users.sf.net)
// Description: EventMaskWatcher is a Watcher extended by an event mask.
// EventMaskWatcherFilter filters EventMaskWatchers with
// respect to their event mask and a specific event.
//------------------------------------------------------------------------------
#include "EventMaskWatcher.h"
// EventMaskWatcher
// constructor
EventMaskWatcher::EventMaskWatcher(const BMessenger &target, uint32 eventMask)
: Watcher(target),
fEventMask(eventMask)
{
}
// EventMask
uint32
EventMaskWatcher::EventMask() const
{
return fEventMask;
}
// EventMaskWatcherFilter
// constructor
EventMaskWatcherFilter::EventMaskWatcherFilter(uint32 event)
: WatcherFilter(),
fEvent(event)
{
}
// Filter
bool
EventMaskWatcherFilter::Filter(Watcher *watcher, BMessage *message)
{
EventMaskWatcher *emWatcher = dynamic_cast<EventMaskWatcher *>(watcher);
return (emWatcher && (emWatcher->EventMask() & fEvent));
}

View File

@ -0,0 +1,56 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// File Name: EventMaskWatcher.h
// Author: Ingo Weinhold (bonefish@users.sf.net)
// Description: EventMaskWatcher is a Watcher extended by an event mask.
// EventMaskWatcherFilter filters EventMaskWatchers with
// respect to their event mask and a specific event.
//------------------------------------------------------------------------------
#ifndef EVENT_MASK_WATCHER_H
#define EVENT_MASK_WATCHER_H
#include "Watcher.h"
// EventMaskWatcher
class EventMaskWatcher : public Watcher {
public:
EventMaskWatcher(const BMessenger &target, uint32 eventMask);
uint32 EventMask() const;
private:
uint32 fEventMask;
};
// EventMaskWatcherFilter
class EventMaskWatcherFilter : public WatcherFilter {
public:
EventMaskWatcherFilter(uint32 event);
virtual bool Filter(Watcher *watcher, BMessage *message);
private:
uint32 fEvent;
};
#endif // EVENT_MASK_WATCHER_H

View File

@ -0,0 +1,78 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// File Name: Watcher.cpp
// Author: Ingo Weinhold (bonefish@users.sf.net)
// Description: A Watcher represents a target of a watching service.
// A WatcherFilter represents a predicate on a Watcher.
//------------------------------------------------------------------------------
#include <Message.h>
#include "Watcher.h"
// Watcher
// constructor
Watcher::Watcher(const BMessenger &target)
: fTarget(target)
{
}
// destructor
Watcher::~Watcher()
{
}
// Target
const BMessenger&
Watcher::Target() const
{
return fTarget;
}
// SendMessage
status_t
Watcher::SendMessage(BMessage *message)
{
return fTarget.SendMessage(message, (BHandler*)NULL, 0);
}
// WatcherFilter
// constructor
WatcherFilter::WatcherFilter()
{
}
// destructor
WatcherFilter::~WatcherFilter()
{
}
// Filter
bool
WatcherFilter::Filter(Watcher *watcher, BMessage *message)
{
return true;
}

View File

@ -0,0 +1,56 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// File Name: Watcher.h
// Author: Ingo Weinhold (bonefish@users.sf.net)
// Description: A Watcher represents a target of a watching service.
// A WatcherFilter represents a predicate on a Watcher.
//------------------------------------------------------------------------------
#ifndef WATCHER_H
#define WATCHER_H
#include <Messenger.h>
// Watcher
class Watcher {
public:
Watcher(const BMessenger &target);
virtual ~Watcher();
const BMessenger &Target() const;
status_t SendMessage(BMessage *message);
private:
BMessenger fTarget;
};
// WatcherFilter
class WatcherFilter {
public:
WatcherFilter();
virtual ~WatcherFilter();
virtual bool Filter(Watcher *watcher, BMessage *message);
};
#endif // WATCHER_H

View File

@ -0,0 +1,114 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// File Name: WatchingService.cpp
// Author: Ingo Weinhold (bonefish@users.sf.net)
// Description: Features everything needed to provide a watching service.
//------------------------------------------------------------------------------
#include <List.h>
#include "Watcher.h"
#include "WatchingService.h"
// constructor
WatchingService::WatchingService()
: fWatchers()
{
}
// destructor
WatchingService::~WatchingService()
{
// delete the watchers
for (watcher_map::iterator it = fWatchers.begin();
it != fWatchers.end();
++it) {
delete it->second;
}
}
// AddWatcher
bool
WatchingService::AddWatcher(Watcher *watcher)
{
bool result = (watcher);
if (result) {
RemoveWatcher(watcher->Target(), true);
fWatchers[watcher->Target()] = watcher;
}
return result;
}
// AddWatcher
bool
WatchingService::AddWatcher(const BMessenger &target)
{
return AddWatcher(new(nothrow) Watcher(target));
}
// RemoveWatcher
bool
WatchingService::RemoveWatcher(Watcher *watcher, bool deleteWatcher)
{
return (watcher && RemoveWatcher(watcher->Target(), deleteWatcher));
}
// RemoveWatcher
bool
WatchingService::RemoveWatcher(const BMessenger &target, bool deleteWatcher)
{
watcher_map::iterator it = fWatchers.find(target);
bool result = (it != fWatchers.end());
if (result) {
if (deleteWatcher)
delete it->second;
fWatchers.erase(it);
}
return result;
}
// NotifyWatchers
void
WatchingService::NotifyWatchers(BMessage *message, WatcherFilter *filter)
{
BList staleWatchers;
// deliver the message
for (watcher_map::iterator it = fWatchers.begin();
it != fWatchers.end();
++it) {
Watcher *watcher = it->second;
// TODO: If a watcher is invalid, but the filter never selects it, it will
// not be removed.
if (!filter || filter->Filter(watcher, message)) {
status_t error = watcher->SendMessage(message);
if (error != B_OK && error != B_WOULD_BLOCK)
staleWatchers.AddItem(watcher);
}
}
// remove the stale watchers
for (int32 i = 0;
Watcher *watcher = (Watcher*)staleWatchers.ItemAt(i);
i++) {
RemoveWatcher(watcher, true);
}
}

View File

@ -0,0 +1,56 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// File Name: WatchingService.h
// Author: Ingo Weinhold (bonefish@users.sf.net)
// Description: Features everything needed to provide a watching service.
//------------------------------------------------------------------------------
#ifndef WATCHING_SERVICE_H
#define WATCHING_SERVICE_H
#include <map>
#include <Messenger.h>
class Watcher;
class WatcherFilter;
class WatchingService {
public:
WatchingService();
virtual ~WatchingService();
bool AddWatcher(Watcher *watcher);
bool AddWatcher(const BMessenger &target);
bool RemoveWatcher(Watcher *watcher, bool deleteWatcher = true);
bool RemoveWatcher(const BMessenger &target, bool deleteWatcher = true);
void NotifyWatchers(BMessage *message, WatcherFilter *filter = NULL);
private:
typedef map<BMessenger,Watcher*> watcher_map;
private:
watcher_map fWatchers;
};
#endif // WATCHING_SERVICE_H