xref: /haiku/src/kits/app/TokenSpace.cpp (revision 81f5654c124bf46fba0fd251f208e2d88d81e1ce)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		TokenSpace.cpp
23 //	Author(s):		Erik Jaesler <erik@cgsoftware.com>
24 //	Description:	Class for creating tokens
25 //------------------------------------------------------------------------------
26 
27 // Standard Includes -----------------------------------------------------------
28 #include <map>
29 #include <stack>
30 
31 // System Includes -------------------------------------------------------------
32 #include <Autolock.h>
33 
34 // Project Includes ------------------------------------------------------------
35 
36 // Local Includes --------------------------------------------------------------
37 #include "TokenSpace.h"
38 
39 // Local Defines ---------------------------------------------------------------
40 
41 // Globals ---------------------------------------------------------------------
42 
43 namespace BPrivate {
44 
45 BTokenSpace gDefaultTokens;
46 
47 
48 //------------------------------------------------------------------------------
49 BTokenSpace::BTokenSpace()
50 {
51 }
52 //------------------------------------------------------------------------------
53 BTokenSpace::~BTokenSpace()
54 {
55 }
56 //------------------------------------------------------------------------------
57 int32 BTokenSpace::NewToken(int16 type, void* object,
58 							new_token_callback callback)
59 {
60 	BAutolock Lock(fLocker);
61 	TTokenInfo ti = { type, object };
62 	int32 token;
63 	if (fTokenBin.empty())
64 	{
65 		token = fTokenCount;
66 		++fTokenCount;
67 	}
68 	else
69 	{
70 		token = fTokenBin.top();
71 		fTokenBin.pop();
72 	}
73 
74 	fTokenMap[token] = ti;
75 
76 	if (callback)
77 	{
78 		callback(type, object);
79 	}
80 
81 	return token;
82 }
83 //------------------------------------------------------------------------------
84 bool BTokenSpace::RemoveToken(int32 token, remove_token_callback callback)
85 {
86 	BAutolock Lock(fLocker);
87 	TTokenMap::iterator iter = fTokenMap.find(token);
88 	if (iter == fTokenMap.end())
89 	{
90 		return false;
91 	}
92 
93 	if (callback)
94 	{
95 		callback(iter->second.type, iter->second.object);
96 	}
97 
98 	fTokenMap.erase(iter);
99 	fTokenBin.push(token);
100 
101 	return true;
102 }
103 //------------------------------------------------------------------------------
104 bool BTokenSpace::CheckToken(int32 token, int16 type) const
105 {
106 	BAutolock Locker(const_cast<BLocker&>(fLocker));
107 	TTokenMap::const_iterator iter = fTokenMap.find(token);
108 	if (iter != fTokenMap.end() && iter->second.type == type)
109 	{
110 		return true;
111 	}
112 
113 	return false;
114 }
115 //------------------------------------------------------------------------------
116 status_t BTokenSpace::GetToken(int32 token, int16 type, void** object,
117 							   get_token_callback callback) const
118 {
119 	BAutolock Locker(const_cast<BLocker&>(fLocker));
120 	TTokenMap::const_iterator iter = fTokenMap.find(token);
121 	if (iter == fTokenMap.end())
122 	{
123 		*object = NULL;
124 		return B_ERROR;
125 	}
126 
127 	if (callback && !callback(iter->second.type, iter->second.object))
128 	{
129 		*object = NULL;
130 		return B_ERROR;
131 	}
132 
133 	*object = iter->second.object;
134 
135 	return B_OK;
136 }
137 //------------------------------------------------------------------------------
138 
139 }	// namespace BPrivate
140 
141 /*
142  * $Log $
143  *
144  * $Id  $
145  *
146  */
147 
148