-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshared_memory.hpp
More file actions
102 lines (85 loc) · 2.72 KB
/
shared_memory.hpp
File metadata and controls
102 lines (85 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*****************************************************************************
* shared_memory.hpp
*
* Created: 5/15/2021 2021 by mguludag
*
* Copyright 2021 mguludag. All rights reserved.
*
* This file may be distributed under the terms of GNU Public License version
* 3 (GPL v3) as defined by the Free Software Foundation (FSF). A copy of the
* license should have been included with this file, or the project in which
* this file belongs to. You may also find the details of GPL v3 at:
* http://www.gnu.org/licenses/gpl-3.0.txt
*
* If you have any questions regarding the use of this file, feel free to
* contact the author of this file, or the owner of the project in which
* this file belongs to.
*****************************************************************************/
#ifndef SHARED_MEMORY_HPP
#define SHARED_MEMORY_HPP
#include <vector>
#include <deque>
#include <mutex>
namespace stdx {
template<typename T, template<typename Val, typename...> class Container = std::deque>
class shared_memory
{
public:
shared_memory() {}
/*!
* \brief at function returns item in shared memory with given index
* \param index
* \return T&
*/
static T &at(int index)
{
std::lock_guard<std::mutex> guard(m_mtx);
return m_data.at(index);
}
/*!
* \brief data function returns the container that used in shared memory
* \return Container<T>&
*/
static Container<T> &data()
{
std::lock_guard<std::mutex> guard(m_mtx);
return m_data;
}
/*!
* \brief push_back function adds the element at the last position in the shared memory
* \param data
*/
static void push_back(const T &data)
{
std::lock_guard<std::mutex> guard(m_mtx);
m_data.push_back(data);
}
/*!
* \brief clear function clears the shared memory
*/
static void clear()
{
std::lock_guard<std::mutex> guard(m_mtx);
m_data.clear();
}
/*!
* \brief pop_front pops and return the first element from the shared memory
* \return T&
*/
static T &pop_front()
{
std::lock_guard<std::mutex> guard(m_mtx);
auto data = m_data.at(0);
m_data.erase(0);
return data;
}
private:
static Container<T> m_data;
static std::mutex m_mtx;
};
template<typename T, template<typename Val, typename...> class Container = std::deque>
Container<T> shared_memory<T, Container>::m_data;
template<typename T, template<typename Val, typename...> class Container = std::deque>
std::mutex shared_memory<T, Container>::m_mtx;
} // namespace stdx
#endif // SHARED_MEMORY_HPP