Reformatted list classes.

BUG=
TEST=Trybots

Review URL: https://webrtc-codereview.appspot.com/995004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3291 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
phoglund@webrtc.org 2012-12-14 09:52:34 +00:00
parent 32519398b6
commit 52d981f60c
6 changed files with 822 additions and 913 deletions

View File

@ -11,13 +11,13 @@
#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_LIST_WRAPPER_H_
#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_LIST_WRAPPER_H_
#include "constructor_magic.h"
#include "webrtc/system_wrappers/interface/constructor_magic.h"
namespace webrtc {
class CriticalSectionWrapper;
class ListItem
{
class ListItem {
friend class ListWrapper;
public:
@ -36,8 +36,7 @@ private:
const unsigned int item_;
};
class ListWrapper
{
class ListWrapper {
public:
ListWrapper();
virtual ~ListWrapper();
@ -102,6 +101,7 @@ private:
ListItem* last_;
unsigned int size_;
};
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_LIST_WRAPPER_H_

View File

@ -8,39 +8,35 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "list_wrapper.h"
#include "webrtc/system_wrappers/interface/list_wrapper.h"
#include "critical_section_wrapper.h"
#include "trace.h"
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
#include "webrtc/system_wrappers/interface/trace.h"
namespace webrtc {
ListItem::ListItem(const void* item)
: next_(0),
prev_(0),
item_ptr_(item),
item_(0)
{
item_(0) {
}
ListItem::ListItem(const unsigned int item)
: next_(0),
prev_(0),
item_ptr_(0),
item_(item)
{
item_(item) {
}
ListItem::~ListItem()
{
ListItem::~ListItem() {
}
void* ListItem::GetItem() const
{
void* ListItem::GetItem() const {
return const_cast<void*>(item_ptr_);
}
unsigned int ListItem::GetUnsignedItem() const
{
unsigned int ListItem::GetUnsignedItem() const {
return item_;
}
@ -48,14 +44,11 @@ ListWrapper::ListWrapper()
: critical_section_(CriticalSectionWrapper::CreateCriticalSection()),
first_(0),
last_(0),
size_(0)
{
size_(0) {
}
ListWrapper::~ListWrapper()
{
if (!Empty())
{
ListWrapper::~ListWrapper() {
if (!Empty()) {
// TODO(hellner) I'm not sure this loggin is useful.
WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1,
"Potential memory leak in ListWrapper");
@ -66,102 +59,84 @@ ListWrapper::~ListWrapper()
delete critical_section_;
}
bool ListWrapper::Empty() const
{
bool ListWrapper::Empty() const {
return !first_ && !last_;
}
unsigned int ListWrapper::GetSize() const
{
unsigned int ListWrapper::GetSize() const {
return size_;
}
int ListWrapper::PushBack(const void* ptr)
{
int ListWrapper::PushBack(const void* ptr) {
ListItem* item = new ListItem(ptr);
CriticalSectionScoped lock(critical_section_);
PushBackImpl(item);
return 0;
}
int ListWrapper::PushBack(const unsigned int item_id)
{
int ListWrapper::PushBack(const unsigned int item_id) {
ListItem* item = new ListItem(item_id);
CriticalSectionScoped lock(critical_section_);
PushBackImpl(item);
return 0;
}
int ListWrapper::PushFront(const unsigned int item_id)
{
int ListWrapper::PushFront(const unsigned int item_id) {
ListItem* item = new ListItem(item_id);
CriticalSectionScoped lock(critical_section_);
PushFrontImpl(item);
return 0;
}
int ListWrapper::PushFront(const void* ptr)
{
int ListWrapper::PushFront(const void* ptr) {
ListItem* item = new ListItem(ptr);
CriticalSectionScoped lock(critical_section_);
PushFrontImpl(item);
return 0;
}
int ListWrapper::PopFront()
{
int ListWrapper::PopFront() {
return Erase(first_);
}
int ListWrapper::PopBack()
{
int ListWrapper::PopBack() {
return Erase(last_);
}
ListItem* ListWrapper::First() const
{
ListItem* ListWrapper::First() const {
return first_;
}
ListItem* ListWrapper::Last() const
{
ListItem* ListWrapper::Last() const {
return last_;
}
ListItem* ListWrapper::Next(ListItem* item) const
{
if(!item)
{
ListItem* ListWrapper::Next(ListItem* item) const {
if (!item) {
return 0;
}
return item->next_;
}
ListItem* ListWrapper::Previous(ListItem* item) const
{
if (!item)
{
ListItem* ListWrapper::Previous(ListItem* item) const {
if (!item) {
return 0;
}
return item->prev_;
}
int ListWrapper::Insert(ListItem* existing_previous_item, ListItem* new_item)
{
if (!new_item)
{
int ListWrapper::Insert(ListItem* existing_previous_item, ListItem* new_item) {
if (!new_item) {
return -1;
}
// Allow existing_previous_item to be NULL if the list is empty.
// TODO(hellner) why allow this? Keep it as is for now to avoid
// breaking API contract.
if (!existing_previous_item && !Empty())
{
if (!existing_previous_item && !Empty()) {
return -1;
}
CriticalSectionScoped lock(critical_section_);
if (!existing_previous_item)
{
if (!existing_previous_item) {
PushBackImpl(new_item);
return 0;
}
@ -169,12 +144,9 @@ int ListWrapper::Insert(ListItem* existing_previous_item, ListItem* new_item)
new_item->next_ = existing_previous_item->next_;
new_item->prev_ = existing_previous_item;
existing_previous_item->next_ = new_item;
if (next_item)
{
if (next_item) {
next_item->prev_ = new_item;
}
else
{
} else {
last_ = new_item;
}
size_++;
@ -182,22 +154,18 @@ int ListWrapper::Insert(ListItem* existing_previous_item, ListItem* new_item)
}
int ListWrapper::InsertBefore(ListItem* existing_next_item,
ListItem* new_item)
{
if (!new_item)
{
ListItem* new_item) {
if (!new_item) {
return -1;
}
// Allow existing_next_item to be NULL if the list is empty.
// Todo: why allow this? Keep it as is for now to avoid breaking API
// contract.
if (!existing_next_item && !Empty())
{
if (!existing_next_item && !Empty()) {
return -1;
}
CriticalSectionScoped lock(critical_section_);
if (!existing_next_item)
{
if (!existing_next_item) {
PushBackImpl(new_item);
return 0;
}
@ -206,59 +174,44 @@ int ListWrapper::InsertBefore(ListItem* existing_next_item,
new_item->next_ = existing_next_item;
new_item->prev_ = previous_item;
existing_next_item->prev_ = new_item;
if (previous_item)
{
if (previous_item) {
previous_item->next_ = new_item;
}
else
{
} else {
first_ = new_item;
}
size_++;
return 0;
}
int ListWrapper::Erase(ListItem* item)
{
if (!item)
{
int ListWrapper::Erase(ListItem* item) {
if (!item) {
return -1;
}
size_--;
ListItem* previous_item = item->prev_;
ListItem* next_item = item->next_;
if (!previous_item)
{
if(next_item)
{
if (!previous_item) {
if (next_item) {
next_item->prev_ = 0;
}
first_ = next_item;
}
else
{
} else {
previous_item->next_ = next_item;
}
if (!next_item)
{
if(previous_item)
{
if (!next_item) {
if (previous_item) {
previous_item->next_ = 0;
}
last_ = previous_item;
}
else
{
} else {
next_item->prev_ = previous_item;
}
delete item;
return 0;
}
void ListWrapper::PushBackImpl(ListItem* item)
{
if (Empty())
{
void ListWrapper::PushBackImpl(ListItem* item) {
if (Empty()) {
first_ = item;
last_ = item;
size_++;
@ -271,10 +224,8 @@ void ListWrapper::PushBackImpl(ListItem* item)
size_++;
}
void ListWrapper::PushFrontImpl(ListItem* item)
{
if (Empty())
{
void ListWrapper::PushFrontImpl(ListItem* item) {
if (Empty()) {
first_ = item;
last_ = item;
size_++;
@ -286,4 +237,5 @@ void ListWrapper::PushFrontImpl(ListItem* item)
first_ = item;
size_++;
}
} //namespace webrtc

View File

@ -11,13 +11,13 @@
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_LIST_NO_STL_H_
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_LIST_NO_STL_H_
#include "constructor_magic.h"
#include "webrtc/system_wrappers/interface/constructor_magic.h"
namespace webrtc {
class CriticalSectionWrapper;
class ListNoStlItem
{
class ListNoStlItem {
public:
ListNoStlItem(const void* ptr);
ListNoStlItem(const unsigned int item);
@ -37,9 +37,7 @@ private:
DISALLOW_COPY_AND_ASSIGN(ListNoStlItem);
};
class ListNoStl
{
class ListNoStl {
public:
ListNoStl();
virtual ~ListNoStl();
@ -74,6 +72,7 @@ private:
unsigned int size_;
DISALLOW_COPY_AND_ASSIGN(ListNoStl);
};
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_LIST_NO_STL_H_

View File

@ -8,118 +8,99 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "list_wrapper.h"
#include "webrtc/system_wrappers/interface/list_wrapper.h"
#include "trace.h"
#include "webrtc/system_wrappers/interface/trace.h"
namespace webrtc {
ListItem::ListItem(const void* item)
: this_iter_(),
item_ptr_(item),
item_(0)
{
item_(0) {
}
ListItem::ListItem(const unsigned int item)
: this_iter_(),
item_ptr_(0),
item_(item)
{
item_(item) {
}
ListItem::~ListItem()
{
ListItem::~ListItem() {
}
void* ListItem::GetItem() const
{
void* ListItem::GetItem() const {
return const_cast<void*>(item_ptr_);
}
unsigned int ListItem::GetUnsignedItem() const
{
unsigned int ListItem::GetUnsignedItem() const {
return item_;
}
ListWrapper::ListWrapper() : list_()
{
ListWrapper::ListWrapper()
: list_() {
}
ListWrapper::~ListWrapper()
{
if (!Empty())
{
ListWrapper::~ListWrapper() {
if (!Empty()) {
// TODO(hellner) I'm not sure this loggin is useful.
WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1,
"Potential memory leak in ListWrapper");
// Remove all remaining list items.
while (Erase(First()) == 0)
{}
while (Erase(First()) == 0) {}
}
}
bool ListWrapper::Empty() const
{
bool ListWrapper::Empty() const {
return list_.empty();
}
unsigned int ListWrapper::GetSize() const
{
unsigned int ListWrapper::GetSize() const {
return list_.size();
}
int ListWrapper::PushBack(const void* ptr)
{
int ListWrapper::PushBack(const void* ptr) {
ListItem* item = new ListItem(ptr);
list_.push_back(item);
return 0;
}
int ListWrapper::PushBack(const unsigned int item_id)
{
int ListWrapper::PushBack(const unsigned int item_id) {
ListItem* item = new ListItem(item_id);
list_.push_back(item);
return 0;
}
int ListWrapper::PushFront(const unsigned int item_id)
{
int ListWrapper::PushFront(const unsigned int item_id) {
ListItem* item = new ListItem(item_id);
list_.push_front(item);
return 0;
}
int ListWrapper::PushFront(const void* ptr)
{
int ListWrapper::PushFront(const void* ptr) {
ListItem* item = new ListItem(ptr);
list_.push_front(item);
return 0;
}
int ListWrapper::PopFront()
{
if(list_.empty())
{
int ListWrapper::PopFront() {
if (list_.empty()) {
return -1;
}
list_.pop_front();
return 0;
}
int ListWrapper::PopBack()
{
if(list_.empty())
{
int ListWrapper::PopBack() {
if (list_.empty()) {
return -1;
}
list_.pop_back();
return 0;
}
ListItem* ListWrapper::First() const
{
if(list_.empty())
{
ListItem* ListWrapper::First() const {
if (list_.empty()) {
return NULL;
}
std::list<ListItem*>::iterator item_iter = list_.begin();
@ -128,10 +109,8 @@ ListItem* ListWrapper::First() const
return return_item;
}
ListItem* ListWrapper::Last() const
{
if(list_.empty())
{
ListItem* ListWrapper::Last() const {
if (list_.empty()) {
return NULL;
}
// std::list::end() addresses the last item + 1. Decrement so that the
@ -143,16 +122,13 @@ ListItem* ListWrapper::Last() const
return return_item;
}
ListItem* ListWrapper::Next(ListItem* item) const
{
if(item == NULL)
{
ListItem* ListWrapper::Next(ListItem* item) const {
if (item == NULL) {
return NULL;
}
std::list<ListItem*>::iterator item_iter = item->this_iter_;
++item_iter;
if (item_iter == list_.end())
{
if (item_iter == list_.end()) {
return NULL;
}
ListItem* return_item = (*item_iter);
@ -160,15 +136,12 @@ ListItem* ListWrapper::Next(ListItem* item) const
return return_item;
}
ListItem* ListWrapper::Previous(ListItem* item) const
{
if(item == NULL)
{
ListItem* ListWrapper::Previous(ListItem* item) const {
if (item == NULL) {
return NULL;
}
std::list<ListItem*>::iterator item_iter = item->this_iter_;
if (item_iter == list_.begin())
{
if (item_iter == list_.begin()) {
return NULL;
}
--item_iter;
@ -178,27 +151,22 @@ ListItem* ListWrapper::Previous(ListItem* item) const
}
int ListWrapper::Insert(ListItem* existing_previous_item,
ListItem* new_item)
{
// Allow existingPreviousItem to be NULL if the list is empty.
ListItem* new_item) {
// Allow existing_previous_item to be NULL if the list is empty.
// TODO(hellner) why allow this? Keep it as is for now to avoid
// breaking API contract.
if (!existing_previous_item && !Empty())
{
if (!existing_previous_item && !Empty()) {
return -1;
}
if (!new_item)
{
if (!new_item) {
return -1;
}
std::list<ListItem*>::iterator insert_location = list_.begin();
if (!Empty())
{
if (!Empty()) {
insert_location = existing_previous_item->this_iter_;
if(insert_location != list_.end())
{
if (insert_location != list_.end()) {
++insert_location;
}
}
@ -208,23 +176,19 @@ int ListWrapper::Insert(ListItem* existing_previous_item,
}
int ListWrapper::InsertBefore(ListItem* existing_next_item,
ListItem* new_item)
{
ListItem* new_item) {
// Allow existing_next_item to be NULL if the list is empty.
// Todo: why allow this? Keep it as is for now to avoid breaking API
// contract.
if (!existing_next_item && !Empty())
{
if (!existing_next_item && !Empty()) {
return -1;
}
if (!new_item)
{
if (!new_item) {
return -1;
}
std::list<ListItem*>::iterator insert_location = list_.begin();
if (!Empty())
{
if (!Empty()) {
insert_location = existing_next_item->this_iter_;
}
@ -232,13 +196,12 @@ int ListWrapper::InsertBefore(ListItem* existing_next_item,
return 0;
}
int ListWrapper::Erase(ListItem* item)
{
if(item == NULL)
{
int ListWrapper::Erase(ListItem* item) {
if (item == NULL) {
return -1;
}
list_.erase(item->this_iter_);
return 0;
}
} // namespace webrtc

View File

@ -13,13 +13,11 @@
#include <list>
#include "constructor_magic.h"
#include "webrtc/system_wrappers/interface/constructor_magic.h"
namespace webrtc {
class ListItem
{
friend class ListWrapper;
class ListItem {
public:
ListItem(const void* ptr);
ListItem(const unsigned int item);
@ -28,14 +26,14 @@ public:
unsigned int GetUnsignedItem() const;
private:
friend class ListWrapper;
mutable std::list<ListItem*>::iterator this_iter_;
const void* item_ptr_;
const unsigned int item_;
DISALLOW_COPY_AND_ASSIGN(ListItem);
};
class ListWrapper
{
class ListWrapper {
public:
ListWrapper();
~ListWrapper();
@ -61,6 +59,7 @@ private:
mutable std::list<ListItem*> list_;
DISALLOW_COPY_AND_ASSIGN(ListWrapper);
};
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_LIST_STL_H_

View File

@ -8,10 +8,10 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "gtest/gtest.h"
#include "webrtc/system_wrappers/interface/list_wrapper.h"
#include "system_wrappers/interface/list_wrapper.h"
#include "system_wrappers/interface/scoped_ptr.h"
#include "gtest/gtest.h"
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
using ::webrtc::ListWrapper;
using ::webrtc::ListItem;
@ -228,8 +228,7 @@ public:
};
ListWrapperSimple* ListWrapperSimple::Create(bool static_allocation) {
if(static_allocation)
{
if (static_allocation) {
return new ListWrapperStatic();
}
return new ListWrapperDynamic();
@ -298,8 +297,7 @@ ListWrapperSimple* CreateInterleavedList(bool static_allocation) {
void PrintList(const ListWrapperSimple* list) {
ListItem* list_item = list->First();
printf("[");
while (list_item != NULL)
{
while (list_item != NULL) {
printf("%3u", list->GetUnsignedItem(list_item));
list_item = list->Next(list_item);
}
@ -404,8 +402,7 @@ TEST(ListWrapperTest,InterLeaveTest) {
unsigned int insert_position = kNumberOfElements / 2;
ASSERT_EQ(insert_position * 2, kNumberOfElements);
while (!descending_list->Empty())
{
while (!descending_list->Empty()) {
ListItem* item = descending_list->Last();
ASSERT_FALSE(item == NULL);
@ -450,8 +447,7 @@ TEST(ListWrapperTest,InterLeaveTestII) {
unsigned int insert_position = kNumberOfElements / 2;
ASSERT_EQ(insert_position * 2, kNumberOfElements);
while (!ascending_list->Empty())
{
while (!ascending_list->Empty()) {
ListItem* item = ascending_list->Last();
ASSERT_FALSE(item == NULL);