Blame googletest/samples/sample3-inl.h

Packit bd1cd8
// Copyright 2005, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
Packit bd1cd8
// A sample program demonstrating using Google C++ testing framework.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
Packit bd1cd8
#ifndef GTEST_SAMPLES_SAMPLE3_INL_H_
Packit bd1cd8
#define GTEST_SAMPLES_SAMPLE3_INL_H_
Packit bd1cd8
Packit bd1cd8
#include <stddef.h>
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
// Queue is a simple queue implemented as a singled-linked list.
Packit bd1cd8
//
Packit bd1cd8
// The element type must support copy constructor.
Packit bd1cd8
template <typename E>  // E is the element type
Packit bd1cd8
class Queue;
Packit bd1cd8
Packit bd1cd8
// QueueNode is a node in a Queue, which consists of an element of
Packit bd1cd8
// type E and a pointer to the next node.
Packit bd1cd8
template <typename E>  // E is the element type
Packit bd1cd8
class QueueNode {
Packit bd1cd8
  friend class Queue<E>;
Packit bd1cd8
Packit bd1cd8
 public:
Packit bd1cd8
  // Gets the element in this node.
Packit bd1cd8
  const E& element() const { return element_; }
Packit bd1cd8
Packit bd1cd8
  // Gets the next node in the queue.
Packit bd1cd8
  QueueNode* next() { return next_; }
Packit bd1cd8
  const QueueNode* next() const { return next_; }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  // Creates a node with a given element value.  The next pointer is
Packit bd1cd8
  // set to NULL.
Packit bd1cd8
  explicit QueueNode(const E& an_element) : element_(an_element), next_(NULL) {}
Packit bd1cd8
Packit bd1cd8
  // We disable the default assignment operator and copy c'tor.
Packit bd1cd8
  const QueueNode& operator = (const QueueNode&);
Packit bd1cd8
  QueueNode(const QueueNode&);
Packit bd1cd8
Packit bd1cd8
  E element_;
Packit bd1cd8
  QueueNode* next_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
template <typename E>  // E is the element type.
Packit bd1cd8
class Queue {
Packit bd1cd8
 public:
Packit bd1cd8
  // Creates an empty queue.
Packit bd1cd8
  Queue() : head_(NULL), last_(NULL), size_(0) {}
Packit bd1cd8
Packit bd1cd8
  // D'tor.  Clears the queue.
Packit bd1cd8
  ~Queue() { Clear(); }
Packit bd1cd8
Packit bd1cd8
  // Clears the queue.
Packit bd1cd8
  void Clear() {
Packit bd1cd8
    if (size_ > 0) {
Packit bd1cd8
      // 1. Deletes every node.
Packit bd1cd8
      QueueNode<E>* node = head_;
Packit bd1cd8
      QueueNode<E>* next = node->next();
Packit bd1cd8
      for (; ;) {
Packit bd1cd8
        delete node;
Packit bd1cd8
        node = next;
Packit bd1cd8
        if (node == NULL) break;
Packit bd1cd8
        next = node->next();
Packit bd1cd8
      }
Packit bd1cd8
Packit bd1cd8
      // 2. Resets the member variables.
Packit bd1cd8
      head_ = last_ = NULL;
Packit bd1cd8
      size_ = 0;
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Gets the number of elements.
Packit bd1cd8
  size_t Size() const { return size_; }
Packit bd1cd8
Packit bd1cd8
  // Gets the first element of the queue, or NULL if the queue is empty.
Packit bd1cd8
  QueueNode<E>* Head() { return head_; }
Packit bd1cd8
  const QueueNode<E>* Head() const { return head_; }
Packit bd1cd8
Packit bd1cd8
  // Gets the last element of the queue, or NULL if the queue is empty.
Packit bd1cd8
  QueueNode<E>* Last() { return last_; }
Packit bd1cd8
  const QueueNode<E>* Last() const { return last_; }
Packit bd1cd8
Packit bd1cd8
  // Adds an element to the end of the queue.  A copy of the element is
Packit bd1cd8
  // created using the copy constructor, and then stored in the queue.
Packit bd1cd8
  // Changes made to the element in the queue doesn't affect the source
Packit bd1cd8
  // object, and vice versa.
Packit bd1cd8
  void Enqueue(const E& element) {
Packit bd1cd8
    QueueNode<E>* new_node = new QueueNode<E>(element);
Packit bd1cd8
Packit bd1cd8
    if (size_ == 0) {
Packit bd1cd8
      head_ = last_ = new_node;
Packit bd1cd8
      size_ = 1;
Packit bd1cd8
    } else {
Packit bd1cd8
      last_->next_ = new_node;
Packit bd1cd8
      last_ = new_node;
Packit bd1cd8
      size_++;
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Removes the head of the queue and returns it.  Returns NULL if
Packit bd1cd8
  // the queue is empty.
Packit bd1cd8
  E* Dequeue() {
Packit bd1cd8
    if (size_ == 0) {
Packit bd1cd8
      return NULL;
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
    const QueueNode<E>* const old_head = head_;
Packit bd1cd8
    head_ = head_->next_;
Packit bd1cd8
    size_--;
Packit bd1cd8
    if (size_ == 0) {
Packit bd1cd8
      last_ = NULL;
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
    E* element = new E(old_head->element());
Packit bd1cd8
    delete old_head;
Packit bd1cd8
Packit bd1cd8
    return element;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Applies a function/functor on each element of the queue, and
Packit bd1cd8
  // returns the result in a new queue.  The original queue is not
Packit bd1cd8
  // affected.
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  Queue* Map(F function) const {
Packit bd1cd8
    Queue* new_queue = new Queue();
Packit bd1cd8
    for (const QueueNode<E>* node = head_; node != NULL; node = node->next_) {
Packit bd1cd8
      new_queue->Enqueue(function(node->element()));
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
    return new_queue;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  QueueNode<E>* head_;  // The first node of the queue.
Packit bd1cd8
  QueueNode<E>* last_;  // The last node of the queue.
Packit bd1cd8
  size_t size_;  // The number of elements in the queue.
Packit bd1cd8
Packit bd1cd8
  // We disallow copying a queue.
Packit bd1cd8
  Queue(const Queue&);
Packit bd1cd8
  const Queue& operator = (const Queue&);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
#endif  // GTEST_SAMPLES_SAMPLE3_INL_H_