SGCT
master (487fac4)
Simple Graphics Cluster Toolkit
Toggle main menu visibility
Loading...
Searching...
No Matches
shareddata.h
Go to the documentation of this file.
1
/*****************************************************************************************
2
* SGCT *
3
* Simple Graphics Cluster Toolkit *
4
* *
5
* Copyright (c) 2012-2026 *
6
* For conditions of distribution and use, see copyright notice in LICENSE.md *
7
****************************************************************************************/
8
9
#ifndef __SGCT__SHAREDDATA__H__
10
#define __SGCT__SHAREDDATA__H__
11
12
#include <sgct/sgctexports.h>
13
14
#include <
sgct/network.h
>
15
#include <array>
16
#include <cstddef>
17
#include <cstdint>
18
#include <functional>
19
#include <string>
20
#include <string_view>
21
#include <type_traits>
22
#include <vector>
23
24
namespace
sgct
{
25
30
class
SGCT_EXPORT SharedData {
31
public
:
32
static
SharedData&
instance
();
33
static
void
destroy
();
34
35
void
setEncodeFunction
(std::function<std::vector<std::byte>()> function);
36
void
setDecodeFunction
(std::function<
void
(
const
std::vector<std::byte>&)> function);
37
41
void
encode
();
42
46
void
decode
(
const
char
* receivedData,
int
receivedLength);
47
48
unsigned
char
*
dataBlock
();
49
int
dataSize
();
50
int
bufferSize
();
51
52
private
:
53
SharedData();
54
55
std::function<std::vector<std::byte>()> _encodeFn;
56
std::function<void(
const
std::vector<std::byte>&)> _decodeFn;
57
58
static
SharedData* _instance;
59
std::vector<std::byte> _dataBlock;
60
std::array<std::byte, Network::HeaderSize> _headerSpace;
61
};
62
63
template
<
typename
T>
64
void
serializeObject
(std::vector<std::byte>& buffer, T value) {
65
static_assert
(
66
std::is_standard_layout_v<T> && std::is_trivial_v<T>,
67
"Type has to be a plain-old data type"
68
);
69
70
buffer.insert(
71
buffer.end(),
72
reinterpret_cast<
const
std::byte*
>
(&value),
73
reinterpret_cast<
const
std::byte*
>
(&value) +
sizeof
(
T
)
74
);
75
}
76
77
template
<
typename
T>
78
void
serializeObject
(std::vector<std::byte>& buffer,
const
std::vector<T>& value) {
79
static_assert
(
80
std::is_standard_layout_v<T> && std::is_trivial_v<T>,
81
"Type has to be a plain-old data type"
82
);
83
84
const
uint32_t size =
static_cast<
uint32_t
>
(value.size());
85
serializeObject
(buffer, size);
86
87
if
(size > 0) {
88
buffer.insert(
89
buffer.end(),
90
reinterpret_cast<
const
std::byte*
>
(value.data()),
91
reinterpret_cast<
const
std::byte*
>
(value.data()) + size *
sizeof
(
T
)
92
);
93
}
94
}
95
96
template
<>
97
SGCT_EXPORT
void
serializeObject
(std::vector<std::byte>& buffer, std::string_view value);
98
99
SGCT_EXPORT
void
serializeObject
(std::vector<std::byte>& buffer,
100
const
std::string& value);
101
SGCT_EXPORT
void
serializeObject
(std::vector<std::byte>& buffer,
102
const
std::wstring& value);
103
104
template
<
typename
T>
105
void
deserializeObject
(
const
std::vector<std::byte>& buffer,
unsigned
int
& pos,
106
T& value)
107
{
108
static_assert
(
109
std::is_standard_layout_v<T> && std::is_trivial_v<T>,
110
"Type has to be a plain-old data type"
111
);
112
113
value = *
reinterpret_cast<
const
T
*
>
(&buffer[pos]);
114
pos +=
sizeof
(
T
);
115
}
116
117
template
<
typename
T>
118
void
deserializeObject
(
const
std::vector<std::byte>& buffer,
unsigned
int
& pos,
119
std::vector<T>& value)
120
{
121
value.clear();
122
123
uint32_t size;
124
deserializeObject<uint32_t>
(buffer, pos, size);
125
126
if
(size == 0) {
127
return
;
128
}
129
130
value.clear();
131
value.assign(
132
reinterpret_cast<
const
T
*
>
(buffer.data() + pos),
133
reinterpret_cast<
const
T
*
>
(buffer.data() + pos + size *
sizeof
(
T
))
134
);
135
pos += size *
sizeof
(
T
);
136
}
137
138
template
<>
139
SGCT_EXPORT
void
deserializeObject
(
const
std::vector<std::byte>& buffer,
unsigned
int
& pos,
140
std::string& value);
141
142
template
<>
143
SGCT_EXPORT
void
deserializeObject
(
const
std::vector<std::byte>& buffer,
unsigned
int
& pos,
144
std::wstring& value);
145
146
}
// namespace sgct
147
148
#endif
// __SGCT__SHAREDDATA__H__
sgct::SharedData::instance
static SharedData & instance()
sgct::SharedData::bufferSize
int bufferSize()
sgct::SharedData::setEncodeFunction
void setEncodeFunction(std::function< std::vector< std::byte >()> function)
sgct::SharedData::setDecodeFunction
void setDecodeFunction(std::function< void(const std::vector< std::byte > &)> function)
sgct::SharedData::dataSize
int dataSize()
sgct::SharedData::decode
void decode(const char *receivedData, int receivedLength)
This function is called internally by SGCT and shouldn't be used by the user.
sgct::SharedData::destroy
static void destroy()
sgct::SharedData::dataBlock
unsigned char * dataBlock()
sgct::SharedData::encode
void encode()
This fuction is called internally by SGCT and shouldn't be used by the user.
sgct
Definition
actions.h:16
sgct::serializeObject
void serializeObject(std::vector< std::byte > &buffer, T value)
Definition
shareddata.h:64
sgct::Key::T
@ T
Definition
keys.h:57
sgct::deserializeObject
void deserializeObject(const std::vector< std::byte > &buffer, unsigned int &pos, T &value)
Definition
shareddata.h:105
network.h
sgct
shareddata.h
Generated by
1.18.0