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


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:07