2 * Copyright 2001-2011, Haiku.
3 * Distributed under the terms of the MIT License.
6 * Erik Jaesler (erik@cgsoftware.com)
7 * Axel Dörfler, axeld@pinc-software.de
11 #include <DirectMessageTarget.h>
12 #include <TokenSpace.h>
19 BTokenSpace gDefaultTokens
;
20 // the default token space - all handlers will go into that one
23 BTokenSpace::BTokenSpace()
25 BLocker("token space"),
31 BTokenSpace::~BTokenSpace()
37 BTokenSpace::NewToken(int16 type
, void* object
)
39 BAutolock
locker(this);
41 token_info tokenInfo
= { type
, object
, NULL
};
42 int32 token
= fTokenCount
;
45 fTokenMap
[token
] = tokenInfo
;
46 } catch (std::bad_alloc
& exception
) {
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
63 BTokenSpace::SetToken(int32 token
, int16 type
, void* object
)
65 BAutolock
locker(this);
67 token_info tokenInfo
= { type
, object
, NULL
};
70 fTokenMap
[token
] = tokenInfo
;
71 } catch (std::bad_alloc
& exception
) {
75 // this makes sure SetToken() plays more or less nice with NewToken()
76 if (token
>= fTokenCount
)
77 fTokenCount
= token
+ 1;
84 BTokenSpace::RemoveToken(int32 token
)
86 BAutolock
locker(this);
88 TokenMap::iterator iterator
= fTokenMap
.find(token
);
89 if (iterator
== fTokenMap
.end())
92 fTokenMap
.erase(iterator
);
97 /*! Checks whether or not the \a token exists with the specified
98 \a type in the token space or not.
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
)
114 BTokenSpace::GetToken(int32 token
, int16 type
, void** _object
) const
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
;
131 BTokenSpace::SetHandlerTarget(int32 token
, BDirectMessageTarget
* target
)
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
;
154 BTokenSpace::AcquireHandlerTarget(int32 token
, BDirectMessageTarget
** _target
)
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
;
174 BTokenSpace::InitAfterFork()
176 // We need to reinitialize the locker to get a new semaphore
177 new (this) BTokenSpace();
181 } // namespace BPrivate