xref: /haiku/src/kits/app/TokenSpace.cpp (revision d5cd5d63ff0ad395989db6cf4841a64d5b545d1d)
1 //------------------------------------------------------------------------------
2 //	TokenSpace.cpp
3 //
4 //------------------------------------------------------------------------------
5 
6 // Standard Includes -----------------------------------------------------------
7 #include <map>
8 #include <stack>
9 
10 // System Includes -------------------------------------------------------------
11 #include <Autolock.h>
12 
13 // Project Includes ------------------------------------------------------------
14 
15 // Local Includes --------------------------------------------------------------
16 #include "TokenSpace.h"
17 
18 // Local Defines ---------------------------------------------------------------
19 
20 // Globals ---------------------------------------------------------------------
21 
22 namespace BPrivate {
23 
24 BTokenSpace gDefaultTokens;
25 
26 
27 //------------------------------------------------------------------------------
28 BTokenSpace::BTokenSpace()
29 {
30 }
31 //------------------------------------------------------------------------------
32 BTokenSpace::~BTokenSpace()
33 {
34 }
35 //------------------------------------------------------------------------------
36 int32 BTokenSpace::NewToken(int16 type, void* object,
37 							new_token_callback callback)
38 {
39 	BAutolock Lock(fLocker);
40 	TTokenInfo ti = { type, object };
41 	int32 token;
42 	if (fTokenBin.empty())
43 	{
44 		token = fTokenCount;
45 		++fTokenCount;
46 	}
47 	else
48 	{
49 		token = fTokenBin.top();
50 		fTokenBin.pop();
51 	}
52 
53 	fTokenMap[token] = ti;
54 
55 	if (callback)
56 	{
57 		callback(type, object);
58 	}
59 
60 	return token;
61 }
62 //------------------------------------------------------------------------------
63 bool BTokenSpace::RemoveToken(int32 token, remove_token_callback callback)
64 {
65 	BAutolock Lock(fLocker);
66 	TTokenMap::iterator iter = fTokenMap.find(token);
67 	if (iter == fTokenMap.end())
68 	{
69 		return false;
70 	}
71 
72 	if (callback)
73 	{
74 		callback(iter->second.type, iter->second.object);
75 	}
76 
77 	fTokenMap.erase(iter);
78 	fTokenBin.push(token);
79 
80 	return true;
81 }
82 //------------------------------------------------------------------------------
83 bool BTokenSpace::CheckToken(int32 token, int16 type) const
84 {
85 	BAutolock Locker(const_cast<BLocker&>(fLocker));
86 	TTokenMap::const_iterator iter = fTokenMap.find(token);
87 	if (iter != fTokenMap.end() && iter->second.type == type)
88 	{
89 		return true;
90 	}
91 
92 	return false;
93 }
94 //------------------------------------------------------------------------------
95 status_t BTokenSpace::GetToken(int32 token, int16 type, void** object,
96 							   get_token_callback callback) const
97 {
98 	BAutolock Locker(const_cast<BLocker&>(fLocker));
99 	TTokenMap::const_iterator iter = fTokenMap.find(token);
100 	if (iter == fTokenMap.end())
101 	{
102 		*object = NULL;
103 		return B_ERROR;
104 	}
105 
106 	if (callback && !callback(iter->second.type, iter->second.object))
107 	{
108 		*object = NULL;
109 		return B_ERROR;
110 	}
111 
112 	*object = iter->second.object;
113 
114 	return B_OK;
115 }
116 //------------------------------------------------------------------------------
117 
118 }	// namespace BPrivate
119 
120 /*
121  * $Log $
122  *
123  * $Id  $
124  *
125  */
126 
127