map_wrapper.h revision 4e51691e58d8d32590b03c1951cb13de4d1c4758
1/* 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_MAP_WRAPPER_H_ 12#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_MAP_WRAPPER_H_ 13 14#include <map> 15 16#include "constructor_magic.h" 17 18namespace webrtc { 19class MapItem 20{ 21friend class MapWrapper; 22 23public: 24 MapItem(int id, void* ptr); 25 virtual ~MapItem(); 26 void* GetItem(); 27 int GetId(); 28 unsigned int GetUnsignedId(); 29 void SetItem(void* ptr); 30 31private: 32 int item_id_; 33 void* item_pointer_; 34 DISALLOW_COPY_AND_ASSIGN(MapItem); 35}; 36 37class MapWrapper 38{ 39public: 40 MapWrapper(); 41 ~MapWrapper(); 42 43 // Puts a pointer to anything in the map and associates it with id. Note, id 44 // needs to be unique for all items in the map. 45 int Insert(int id, void* ptr); 46 47 // Removes item from map. 48 int Erase(MapItem* item); 49 50 // Finds item with associated with id and removes it from the map. 51 int Erase(int id); 52 53 // Returns the number of elements stored in the map. 54 int Size() const; 55 56 // Returns a pointer to the first MapItem in the map. 57 MapItem* First() const; 58 59 // Returns a pointer to the last MapItem in the map. 60 MapItem* Last() const; 61 62 // Returns a pointer to the MapItem stored after item in the map. 63 MapItem* Next(MapItem* item) const; 64 65 // Returns a pointer to the MapItem stored before item in the map. 66 MapItem* Previous(MapItem* item) const; 67 68 // Returns a pointer to the MapItem associated with id from the map. 69 MapItem* Find(int id) const; 70 71private: 72 std::map<int, MapItem*> map_; 73 DISALLOW_COPY_AND_ASSIGN(MapWrapper); 74}; 75} // namespace webrtc 76 77#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_MAP_WRAPPER_H_ 78