blob: aad6f5c2271d3b9ae1f0d1fa81b4d2e6bb3f156b [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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.org6e0ce732012-12-18 17:18:35 +000011#include "webrtc/system_wrappers/interface/map_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000013#include "webrtc/system_wrappers/interface/trace.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000014
15namespace webrtc {
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000016
17MapItem::MapItem(int id, void* item)
18 : item_id_(id),
19 item_pointer_(item) {
niklase@google.com470e71d2011-07-07 08:21:25 +000020}
21
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000022MapItem::~MapItem() {
niklase@google.com470e71d2011-07-07 08:21:25 +000023}
24
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000025void* MapItem::GetItem() {
26 return item_pointer_;
niklase@google.com470e71d2011-07-07 08:21:25 +000027}
28
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000029int MapItem::GetId() {
30 return item_id_;
niklase@google.com470e71d2011-07-07 08:21:25 +000031}
32
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000033unsigned int MapItem::GetUnsignedId() {
34 return static_cast<unsigned int>(item_id_);
niklase@google.com470e71d2011-07-07 08:21:25 +000035}
36
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000037void MapItem::SetItem(void* ptr) {
38 item_pointer_ = ptr;
niklase@google.com470e71d2011-07-07 08:21:25 +000039}
40
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000041MapWrapper::MapWrapper() : map_() {
niklase@google.com470e71d2011-07-07 08:21:25 +000042}
43
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000044MapWrapper::~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.com470e71d2011-07-07 08:21:25 +000055}
56
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000057int MapWrapper::Size() const {
58 return (int)map_.size();
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000061int MapWrapper::Insert(int id, void* ptr) {
62 map_[id] = new MapItem(id, ptr);
63 return 0;
64}
65
66MapItem* 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
74MapItem* 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
82MapItem* MapWrapper::Next(MapItem* item) const {
83 if (item == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000084 return 0;
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000085 }
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.com470e71d2011-07-07 08:21:25 +000094}
95
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000096MapItem* MapWrapper::Previous(MapItem* item) const {
97 if (item == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000098 return 0;
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000099 }
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.com470e71d2011-07-07 08:21:25 +0000108}
109
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +0000110MapItem* 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.com470e71d2011-07-07 08:21:25 +0000116}
117
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +0000118int MapWrapper::Erase(MapItem* item) {
119 if (item == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000120 return -1;
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +0000121 }
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.com470e71d2011-07-07 08:21:25 +0000129}
130
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +0000131int 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.com470e71d2011-07-07 08:21:25 +0000139}
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +0000140
niklase@google.com470e71d2011-07-07 08:21:25 +0000141} // namespace webrtc