BPicture: Fix archive constructor.
[haiku.git] / src / kits / app / TokenSpace.cpp
blob60c8ba2aa06031a1ca12674119e8aa823b719081
1 /*
2 * Copyright 2001-2011, Haiku.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Erik Jaesler (erik@cgsoftware.com)
7 * Axel Dörfler, axeld@pinc-software.de
8 */
11 #include <DirectMessageTarget.h>
12 #include <TokenSpace.h>
14 #include <Autolock.h>
17 namespace BPrivate {
19 BTokenSpace gDefaultTokens;
20 // the default token space - all handlers will go into that one
23 BTokenSpace::BTokenSpace()
25 BLocker("token space"),
26 fTokenCount(1)
31 BTokenSpace::~BTokenSpace()
36 int32
37 BTokenSpace::NewToken(int16 type, void* object)
39 BAutolock locker(this);
41 token_info tokenInfo = { type, object, NULL };
42 int32 token = fTokenCount;
44 try {
45 fTokenMap[token] = tokenInfo;
46 } catch (std::bad_alloc& exception) {
47 return -1;
50 fTokenCount++;
52 return token;
56 /*!
57 Inserts the specified token into the token space. If that token
58 already exists, it will be overwritten.
59 Don't mix NewToken() and this method unless you know what you're
60 doing.
62 bool
63 BTokenSpace::SetToken(int32 token, int16 type, void* object)
65 BAutolock locker(this);
67 token_info tokenInfo = { type, object, NULL };
69 try {
70 fTokenMap[token] = tokenInfo;
71 } catch (std::bad_alloc& exception) {
72 return false;
75 // this makes sure SetToken() plays more or less nice with NewToken()
76 if (token >= fTokenCount)
77 fTokenCount = token + 1;
79 return true;
83 bool
84 BTokenSpace::RemoveToken(int32 token)
86 BAutolock locker(this);
88 TokenMap::iterator iterator = fTokenMap.find(token);
89 if (iterator == fTokenMap.end())
90 return false;
92 fTokenMap.erase(iterator);
93 return true;
97 /*! Checks whether or not the \a token exists with the specified
98 \a type in the token space or not.
100 bool
101 BTokenSpace::CheckToken(int32 token, int16 type) const
103 BAutolock locker(const_cast<BTokenSpace&>(*this));
105 TokenMap::const_iterator iterator = fTokenMap.find(token);
106 if (iterator != fTokenMap.end() && iterator->second.type == type)
107 return true;
109 return false;
113 status_t
114 BTokenSpace::GetToken(int32 token, int16 type, void** _object) const
116 if (token < 1)
117 return B_ENTRY_NOT_FOUND;
119 BAutolock locker(const_cast<BTokenSpace&>(*this));
121 TokenMap::const_iterator iterator = fTokenMap.find(token);
122 if (iterator == fTokenMap.end() || iterator->second.type != type)
123 return B_ENTRY_NOT_FOUND;
125 *_object = iterator->second.object;
126 return B_OK;
130 status_t
131 BTokenSpace::SetHandlerTarget(int32 token, BDirectMessageTarget* target)
133 if (token < 1)
134 return B_ENTRY_NOT_FOUND;
136 BAutolock locker(const_cast<BTokenSpace&>(*this));
138 TokenMap::iterator iterator = fTokenMap.find(token);
139 if (iterator == fTokenMap.end() || iterator->second.type != B_HANDLER_TOKEN)
140 return B_ENTRY_NOT_FOUND;
142 if (iterator->second.target != NULL)
143 iterator->second.target->Release();
145 iterator->second.target = target;
146 if (target != NULL)
147 target->Acquire();
149 return B_OK;
153 status_t
154 BTokenSpace::AcquireHandlerTarget(int32 token, BDirectMessageTarget** _target)
156 if (token < 1)
157 return B_ENTRY_NOT_FOUND;
159 BAutolock locker(const_cast<BTokenSpace&>(*this));
161 TokenMap::const_iterator iterator = fTokenMap.find(token);
162 if (iterator == fTokenMap.end() || iterator->second.type != B_HANDLER_TOKEN)
163 return B_ENTRY_NOT_FOUND;
165 if (iterator->second.target != NULL)
166 iterator->second.target->Acquire();
168 *_target = iterator->second.target;
169 return B_OK;
173 void
174 BTokenSpace::InitAfterFork()
176 // We need to reinitialize the locker to get a new semaphore
177 new (this) BTokenSpace();
181 } // namespace BPrivate