From df226cd7b3906e8a33f0583f8efbf76712e59e50 Mon Sep 17 00:00:00 2001 From: ejakowatz Date: Mon, 15 Jul 2002 16:04:06 +0000 Subject: [PATCH] A new class to manage the list of loopers in a team. Provides the functionality of the existing static BLooper functions (which now call through to it) in a nicer package. New code should use the global instance of this (BPrivate::gLooperList) instead of the old BLooper functions (which are officially deprecated). git-svn-id: file:///srv/svn/repos/haiku/trunk/current@244 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/app/LooperList.h | 124 +++++++++++++++ src/kits/app/LooperList.cpp | 250 +++++++++++++++++++++++++++++++ 2 files changed, 374 insertions(+) create mode 100644 headers/private/app/LooperList.h create mode 100644 src/kits/app/LooperList.cpp diff --git a/headers/private/app/LooperList.h b/headers/private/app/LooperList.h new file mode 100644 index 0000000000..7358639d55 --- /dev/null +++ b/headers/private/app/LooperList.h @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// 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: LooperList.h +// Author(s): Erik Jaesler (erik@cgsoftware.com) +// Description: Maintains a global list of all loopers in a given team. +//------------------------------------------------------------------------------ + +#ifndef LOOPERLIST_H +#define LOOPERLIST_H + +// Standard Includes ----------------------------------------------------------- +#include + +// System Includes ------------------------------------------------------------- +#include +#include +#include + +// Project Includes ------------------------------------------------------------ + +// Local Includes -------------------------------------------------------------- + +// Local Defines --------------------------------------------------------------- + +// Globals --------------------------------------------------------------------- + +class BLooper; + +namespace BPrivate { + +class BLooperList +{ + public: + BLooperList(); + + bool Lock(); + void Unlock(); + bool IsLocked(); + + void AddLooper(BLooper* l); + bool IsLooperValid(const BLooper* l); + bool RemoveLooper(BLooper* l); + void GetLooperList(BList* list); + BLooper* LooperForThread(thread_id tid); + BLooper* LooperForName(const char* name); + BLooper* LooperForPort(port_id port); + + private: + struct LooperData + { + LooperData(); + LooperData(BLooper* loop, uint32 i); + LooperData(const LooperData& rhs); + LooperData& operator=(const LooperData& rhs); + + BLooper* looper; + uint32 id; + }; + + static bool EmptySlotPred(LooperData& Data); + struct FindLooperPred + { + FindLooperPred(const BLooper* loop) : looper(loop) {;} + bool operator()(LooperData& Data); + const BLooper* looper; + }; + struct FindThreadPred + { + FindThreadPred(thread_id tid) : thread(tid) {;} + bool operator()(LooperData& Data); + thread_id thread; + }; + struct FindNamePred + { + FindNamePred(const char* n) : name(n) {;} + bool operator()(LooperData& Data); + const char* name; + }; + struct FindPortPred + { + FindPortPred(port_id pid) : port(pid) {;} + bool operator()(LooperData& Data); + port_id port; + }; + + void AssertLocked(); + + BLocker fLock; + uint32 fLooperID; + std::vector fData; +}; + +extern _IMPEXP_BE BLooperList gLooperList; +} + + +#endif //LOOPERLIST_H + +/* + * $Log $ + * + * $Id $ + * + */ + diff --git a/src/kits/app/LooperList.cpp b/src/kits/app/LooperList.cpp new file mode 100644 index 0000000000..052edfaa09 --- /dev/null +++ b/src/kits/app/LooperList.cpp @@ -0,0 +1,250 @@ +//------------------------------------------------------------------------------ +// 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: LooperList.cpp +// Author(s): Erik Jaesler (erik@cgsoftware.com) +// Description: Maintains a global list of all loopers in a given team. +//------------------------------------------------------------------------------ + +// Standard Includes ----------------------------------------------------------- +#include + +// System Includes ------------------------------------------------------------- +#include +#include + +// Project Includes ------------------------------------------------------------ + +// Local Includes -------------------------------------------------------------- +#include "LooperList.h" + +// Local Defines --------------------------------------------------------------- + +// Globals --------------------------------------------------------------------- +using std::vector; + +namespace BPrivate { + +BLooperList gLooperList; + +typedef vector::iterator LDIter; + +//------------------------------------------------------------------------------ +BLooperList::BLooperList() + : fLooperID(0) +{ +} +//------------------------------------------------------------------------------ +bool BLooperList::Lock() +{ + return fLock.Lock(); +} +//------------------------------------------------------------------------------ +void BLooperList::Unlock() +{ + fLock.Unlock(); +} +//------------------------------------------------------------------------------ +bool BLooperList::IsLocked() +{ + return fLock.IsLocked(); +} +//------------------------------------------------------------------------------ +void BLooperList::AddLooper(BLooper* looper) +{ + BAutolock Listlock(fLock); + AssertLocked(); + if (!IsLooperValid(looper)) + { + LDIter i = find_if(fData.begin(), fData.end(), EmptySlotPred); + if (i == fData.end()) + { + fData.push_back(LooperData(looper, ++fLooperID)); + looper->fLooperID = fLooperID; + looper->Lock(); + } + else + { + i->looper = looper; + i->id = ++fLooperID; + looper->fLooperID = fLooperID; + looper->Lock(); + } + } +} +//------------------------------------------------------------------------------ +bool BLooperList::IsLooperValid(const BLooper* looper) +{ + BAutolock Listlock(fLock); + AssertLocked(); + + return find_if(fData.begin(), fData.end(), FindLooperPred(looper)) != + fData.end(); +} +//------------------------------------------------------------------------------ +bool BLooperList::RemoveLooper(BLooper* looper) +{ + BAutolock Listlock(fLock); + AssertLocked(); + + LDIter i = find_if(fData.begin(), fData.end(), FindLooperPred(looper)); + if (i != fData.end()) + { + i->looper = NULL; + return true; + } + + return false; +} +//------------------------------------------------------------------------------ +void BLooperList::GetLooperList(BList* list) +{ + BAutolock Listlock(fLock); + AssertLocked(); + for (uint32 i = 0; i < fData.size(); ++i) + { + list->AddItem(fData[i].looper); + } +} +//------------------------------------------------------------------------------ +BLooper* BLooperList::LooperForThread(thread_id tid) +{ + BAutolock Listlock(fLock); + AssertLocked(); + BLooper* looper = NULL; + LDIter i = find_if(fData.begin(), fData.end(), FindThreadPred(tid)); + if (i != fData.end()) + { + looper = i->looper; + } + + return looper; +} +//------------------------------------------------------------------------------ +BLooper* BLooperList::LooperForName(const char* name) +{ + BAutolock Listlock(fLock); + AssertLocked(); + BLooper* looper = NULL; + LDIter i = find_if(fData.begin(), fData.end(), FindNamePred(name)); + if (i != fData.end()) + { + looper = i->looper; + } + + return looper; +} +//------------------------------------------------------------------------------ +BLooper* BLooperList::LooperForPort(port_id port) +{ + BAutolock Listlock(fLock); + AssertLocked(); + BLooper* looper = NULL; + LDIter i = find_if(fData.begin(), fData.end(), FindPortPred(port)); + if (i != fData.end()) + { + looper = i->looper; + } + + return looper; +} +//------------------------------------------------------------------------------ +bool BLooperList::EmptySlotPred(LooperData& Data) +{ + return Data.looper == NULL; +} +//------------------------------------------------------------------------------ +void BLooperList::AssertLocked() +{ + if (!IsLocked()) + { + debugger("looperlist is not locked; proceed at great risk!"); + } +} +//------------------------------------------------------------------------------ + + +//------------------------------------------------------------------------------ +// #pragma mark - +// #pragma mark BLooperList::LooperData +// #pragma mark - +//------------------------------------------------------------------------------ +BLooperList::LooperData::LooperData() + : looper(NULL), id(0) +{ +} +//------------------------------------------------------------------------------ +BLooperList::LooperData::LooperData(BLooper* loop, uint32 i) + : looper(loop), id(i) +{ + ; +} +//------------------------------------------------------------------------------ +BLooperList::LooperData::LooperData(const LooperData& rhs) +{ + *this = rhs; +} +//------------------------------------------------------------------------------ +BLooperList::LooperData& +BLooperList::LooperData::operator=(const LooperData& rhs) +{ + if (this != &rhs) + { + looper = rhs.looper; + id = rhs.id; + } + + return *this; +} +//------------------------------------------------------------------------------ + + +//------------------------------------------------------------------------------ +bool BLooperList::FindLooperPred::operator()(BLooperList::LooperData& Data) +{ + return looper == Data.looper; +} +//------------------------------------------------------------------------------ +bool BLooperList::FindThreadPred::operator()(LooperData& Data) +{ + return thread == Data.looper->Thread(); +} +//------------------------------------------------------------------------------ +bool BLooperList::FindNamePred::operator()(LooperData& Data) +{ + return strcmp(name, Data.looper->Name()) == 0; +} +//------------------------------------------------------------------------------ +bool BLooperList::FindPortPred::operator()(LooperData& Data) +{ + return port == _get_looper_port_(Data.looper); +} +//------------------------------------------------------------------------------ + +} // namespace BPrivate + +/* + * $Log $ + * + * $Id $ + * + */ +