1 /* 2 * Copyright 2001-2007, Haiku. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Erik Jaesler (erik@cgsoftware.com) 7 * Axel Dörfler, axeld@pinc-software.de 8 */ 9 10 11 #include <DirectMessageTarget.h> 12 #include <TokenSpace.h> 13 14 #include <Autolock.h> 15 16 17 namespace BPrivate { 18 19 BTokenSpace gDefaultTokens; 20 // the default token space - all handlers will go into that one 21 22 23 BTokenSpace::BTokenSpace() 24 : 25 fTokenCount(1) 26 { 27 } 28 29 30 BTokenSpace::~BTokenSpace() 31 { 32 } 33 34 35 int32 36 BTokenSpace::NewToken(int16 type, void* object) 37 { 38 BAutolock locker(this); 39 40 token_info tokenInfo = { type, object, NULL }; 41 int32 token = fTokenCount++; 42 43 fTokenMap[token] = tokenInfo; 44 45 return token; 46 } 47 48 49 /*! 50 Inserts the specified token into the token space. If that token 51 already exists, it will be overwritten. 52 Don't mix NewToken() and this method unless you know what you're 53 doing. 54 */ 55 void 56 BTokenSpace::SetToken(int32 token, int16 type, void* object) 57 { 58 BAutolock locker(this); 59 60 token_info tokenInfo = { type, object, NULL }; 61 fTokenMap[token] = tokenInfo; 62 63 // this makes sure SetToken() plays more or less nice with NewToken() 64 if (token >= fTokenCount) 65 fTokenCount = token + 1; 66 } 67 68 69 bool 70 BTokenSpace::RemoveToken(int32 token) 71 { 72 BAutolock locker(this); 73 74 TokenMap::iterator iterator = fTokenMap.find(token); 75 if (iterator == fTokenMap.end()) 76 return false; 77 78 fTokenMap.erase(iterator); 79 return true; 80 } 81 82 83 /*! Checks wether or not the \a token exists with the specified 84 \a type in the token space or not. 85 */ 86 bool 87 BTokenSpace::CheckToken(int32 token, int16 type) const 88 { 89 BAutolock locker(const_cast<BTokenSpace&>(*this)); 90 91 TokenMap::const_iterator iterator = fTokenMap.find(token); 92 if (iterator != fTokenMap.end() && iterator->second.type == type) 93 return true; 94 95 return false; 96 } 97 98 99 status_t 100 BTokenSpace::GetToken(int32 token, int16 type, void** _object) const 101 { 102 if (token < 1) 103 return B_ENTRY_NOT_FOUND; 104 105 BAutolock locker(const_cast<BTokenSpace&>(*this)); 106 107 TokenMap::const_iterator iterator = fTokenMap.find(token); 108 if (iterator == fTokenMap.end() || iterator->second.type != type) 109 return B_ENTRY_NOT_FOUND; 110 111 *_object = iterator->second.object; 112 return B_OK; 113 } 114 115 116 status_t 117 BTokenSpace::SetHandlerTarget(int32 token, BDirectMessageTarget* target) 118 { 119 if (token < 1) 120 return B_ENTRY_NOT_FOUND; 121 122 BAutolock locker(const_cast<BTokenSpace&>(*this)); 123 124 TokenMap::iterator iterator = fTokenMap.find(token); 125 if (iterator == fTokenMap.end() || iterator->second.type != B_HANDLER_TOKEN) 126 return B_ENTRY_NOT_FOUND; 127 128 if (iterator->second.target != NULL) 129 iterator->second.target->Release(); 130 131 iterator->second.target = target; 132 if (target != NULL) 133 target->Acquire(); 134 135 return B_OK; 136 } 137 138 139 status_t 140 BTokenSpace::AcquireHandlerTarget(int32 token, BDirectMessageTarget** _target) 141 { 142 if (token < 1) 143 return B_ENTRY_NOT_FOUND; 144 145 BAutolock locker(const_cast<BTokenSpace&>(*this)); 146 147 TokenMap::const_iterator iterator = fTokenMap.find(token); 148 if (iterator == fTokenMap.end() || iterator->second.type != B_HANDLER_TOKEN) 149 return B_ENTRY_NOT_FOUND; 150 151 if (iterator->second.target != NULL) 152 iterator->second.target->Acquire(); 153 154 *_target = iterator->second.target; 155 return B_OK; 156 } 157 158 } // namespace BPrivate 159