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