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 #include "map_wrapper.h" 12 13 #include "trace.h" 14 15 namespace webrtc { 16 MapItem::MapItem(int id, void* item) : item_id_(id), item_pointer_(item) 17 { 18 } 19 20 MapItem::~MapItem() 21 { 22 } 23 24 void* MapItem::GetItem() 25 { 26 return item_pointer_; 27 } 28 29 int MapItem::GetId() 30 { 31 return item_id_; 32 } 33 34 unsigned int MapItem::GetUnsignedId() 35 { 36 return static_cast<unsigned int>(item_id_); 37 } 38 39 void MapItem::SetItem(void* ptr) 40 { 41 item_pointer_ = ptr; 42 } 43 44 MapWrapper::MapWrapper() : map_() 45 { 46 } 47 48 MapWrapper::~MapWrapper() 49 { 50 if (!map_.empty()) 51 { 52 WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1, 53 "Potential memory leak in MapWrapper"); 54 // Remove all map items. Please note that std::map::clear() can't be 55 // used because each item has some dynamically allocated memory 56 // associated with it (i.e. using std::map::clear would introduce a 57 // memory leak). 58 while (Erase(First()) == 0) 59 {} 60 } 61 } 62 63 int MapWrapper::Size() const 64 { 65 return (int)map_.size(); 66 } 67 68 int MapWrapper::Insert(int id, void* ptr) 69 { 70 map_[id] = new MapItem(id,ptr); 71 return 0; 72 } 73 74 MapItem* MapWrapper::First() const 75 { 76 std::map<int, MapItem*>::const_iterator it = map_.begin(); 77 if (it != map_.end()) 78 { 79 return it->second; 80 } 81 return 0; 82 } 83 84 MapItem* MapWrapper::Last() const 85 { 86 std::map<int, MapItem*>::const_reverse_iterator it = map_.rbegin(); 87 if (it != map_.rend()) 88 { 89 return it->second; 90 } 91 return 0; 92 } 93 94 MapItem* MapWrapper::Next(MapItem* item) const 95 { 96 if (item == 0) 97 { 98 return 0; 99 } 100 std::map<int, MapItem*>::const_iterator it = map_.find(item->item_id_); 101 if (it != map_.end()) 102 { 103 it++; 104 if (it != map_.end()) 105 { 106 return it->second; 107 } 108 } 109 return 0; 110 } 111 112 MapItem* MapWrapper::Previous(MapItem* item) const 113 { 114 if (item == 0) 115 { 116 return 0; 117 } 118 119 std::map<int, MapItem*>::const_iterator it = map_.find(item->item_id_); 120 if ((it != map_.end()) && 121 (it != map_.begin())) 122 { 123 --it; 124 return it->second; 125 } 126 return 0; 127 } 128 129 MapItem* MapWrapper::Find(int id) const 130 { 131 std::map<int, MapItem*>::const_iterator it = map_.find(id); 132 if (it != map_.end()) 133 { 134 return it->second; 135 } 136 return 0; 137 } 138 139 int MapWrapper::Erase(MapItem* item) 140 { 141 if (item == 0) 142 { 143 return -1; 144 } 145 std::map<int, MapItem*>::iterator it = map_.find(item->item_id_); 146 if (it != map_.end()) 147 { 148 delete it->second; 149 map_.erase(it); 150 return 0; 151 } 152 return -1; 153 } 154 155 int MapWrapper::Erase(const int id) 156 { 157 std::map<int, MapItem*>::iterator it = map_.find(id); 158 if (it != map_.end()) 159 { 160 delete it->second; 161 map_.erase(it); 162 return 0; 163 } 164 return -1; 165 } 166 } // namespace webrtc 167