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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/testing/gtest/samples/sample3-inl.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,172 @@
     1.4 +// Copyright 2005, Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +
    1.33 +// A sample program demonstrating using Google C++ testing framework.
    1.34 +//
    1.35 +// Author: wan@google.com (Zhanyong Wan)
    1.36 +
    1.37 +#ifndef GTEST_SAMPLES_SAMPLE3_INL_H_
    1.38 +#define GTEST_SAMPLES_SAMPLE3_INL_H_
    1.39 +
    1.40 +#include <stddef.h>
    1.41 +
    1.42 +
    1.43 +// Queue is a simple queue implemented as a singled-linked list.
    1.44 +//
    1.45 +// The element type must support copy constructor.
    1.46 +template <typename E>  // E is the element type
    1.47 +class Queue;
    1.48 +
    1.49 +// QueueNode is a node in a Queue, which consists of an element of
    1.50 +// type E and a pointer to the next node.
    1.51 +template <typename E>  // E is the element type
    1.52 +class QueueNode {
    1.53 +  friend class Queue<E>;
    1.54 +
    1.55 + public:
    1.56 +  // Gets the element in this node.
    1.57 +  const E& element() const { return element_; }
    1.58 +
    1.59 +  // Gets the next node in the queue.
    1.60 +  QueueNode* next() { return next_; }
    1.61 +  const QueueNode* next() const { return next_; }
    1.62 +
    1.63 + private:
    1.64 +  // Creates a node with a given element value.  The next pointer is
    1.65 +  // set to NULL.
    1.66 +  explicit QueueNode(const E& an_element) : element_(an_element), next_(NULL) {}
    1.67 +
    1.68 +  // We disable the default assignment operator and copy c'tor.
    1.69 +  const QueueNode& operator = (const QueueNode&);
    1.70 +  QueueNode(const QueueNode&);
    1.71 +
    1.72 +  E element_;
    1.73 +  QueueNode* next_;
    1.74 +};
    1.75 +
    1.76 +template <typename E>  // E is the element type.
    1.77 +class Queue {
    1.78 + public:
    1.79 +  // Creates an empty queue.
    1.80 +  Queue() : head_(NULL), last_(NULL), size_(0) {}
    1.81 +
    1.82 +  // D'tor.  Clears the queue.
    1.83 +  ~Queue() { Clear(); }
    1.84 +
    1.85 +  // Clears the queue.
    1.86 +  void Clear() {
    1.87 +    if (size_ > 0) {
    1.88 +      // 1. Deletes every node.
    1.89 +      QueueNode<E>* node = head_;
    1.90 +      QueueNode<E>* next = node->next();
    1.91 +      for (; ;) {
    1.92 +        delete node;
    1.93 +        node = next;
    1.94 +        if (node == NULL) break;
    1.95 +        next = node->next();
    1.96 +      }
    1.97 +
    1.98 +      // 2. Resets the member variables.
    1.99 +      head_ = last_ = NULL;
   1.100 +      size_ = 0;
   1.101 +    }
   1.102 +  }
   1.103 +
   1.104 +  // Gets the number of elements.
   1.105 +  size_t Size() const { return size_; }
   1.106 +
   1.107 +  // Gets the first element of the queue, or NULL if the queue is empty.
   1.108 +  QueueNode<E>* Head() { return head_; }
   1.109 +  const QueueNode<E>* Head() const { return head_; }
   1.110 +
   1.111 +  // Gets the last element of the queue, or NULL if the queue is empty.
   1.112 +  QueueNode<E>* Last() { return last_; }
   1.113 +  const QueueNode<E>* Last() const { return last_; }
   1.114 +
   1.115 +  // Adds an element to the end of the queue.  A copy of the element is
   1.116 +  // created using the copy constructor, and then stored in the queue.
   1.117 +  // Changes made to the element in the queue doesn't affect the source
   1.118 +  // object, and vice versa.
   1.119 +  void Enqueue(const E& element) {
   1.120 +    QueueNode<E>* new_node = new QueueNode<E>(element);
   1.121 +
   1.122 +    if (size_ == 0) {
   1.123 +      head_ = last_ = new_node;
   1.124 +      size_ = 1;
   1.125 +    } else {
   1.126 +      last_->next_ = new_node;
   1.127 +      last_ = new_node;
   1.128 +      size_++;
   1.129 +    }
   1.130 +  }
   1.131 +
   1.132 +  // Removes the head of the queue and returns it.  Returns NULL if
   1.133 +  // the queue is empty.
   1.134 +  E* Dequeue() {
   1.135 +    if (size_ == 0) {
   1.136 +      return NULL;
   1.137 +    }
   1.138 +
   1.139 +    const QueueNode<E>* const old_head = head_;
   1.140 +    head_ = head_->next_;
   1.141 +    size_--;
   1.142 +    if (size_ == 0) {
   1.143 +      last_ = NULL;
   1.144 +    }
   1.145 +
   1.146 +    E* element = new E(old_head->element());
   1.147 +    delete old_head;
   1.148 +
   1.149 +    return element;
   1.150 +  }
   1.151 +
   1.152 +  // Applies a function/functor on each element of the queue, and
   1.153 +  // returns the result in a new queue.  The original queue is not
   1.154 +  // affected.
   1.155 +  template <typename F>
   1.156 +  Queue* Map(F function) const {
   1.157 +    Queue* new_queue = new Queue();
   1.158 +    for (const QueueNode<E>* node = head_; node != NULL; node = node->next_) {
   1.159 +      new_queue->Enqueue(function(node->element()));
   1.160 +    }
   1.161 +
   1.162 +    return new_queue;
   1.163 +  }
   1.164 +
   1.165 + private:
   1.166 +  QueueNode<E>* head_;  // The first node of the queue.
   1.167 +  QueueNode<E>* last_;  // The last node of the queue.
   1.168 +  size_t size_;  // The number of elements in the queue.
   1.169 +
   1.170 +  // We disallow copying a queue.
   1.171 +  Queue(const Queue&);
   1.172 +  const Queue& operator = (const Queue&);
   1.173 +};
   1.174 +
   1.175 +#endif  // GTEST_SAMPLES_SAMPLE3_INL_H_

mercurial