Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/webrtc/system_wrappers/source/list_stl.cc @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

File size: 4.7 KB
Line 
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 "webrtc/system_wrappers/interface/list_wrapper.h"
12
13#include "webrtc/system_wrappers/interface/trace.h"
14
15namespace webrtc {
16
17ListItem::ListItem(const void* item)
18    : this_iter_(),
19      item_ptr_(item),
20      item_(0) {
21}
22
23ListItem::ListItem(const unsigned int item)
24    : this_iter_(),
25      item_ptr_(0),
26      item_(item) {
27}
28
29ListItem::~ListItem() {
30}
31
32void* ListItem::GetItem() const {
33  return const_cast<void*>(item_ptr_);
34}
35
36unsigned int ListItem::GetUnsignedItem() const {
37  return item_;
38}
39
40ListWrapper::ListWrapper()
41    : list_() {
42}
43
44ListWrapper::~ListWrapper() {
45  if (!Empty()) {
46    // TODO(hellner) I'm not sure this loggin is useful.
47    WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1,
48                 "Potential memory leak in ListWrapper");
49    // Remove all remaining list items.
50    while (Erase(First()) == 0) {}
51  }
52}
53
54bool ListWrapper::Empty() const {
55  return list_.empty();
56}
57
58unsigned int ListWrapper::GetSize() const {
59  return list_.size();
60}
61
62int ListWrapper::PushBack(const void* ptr) {
63  ListItem* item = new ListItem(ptr);
64  list_.push_back(item);
65  return 0;
66}
67
68int ListWrapper::PushBack(const unsigned int item_id) {
69  ListItem* item = new ListItem(item_id);
70  list_.push_back(item);
71  return 0;
72}
73
74int ListWrapper::PushFront(const unsigned int item_id) {
75  ListItem* item = new ListItem(item_id);
76  list_.push_front(item);
77  return 0;
78}
79
80int ListWrapper::PushFront(const void* ptr) {
81  ListItem* item = new ListItem(ptr);
82  list_.push_front(item);
83  return 0;
84}
85
86int ListWrapper::PopFront() {
87  if (list_.empty()) {
88    return -1;
89  }
90  list_.pop_front();
91  return 0;
92}
93
94int ListWrapper::PopBack() {
95  if (list_.empty()) {
96    return -1;
97  }
98  list_.pop_back();
99  return 0;
100}
101
102ListItem* ListWrapper::First() const {
103  if (list_.empty()) {
104    return NULL;
105  }
106  std::list<ListItem*>::iterator item_iter = list_.begin();
107  ListItem* return_item = (*item_iter);
108  return_item->this_iter_ = item_iter;
109  return return_item;
110}
111
112ListItem* ListWrapper::Last() const {
113  if (list_.empty()) {
114    return NULL;
115  }
116  // std::list::end() addresses the last item + 1. Decrement so that the
117  // actual last is accessed.
118  std::list<ListItem*>::iterator item_iter = list_.end();
119  --item_iter;
120  ListItem* return_item = (*item_iter);
121  return_item->this_iter_ = item_iter;
122  return return_item;
123}
124
125ListItem* ListWrapper::Next(ListItem* item) const {
126  if (item == NULL) {
127    return NULL;
128  }
129  std::list<ListItem*>::iterator item_iter = item->this_iter_;
130  ++item_iter;
131  if (item_iter == list_.end()) {
132    return NULL;
133  }
134  ListItem* return_item = (*item_iter);
135  return_item->this_iter_ = item_iter;
136  return return_item;
137}
138
139ListItem* ListWrapper::Previous(ListItem* item) const {
140  if (item == NULL) {
141    return NULL;
142  }
143  std::list<ListItem*>::iterator item_iter = item->this_iter_;
144  if (item_iter == list_.begin()) {
145    return NULL;
146  }
147  --item_iter;
148  ListItem* return_item = (*item_iter);
149  return_item->this_iter_ = item_iter;
150  return return_item;
151}
152
153int ListWrapper::Insert(ListItem* existing_previous_item,
154                        ListItem* new_item) {
155  // Allow existing_previous_item to be NULL if the list is empty.
156  // TODO(hellner) why allow this? Keep it as is for now to avoid
157  // breaking API contract.
158  if (!existing_previous_item && !Empty()) {
159    return -1;
160  }
161
162  if (!new_item) {
163    return -1;
164  }
165
166  std::list<ListItem*>::iterator insert_location = list_.begin();
167  if (!Empty()) {
168    insert_location = existing_previous_item->this_iter_;
169    if (insert_location != list_.end()) {
170      ++insert_location;
171    }
172  }
173
174  list_.insert(insert_location, new_item);
175  return 0;
176}
177
178int ListWrapper::InsertBefore(ListItem* existing_next_item,
179                              ListItem* new_item) {
180  // Allow existing_next_item to be NULL if the list is empty.
181  // Todo: why allow this? Keep it as is for now to avoid breaking API
182  // contract.
183  if (!existing_next_item && !Empty()) {
184    return -1;
185  }
186  if (!new_item) {
187    return -1;
188  }
189
190  std::list<ListItem*>::iterator insert_location = list_.begin();
191  if (!Empty()) {
192    insert_location = existing_next_item->this_iter_;
193  }
194
195  list_.insert(insert_location, new_item);
196  return 0;
197}
198
199int ListWrapper::Erase(ListItem* item) {
200  if (item == NULL) {
201    return -1;
202  }
203  list_.erase(item->this_iter_);
204  return 0;
205}
206
207}  // namespace webrtc
Note: See TracBrowser for help on using the repository browser.