SGCT master (487fac4)
Simple Graphics Cluster Toolkit
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
24namespace sgct {
25
30class SGCT_EXPORT SharedData {
31public:
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();
51
52private:
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
63template <typename T>
64void 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
77template <typename T>
78void 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
96template <>
97SGCT_EXPORT void serializeObject(std::vector<std::byte>& buffer, std::string_view value);
98
99SGCT_EXPORT void serializeObject(std::vector<std::byte>& buffer,
100 const std::string& value);
101SGCT_EXPORT void serializeObject(std::vector<std::byte>& buffer,
102 const std::wstring& value);
103
104template <typename T>
105void 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
117template <typename T>
118void 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
138template <>
139SGCT_EXPORT void deserializeObject(const std::vector<std::byte>& buffer, unsigned int& pos,
140 std::string& value);
141
142template <>
143SGCT_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__
static SharedData & instance()
void setEncodeFunction(std::function< std::vector< std::byte >()> function)
void setDecodeFunction(std::function< void(const std::vector< std::byte > &)> function)
void decode(const char *receivedData, int receivedLength)
This function is called internally by SGCT and shouldn't be used by the user.
static void destroy()
unsigned char * dataBlock()
void encode()
This fuction is called internally by SGCT and shouldn't be used by the user.
Definition actions.h:16
void serializeObject(std::vector< std::byte > &buffer, T value)
Definition shareddata.h:64
@ T
Definition keys.h:57
void deserializeObject(const std::vector< std::byte > &buffer, unsigned int &pos, T &value)
Definition shareddata.h:105