Cleanup. Added BTokenSpace::GetList(), though it's probably not needed after all

(we can still remove it later if needed).
Added B_SERVER_TOKEN type.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13451 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2005-07-05 17:22:35 +00:00
parent 2e6a5805ba
commit ef571f678c
2 changed files with 50 additions and 38 deletions

View File

@ -1,31 +1,32 @@
//------------------------------------------------------------------------------
// TokenSpace.h
//
//------------------------------------------------------------------------------
/*
* Copyright 2001-2005, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Erik Jaesler (erik@cgsoftware.com)
* Axel Dörfler, axeld@pinc-software.de
*/
#ifndef _TOKEN_SPACE_H
#define _TOKEN_SPACE_H
#ifndef TOKENSPACE_H
#define TOKENSPACE_H
// Standard Includes -----------------------------------------------------------
#include <map>
#include <stack>
// System Includes -------------------------------------------------------------
#include <BeBuild.h>
#include <Locker.h>
#include <SupportDefs.h>
// Project Includes ------------------------------------------------------------
// Local Includes --------------------------------------------------------------
// Local Defines ---------------------------------------------------------------
// token types as specified in targets
#define B_PREFERRED_TOKEN -2 /* A little bird told me about this one */
#define B_NULL_TOKEN -1
#define B_ANY_TOKEN 0
#define B_HANDLER_TOKEN 1
// Globals ---------------------------------------------------------------------
// token types in the token list
#define B_HANDLER_TOKEN 1
#define B_SERVER_TOKEN 2
namespace BPrivate {
@ -33,19 +34,20 @@ typedef void (*new_token_callback)(int16, void*);
typedef void (*remove_token_callback)(int16, void*);
typedef bool (*get_token_callback)(int16, void*);
class BTokenSpace
{
class BTokenSpace : public BLocker {
public:
BTokenSpace();
~BTokenSpace();
int32 NewToken(int16 type, void* object,
new_token_callback callback= NULL);
new_token_callback callback = NULL);
bool RemoveToken(int32 token, remove_token_callback callback = NULL);
bool CheckToken(int32, int16) const;
status_t GetToken(int32, int16, void**,
bool CheckToken(int32 token, int16 type) const;
status_t GetToken(int32 token, int16 type, void** object,
get_token_callback callback = NULL) const;
status_t GetList(int32*& tokens, int32& count) const;
// Possible expansion
// void Dump(BDataIO&, bool) const;
// int32 NewToken(void*, BDirectMessageTarget*, void (*)(short, void*));
@ -53,10 +55,9 @@ class BTokenSpace
// BDirectMessageTarget* TokenTarget(uint32 token, int16 type);
private:
struct TTokenInfo
{
int16 type;
void* object;
struct TTokenInfo {
int16 type;
void* object;
};
typedef std::map<int32, TTokenInfo> TTokenMap;
@ -64,7 +65,6 @@ class BTokenSpace
TTokenMap fTokenMap;
std::stack<int32> fTokenBin;
int32 fTokenCount;
BLocker fLocker;
};
// Possible expansion
@ -79,12 +79,4 @@ extern _IMPEXP_BE BTokenSpace gDefaultTokens;
} // namespace BPrivate
#endif //TOKENSPACE_H
/*
* $Log $
*
* $Id $
*
*/
#endif // _TOKEN_SPACE_H

View File

@ -4,6 +4,7 @@
*
* Authors:
* Erik Jaesler (erik@cgsoftware.com)
* Axel Dörfler, axeld@pinc-software.de
*/
@ -35,7 +36,7 @@ int32
BTokenSpace::NewToken(int16 type, void* object,
new_token_callback callback)
{
BAutolock Lock(fLocker);
BAutolock locker(this);
TTokenInfo tokenInfo = { type, object };
int32 token;
@ -60,7 +61,7 @@ BTokenSpace::NewToken(int16 type, void* object,
bool
BTokenSpace::RemoveToken(int32 token, remove_token_callback callback)
{
BAutolock Lock(fLocker);
BAutolock locker(this);
TTokenMap::iterator iter = fTokenMap.find(token);
if (iter == fTokenMap.end())
@ -83,7 +84,7 @@ BTokenSpace::RemoveToken(int32 token, remove_token_callback callback)
bool
BTokenSpace::CheckToken(int32 token, int16 type) const
{
BAutolock Locker(const_cast<BLocker&>(fLocker));
BAutolock locker(const_cast<BTokenSpace&>(*this));
TTokenMap::const_iterator iter = fTokenMap.find(token);
if (iter != fTokenMap.end() && iter->second.type == type)
@ -97,7 +98,7 @@ status_t
BTokenSpace::GetToken(int32 token, int16 type, void** object,
get_token_callback callback) const
{
BAutolock Locker(const_cast<BLocker&>(fLocker));
BAutolock locker(const_cast<BTokenSpace&>(*this));
TTokenMap::const_iterator iter = fTokenMap.find(token);
if (iter == fTokenMap.end() || iter->second.type != type) {
@ -115,5 +116,24 @@ BTokenSpace::GetToken(int32 token, int16 type, void** object,
return B_OK;
}
} // namespace BPrivate
status_t
BTokenSpace::GetList(int32*& tokens, int32& count) const
{
BAutolock locker(const_cast<BTokenSpace&>(*this));
count = fTokenMap.size();
tokens = (int32*)malloc(count * sizeof(int32));
if (tokens == NULL)
return B_NO_MEMORY;
TTokenMap::const_iterator iterator = fTokenMap.begin();
for (int32 i = 0; iterator != fTokenMap.end(); i++) {
tokens[i] = iterator->first;
iterator++;
}
return B_OK;
}
} // namespace BPrivate