qxLib
include
qx
static_buffer.h
Go to the documentation of this file.
1
/**
2
3
@file static_buffer.h
4
@author Khrapov
5
@date 15.08.2022
6
@copyright © Nick Khrapov, 2022. All right reserved.
7
8
**/
9
#pragma once
10
11
#include <
qx/patterns/singleton.h
>
12
13
#include <functional>
14
#include <vector>
15
16
namespace
qx
17
{
18
19
/**
20
21
@class static_buffer
22
@brief Class contains static buffers of different types
23
@details These buffers may be used to decrease number of allocations when you need temporary storage
24
@author Khrapov
25
@date 15.08.2022
26
27
**/
28
class
static_buffer
29
{
30
QX_SINGLETON_S(
static_buffer
, thread_local);
31
32
public
:
33
/**
34
@brief Get buffer of type T
35
@tparam T - buffer type
36
@retval - buffer instance
37
**/
38
template
<
class
T>
39
T&
get_buffer
()
40
{
41
thread_local T buffer = [
this
]()
42
{
43
m_Cleaners.push_back(
44
[]()
45
{
46
buffer = T();
47
});
48
49
return
T();
50
}();
51
52
return
buffer;
53
}
54
55
/**
56
@brief Clear all buffers
57
@warning This method should be called from every thread using this class
58
Otherwise we would have to protect the modification of the buffer with a mutex
59
**/
60
void
clear
()
61
{
62
for
(
const
auto
& cleaner : m_Cleaners)
63
cleaner();
64
}
65
66
private
:
67
std::vector<std::function<void()>> m_Cleaners;
68
};
69
70
}
// namespace qx
qx::static_buffer
Class contains static buffers of different types.
Definition:
static_buffer.h:29
qx::static_buffer::clear
void clear()
Clear all buffers.
Definition:
static_buffer.h:60
qx::static_buffer::get_buffer
T & get_buffer()
Get buffer of type T.
Definition:
static_buffer.h:39
singleton.h
Generated by
1.9.1