orca-sim
Buffer.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * This file is part of project ORCA. More information on the project
3  * can be found at the following repositories at GitHub's website.
4  *
5  * http://https://github.com/andersondomingues/orca-sim
6  * http://https://github.com/andersondomingues/orca-software
7  * http://https://github.com/andersondomingues/orca-mpsoc
8  * http://https://github.com/andersondomingues/orca-tools
9  *
10  * Copyright (C) 2018-2020 Anderson Domingues, <ti.andersondomingues@gmail.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 ******************************************************************************/
26 #include "Buffer.hpp"
27 
29 
34 template <typename T>
35 Buffer<T>::Buffer(std::string name, uint32_t capacity) : UntimedModel(name) {
36  _name = name;
37  _size = 0;
38  _queue = new std::queue<T>();
39  _capacity = capacity;
40 }
41 
45 template <typename T>
47  // the underlying queue is the only object
48  // with dynamic allocation
49  delete(_queue);
50 }
51 
55 template <typename T>
57  // prevents underflow
58  #ifdef BUFFER_UNDERFLOW_CHECKING
59  if (_size == 0) {
60  throw std::runtime_error(this->GetName() +
61  ": unable to pop from an empty queue");
62  }
63  #endif
64 
65  _size--;
66  _queue->pop();
67 }
68 
73 template <typename T>
74 void Buffer<T>::push(T e) {
75  #ifdef BUFFER_OVERFLOW_CHECKING
76  // prevents overflow
77  if (_size == _capacity)
78  throw std::runtime_error(this->GetName() +
79  ": unable to push to a full queue.");
80  #endif
81 
82  _size++;
83  _queue->push(e);
84 }
85 
90 template <typename T>
91 uint32_t Buffer<T>::capacity() {
92  return _capacity;
93 }
94 
99 template <typename T>
101  return _queue->front();
102 }
103 
108 template <typename T>
109 uint32_t Buffer<T>::size() {
110  return _size;
111 }
112 
117 template <typename T>
118 uint32_t Buffer<T>::full() {
119  return _size == _capacity;
120 }
Untimed models represent hardware models whose clock period is irrelevant for the simulation...