media/webrtc/trunk/testing/gtest/samples/sample3-inl.h

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 // Copyright 2005, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29
michael@0 30 // A sample program demonstrating using Google C++ testing framework.
michael@0 31 //
michael@0 32 // Author: wan@google.com (Zhanyong Wan)
michael@0 33
michael@0 34 #ifndef GTEST_SAMPLES_SAMPLE3_INL_H_
michael@0 35 #define GTEST_SAMPLES_SAMPLE3_INL_H_
michael@0 36
michael@0 37 #include <stddef.h>
michael@0 38
michael@0 39
michael@0 40 // Queue is a simple queue implemented as a singled-linked list.
michael@0 41 //
michael@0 42 // The element type must support copy constructor.
michael@0 43 template <typename E> // E is the element type
michael@0 44 class Queue;
michael@0 45
michael@0 46 // QueueNode is a node in a Queue, which consists of an element of
michael@0 47 // type E and a pointer to the next node.
michael@0 48 template <typename E> // E is the element type
michael@0 49 class QueueNode {
michael@0 50 friend class Queue<E>;
michael@0 51
michael@0 52 public:
michael@0 53 // Gets the element in this node.
michael@0 54 const E& element() const { return element_; }
michael@0 55
michael@0 56 // Gets the next node in the queue.
michael@0 57 QueueNode* next() { return next_; }
michael@0 58 const QueueNode* next() const { return next_; }
michael@0 59
michael@0 60 private:
michael@0 61 // Creates a node with a given element value. The next pointer is
michael@0 62 // set to NULL.
michael@0 63 explicit QueueNode(const E& an_element) : element_(an_element), next_(NULL) {}
michael@0 64
michael@0 65 // We disable the default assignment operator and copy c'tor.
michael@0 66 const QueueNode& operator = (const QueueNode&);
michael@0 67 QueueNode(const QueueNode&);
michael@0 68
michael@0 69 E element_;
michael@0 70 QueueNode* next_;
michael@0 71 };
michael@0 72
michael@0 73 template <typename E> // E is the element type.
michael@0 74 class Queue {
michael@0 75 public:
michael@0 76 // Creates an empty queue.
michael@0 77 Queue() : head_(NULL), last_(NULL), size_(0) {}
michael@0 78
michael@0 79 // D'tor. Clears the queue.
michael@0 80 ~Queue() { Clear(); }
michael@0 81
michael@0 82 // Clears the queue.
michael@0 83 void Clear() {
michael@0 84 if (size_ > 0) {
michael@0 85 // 1. Deletes every node.
michael@0 86 QueueNode<E>* node = head_;
michael@0 87 QueueNode<E>* next = node->next();
michael@0 88 for (; ;) {
michael@0 89 delete node;
michael@0 90 node = next;
michael@0 91 if (node == NULL) break;
michael@0 92 next = node->next();
michael@0 93 }
michael@0 94
michael@0 95 // 2. Resets the member variables.
michael@0 96 head_ = last_ = NULL;
michael@0 97 size_ = 0;
michael@0 98 }
michael@0 99 }
michael@0 100
michael@0 101 // Gets the number of elements.
michael@0 102 size_t Size() const { return size_; }
michael@0 103
michael@0 104 // Gets the first element of the queue, or NULL if the queue is empty.
michael@0 105 QueueNode<E>* Head() { return head_; }
michael@0 106 const QueueNode<E>* Head() const { return head_; }
michael@0 107
michael@0 108 // Gets the last element of the queue, or NULL if the queue is empty.
michael@0 109 QueueNode<E>* Last() { return last_; }
michael@0 110 const QueueNode<E>* Last() const { return last_; }
michael@0 111
michael@0 112 // Adds an element to the end of the queue. A copy of the element is
michael@0 113 // created using the copy constructor, and then stored in the queue.
michael@0 114 // Changes made to the element in the queue doesn't affect the source
michael@0 115 // object, and vice versa.
michael@0 116 void Enqueue(const E& element) {
michael@0 117 QueueNode<E>* new_node = new QueueNode<E>(element);
michael@0 118
michael@0 119 if (size_ == 0) {
michael@0 120 head_ = last_ = new_node;
michael@0 121 size_ = 1;
michael@0 122 } else {
michael@0 123 last_->next_ = new_node;
michael@0 124 last_ = new_node;
michael@0 125 size_++;
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 // Removes the head of the queue and returns it. Returns NULL if
michael@0 130 // the queue is empty.
michael@0 131 E* Dequeue() {
michael@0 132 if (size_ == 0) {
michael@0 133 return NULL;
michael@0 134 }
michael@0 135
michael@0 136 const QueueNode<E>* const old_head = head_;
michael@0 137 head_ = head_->next_;
michael@0 138 size_--;
michael@0 139 if (size_ == 0) {
michael@0 140 last_ = NULL;
michael@0 141 }
michael@0 142
michael@0 143 E* element = new E(old_head->element());
michael@0 144 delete old_head;
michael@0 145
michael@0 146 return element;
michael@0 147 }
michael@0 148
michael@0 149 // Applies a function/functor on each element of the queue, and
michael@0 150 // returns the result in a new queue. The original queue is not
michael@0 151 // affected.
michael@0 152 template <typename F>
michael@0 153 Queue* Map(F function) const {
michael@0 154 Queue* new_queue = new Queue();
michael@0 155 for (const QueueNode<E>* node = head_; node != NULL; node = node->next_) {
michael@0 156 new_queue->Enqueue(function(node->element()));
michael@0 157 }
michael@0 158
michael@0 159 return new_queue;
michael@0 160 }
michael@0 161
michael@0 162 private:
michael@0 163 QueueNode<E>* head_; // The first node of the queue.
michael@0 164 QueueNode<E>* last_; // The last node of the queue.
michael@0 165 size_t size_; // The number of elements in the queue.
michael@0 166
michael@0 167 // We disallow copying a queue.
michael@0 168 Queue(const Queue&);
michael@0 169 const Queue& operator = (const Queue&);
michael@0 170 };
michael@0 171
michael@0 172 #endif // GTEST_SAMPLES_SAMPLE3_INL_H_

mercurial