1 /* 2 * Copyright 2001-2006, 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 <map> 12 #include <stack> 13 14 #include <Autolock.h> 15 16 #include "TokenSpace.h" 17 18 19 namespace BPrivate { 20 21 BTokenSpace gDefaultTokens; 22 // the default token space - all handlers will go into that one 23 24 25 BTokenSpace::BTokenSpace() 26 : 27 fTokenCount(1) 28 { 29 } 30 31 32 BTokenSpace::~BTokenSpace() 33 { 34 } 35 36 37 int32 38 BTokenSpace::NewToken(int16 type, void* object) 39 { 40 BAutolock locker(this); 41 42 token_info tokenInfo = { type, object }; 43 int32 token = fTokenCount++; 44 45 fTokenMap[token] = tokenInfo; 46 47 return token; 48 } 49 50 51 /*! 52 Inserts the specified token into the token space. If that token 53 already exists, it will be overwritten. 54 Don't mix NewToken() and this method unless you know what you're 55 doing. 56 */ 57 void 58 BTokenSpace::SetToken(int32 token, int16 type, void* object) 59 { 60 BAutolock locker(this); 61 62 token_info tokenInfo = { type, object }; 63 fTokenMap[token] = tokenInfo; 64 65 // this makes sure SetToken() plays more or less nice with NewToken() 66 if (token >= fTokenCount) 67 fTokenCount = token + 1; 68 } 69 70 71 bool 72 BTokenSpace::RemoveToken(int32 token) 73 { 74 BAutolock locker(this); 75 76 TokenMap::iterator iterator = fTokenMap.find(token); 77 if (iterator == fTokenMap.end()) 78 return false; 79 80 fTokenMap.erase(iterator); 81 return true; 82 } 83 84 85 /*! Checks wether or not the \a token exists with the specified 86 \a type in the token space or not. 87 */ 88 bool 89 BTokenSpace::CheckToken(int32 token, int16 type) const 90 { 91 BAutolock locker(const_cast<BTokenSpace&>(*this)); 92 93 TokenMap::const_iterator iterator = fTokenMap.find(token); 94 if (iterator != fTokenMap.end() && iterator->second.type == type) 95 return true; 96 97 return false; 98 } 99 100 101 status_t 102 BTokenSpace::GetToken(int32 token, int16 type, void** _object) const 103 { 104 BAutolock locker(const_cast<BTokenSpace&>(*this)); 105 106 if (token < 1) 107 return B_ENTRY_NOT_FOUND; 108 109 TokenMap::const_iterator iterator = fTokenMap.find(token); 110 111 if (iterator == fTokenMap.end() || iterator->second.type != type) 112 return B_ENTRY_NOT_FOUND; 113 114 *_object = iterator->second.object; 115 return B_OK; 116 } 117 118 } // namespace BPrivate 119