Project
Loading...
Searching...
No Matches
FIFO.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#ifndef ALICEO2_FIFOUTILS_H_
16#define ALICEO2_FIFOUTILS_H_
17
18#include <deque>
19#include <mutex>
20#include <stdexcept>
21
22namespace o2
23{
24namespace utils
25{
26
27template <typename T>
28class FIFO
29{
30 public:
31 size_t size() const
32 {
33 std::lock_guard<std::mutex> lock(mMutex);
34 return mQueue.size();
35 }
36
37 void clear()
38 {
39 std::lock_guard<std::mutex> lock(mMutex);
40 mQueue.clear();
41 }
42
43 bool empty() const
44 {
45 std::lock_guard<std::mutex> lock(mMutex);
46 return mQueue.empty();
47 }
48
49 template <typename... Args>
50 void push(Args&&... args)
51 {
52 std::lock_guard<std::mutex> lock(mMutex);
53 mQueue.emplace_back(std::forward<Args>(args)...);
54 }
55
56 void pop()
57 {
58 std::lock_guard<std::mutex> lock(mMutex);
59 if (!mQueue.empty()) {
60 mQueue.pop_front();
61 }
62 }
63
64 const T& front() const
65 {
66 std::lock_guard<std::mutex> lock(mMutex);
67 if (mQueue.empty()) {
68 throw std::runtime_error("attempt to access front of empty queue");
69 }
70 return mQueue.front();
71 }
72
73 T& front()
74 {
75 std::lock_guard<std::mutex> lock(mMutex);
76 if (mQueue.empty()) {
77 throw std::runtime_error("attempt to access front of empty queue");
78 }
79 return mQueue.front();
80 }
81
82 const T* frontPtr() const
83 {
84 std::lock_guard<std::mutex> lock(mMutex);
85 if (mQueue.empty()) {
86 return nullptr;
87 }
88 return &mQueue.front();
89 }
90
92 {
93 std::lock_guard<std::mutex> lock(mMutex);
94 if (mQueue.empty()) {
95 return nullptr;
96 }
97 return &mQueue.front();
98 }
99
100 auto& getQueue() const { return mQueue; }
101
102 private:
103 mutable std::mutex mMutex;
104 std::deque<T> mQueue{};
105};
106
107} // namespace utils
108} // namespace o2
109
110#endif
const T * frontPtr() const
Definition FIFO.h:82
size_t size() const
Definition FIFO.h:31
void pop()
Definition FIFO.h:56
const T & front() const
Definition FIFO.h:64
void clear()
Definition FIFO.h:37
auto & getQueue() const
Definition FIFO.h:100
void push(Args &&... args)
Definition FIFO.h:50
T & front()
Definition FIFO.h:73
bool empty() const
Definition FIFO.h:43
T * frontPtr()
Definition FIFO.h:91
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Common utility functions.