From a6dbddff43ce0990fb05e31269271a207f6c334f Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sat, 25 Apr 2009 16:51:05 +0000 Subject: [PATCH] A key-based mechanism to manage sub windows. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30402 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/debuganalyzer/SubWindow.cpp | 98 +++++++++++++++++++++ src/apps/debuganalyzer/SubWindow.h | 63 +++++++++++++ src/apps/debuganalyzer/SubWindowManager.cpp | 81 +++++++++++++++++ src/apps/debuganalyzer/SubWindowManager.h | 66 ++++++++++++++ 4 files changed, 308 insertions(+) create mode 100644 src/apps/debuganalyzer/SubWindow.cpp create mode 100644 src/apps/debuganalyzer/SubWindow.h create mode 100644 src/apps/debuganalyzer/SubWindowManager.cpp create mode 100644 src/apps/debuganalyzer/SubWindowManager.h diff --git a/src/apps/debuganalyzer/SubWindow.cpp b/src/apps/debuganalyzer/SubWindow.cpp new file mode 100644 index 0000000000..075ad73c38 --- /dev/null +++ b/src/apps/debuganalyzer/SubWindow.cpp @@ -0,0 +1,98 @@ +/* + * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ + +#include "SubWindow.h" + +#include + +#include "SubWindowManager.h" + + +// #pragma mark - SubWindowKey + + +SubWindowKey::~SubWindowKey() +{ +} + + +// #pragma mark - ObjectSubWindowKey + + +ObjectSubWindowKey::ObjectSubWindowKey(void* object) + : + fObject(object) +{ +} + + +bool +ObjectSubWindowKey::Equals(const SubWindowKey* other) const +{ + if (this == other) + return true; + + const ObjectSubWindowKey* key + = dynamic_cast(other); + return key != NULL && fObject == key->fObject; +} + + +size_t +ObjectSubWindowKey::HashCode() const +{ + return (size_t)(addr_t)fObject; +} + + +// #pragma mark - SubWindow + + +SubWindow::SubWindow(SubWindowManager* manager, BRect frame, const char* title, + window_type type, uint32 flags, uint32 workspace) + : + BWindow(frame, title, type, flags, workspace), + fSubWindowManager(manager), + fSubWindowKey(NULL) + +{ + fSubWindowManager->AddReference(); +} + + +SubWindow::~SubWindow() +{ + RemoveFromSubWindowManager(); + fSubWindowManager->RemoveReference(); +} + + +bool +SubWindow::AddToSubWindowManager(SubWindowKey* key) +{ + AutoLocker locker(fSubWindowManager); + + fSubWindowKey = key; + + if (!fSubWindowManager->AddSubWindow(this)) { + fSubWindowKey = NULL; + return false; + } + + return true; +} + + +bool +SubWindow::RemoveFromSubWindowManager() +{ + if (fSubWindowKey == NULL) + return false; + + fSubWindowManager->RemoveSubWindow(this); + delete fSubWindowKey; + fSubWindowKey = NULL; + return true; +} diff --git a/src/apps/debuganalyzer/SubWindow.h b/src/apps/debuganalyzer/SubWindow.h new file mode 100644 index 0000000000..50ed229890 --- /dev/null +++ b/src/apps/debuganalyzer/SubWindow.h @@ -0,0 +1,63 @@ +/* + * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ +#ifndef SUB_WINDOW_H +#define SUB_WINDOW_H + +#include + +#include + + +class SubWindowManager; + + +class SubWindowKey { +public: + virtual ~SubWindowKey(); + + virtual bool Equals(const SubWindowKey* other) const = 0; + virtual size_t HashCode() const = 0; +}; + + +class ObjectSubWindowKey : public SubWindowKey { +public: + ObjectSubWindowKey(void* object); + + virtual bool Equals(const SubWindowKey* other) const; + virtual size_t HashCode() const; + +private: + void* fObject; +}; + + +class SubWindow : public BWindow, public HashTableLink { +public: + SubWindow(SubWindowManager* manager, + BRect frame, const char* title, + window_type type, uint32 flags, + uint32 workspace = B_CURRENT_WORKSPACE); + virtual ~SubWindow(); + + inline SubWindowKey* GetSubWindowKey() const; + + bool AddToSubWindowManager(SubWindowKey* key); + bool RemoveFromSubWindowManager(); + +protected: + SubWindowManager* fSubWindowManager; + SubWindowKey* fSubWindowKey; +}; + + +SubWindowKey* +SubWindow::GetSubWindowKey() const +{ + return fSubWindowKey; +} + + +#endif // SUB_WINDOW_H diff --git a/src/apps/debuganalyzer/SubWindowManager.cpp b/src/apps/debuganalyzer/SubWindowManager.cpp new file mode 100644 index 0000000000..f11e086980 --- /dev/null +++ b/src/apps/debuganalyzer/SubWindowManager.cpp @@ -0,0 +1,81 @@ +/* + * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ + +#include "SubWindowManager.h" + +#include + +#include + +#include + + +SubWindowManager::SubWindowManager(BLooper* parent) + : + fParent(parent), + fSubWindows() +{ +} + + +SubWindowManager::~SubWindowManager() +{ +} + + +status_t +SubWindowManager::Init() +{ + return fSubWindows.Init(); +} + + +bool +SubWindowManager::AddSubWindow(SubWindow* window) +{ + AutoLocker locker(this); + + if (fSubWindows.Lookup(*window->GetSubWindowKey()) != NULL) + return false; + + fSubWindows.Insert(window); + + return true; +} + + +bool +SubWindowManager::RemoveSubWindow(SubWindow* window) +{ + AutoLocker locker(this); + + return fSubWindows.Remove(window); +} + + +SubWindow* +SubWindowManager::LookupSubWindow(const SubWindowKey& key) const +{ + return fSubWindows.Lookup(key); +} + + +void +SubWindowManager::Broadcast(uint32 messageCode) +{ + BMessage message(messageCode); + Broadcast(&message); +} + + +void +SubWindowManager::Broadcast(BMessage* message) +{ + AutoLocker locker(this); + + SubWindowTable::Iterator it = fSubWindows.GetIterator(); + while (SubWindow* window = it.Next()) + window->PostMessage(message); +} diff --git a/src/apps/debuganalyzer/SubWindowManager.h b/src/apps/debuganalyzer/SubWindowManager.h new file mode 100644 index 0000000000..39b26437ce --- /dev/null +++ b/src/apps/debuganalyzer/SubWindowManager.h @@ -0,0 +1,66 @@ +/* + * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ +#ifndef SUB_WINDOW_MANAGER_H +#define SUB_WINDOW_MANAGER_H + +#include + +#include + +#include + +#include "SubWindow.h" + + +class SubWindowManager : public Referenceable, public BLocker { +public: + SubWindowManager(BLooper* parent); + virtual ~SubWindowManager(); + + status_t Init(); + + bool AddSubWindow(SubWindow* window); + bool RemoveSubWindow(SubWindow* window); + SubWindow* LookupSubWindow(const SubWindowKey& key) const; + + void Broadcast(uint32 messageCode); + void Broadcast(BMessage* message); + +private: + struct HashDefinition { + typedef SubWindowKey KeyType; + typedef SubWindow ValueType; + + size_t HashKey(const SubWindowKey& key) const + { + return key.HashCode(); + } + + size_t Hash(const SubWindow* value) const + { + return value->GetSubWindowKey()->HashCode(); + } + + bool Compare(const SubWindowKey& key, + const SubWindow* value) const + { + return key.Equals(value->GetSubWindowKey()); + } + + HashTableLink* GetLink(SubWindow* value) const + { + return value; + } + }; + + typedef OpenHashTable SubWindowTable; + +private: + BLooper* fParent; + SubWindowTable fSubWindows; +}; + + +#endif // SUB_WINDOW_MANAGER_H