A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PDBCircularBuffer.cc
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 
19 #ifndef PDB_CIRCULAR_BUFFER_CC
20 #define PDB_CIRCULAR_BUFFER_CC
21 
22 #include "PDBCircularBuffer.h"
23 #include <string>
24 #include <iostream>
25 #include <stdexcept>
26 #include <cstdlib>
27 
28 
29 template <class T>
31  this->maxArraySize = bufferSize + 1;
32  this->logger = logger;
33  this->initArray();
34 }
35 
36 template <class T>
38  // the buffer is not responsible for freeing the elements in the buffer
39  delete[] this->array;
40 }
41 
42 template <class T>
44  this->array = new T[this->maxArraySize];
45  if (this->array == nullptr) {
46  std::cout << "PDBCircularBuffer: Out of Memory in Heap.\n";
47  this->logger->writeLn("PDBCircularBuffer: Out of Memory in Heap.");
48  return -1;
49  }
50  int i;
51  for (i = 0; i < this->maxArraySize; i++) {
52 
53  this->array[i] = nullptr;
54  }
55  this->arrayHead = 0;
56  this->arrayTail = 0;
57  return 0;
58 }
59 
60 template <class T>
61 int PDBCircularBuffer<T>::addToTail(T const& elem) {
62  if (this->isFull()) {
63  return -1;
64  }
65  this->logger->writeLn(
66  "PDBCircularBuffer<T>: the buffer is not full, adding the element to tail...");
67  this->arrayTail = (this->arrayTail + 1) % this->maxArraySize;
68  this->array[this->arrayTail] = elem;
69 
70  return 0;
71 }
72 
73 template <class T>
75  if (this->isEmpty()) {
76  this->logger->writeLn("PDBCircularBuffer: array is empty.");
77  throw std::out_of_range("PDBCircularBuffer<>::popFromHead(): empty buffer");
78  }
79  this->arrayHead = (this->arrayHead + 1) % this->maxArraySize;
80  T elem = this->array[this->arrayHead];
81  return elem;
82 }
83 
84 template <class T>
86  return (this->arrayHead == (this->arrayTail + 1) % this->maxArraySize);
87 }
88 
89 template <class T>
91  return (this->arrayHead == this->arrayTail);
92 }
93 
94 template <class T>
96  return (this->arrayTail - this->arrayHead + this->maxArraySize) % this->maxArraySize;
97 }
98 
99 #endif
PDBCircularBuffer(unsigned int bufferSize, pdb::PDBLoggerPtr logger)
unsigned int getSize()
int addToTail(T const &)
std::shared_ptr< PDBLogger > PDBLoggerPtr
Definition: PDBLogger.h:40