BmnRoot
Loading...
Searching...
No Matches
UniValue.h
Go to the documentation of this file.
1// -------------------------------------------------------------------------
2// ----- UniValue header file -----
3// ----- Created 03/05/17 by K. Gertsenberger -----
4// -------------------------------------------------------------------------
5
12#ifndef UNIVALUE_H
13#define UNIVALUE_H 1
14
15#include "nlohmann/json.hpp"
16#include "uni_db_settings.h"
17
18#include <boost/endian/conversion.hpp>
19#include <cstring>
20#include <iostream>
21#include <map>
22#include <string>
23#include <vector>
24using namespace std;
26
27enum enumValueType : unsigned int;
29{
30 virtual ~UniValue() {} // Destructor
31 virtual enumValueType GetType() = 0;
32 virtual size_t GetStorageSize() = 0;
33 virtual void ReadValue(unsigned char* source) = 0;
34 virtual void WriteValue(unsigned char* destination) = 0;
35
36 protected:
37 void Read(unsigned char*& source, uint8_t& value)
38 {
39 memcpy(&value, source, 1);
40 source++;
41 }
42 void Write(unsigned char*& destination, uint8_t& value)
43 {
44 memcpy(destination, &value, 1);
45 destination++;
46 }
47
48 void Read(unsigned char*& source, int32_t& value)
49 {
50 memcpy(&value, source, 4);
51 boost::endian::little_to_native_inplace(value);
52 source += 4;
53 }
54 void Write(unsigned char*& destination, int32_t& value)
55 {
56 boost::endian::native_to_little_inplace(value);
57 memcpy(destination, &value, 4);
58 destination += 4;
59 }
60
61 void Read(unsigned char*& source, uint32_t& value)
62 {
63 memcpy(&value, source, 4);
64 boost::endian::little_to_native_inplace(value);
65 source += 4;
66 }
67 void Write(unsigned char*& destination, uint32_t& value)
68 {
69 boost::endian::native_to_little_inplace(value);
70 memcpy(destination, &value, 4);
71 destination += 4;
72 }
73
74 void Read(unsigned char*& source, double& value)
75 {
76 memcpy(&value, source, 8);
77 source += 8;
78 }
79 void Write(unsigned char*& destination, double& value)
80 {
81 memcpy(destination, &value, 8);
82 destination += 8;
83 }
84
85 void Read(unsigned char*& source, string& value)
86 {
87 value.assign((char*)source);
88 source += value.length() + 1;
89 }
90 void Write(unsigned char*& destination, string& value)
91 {
92 memcpy(destination, value.c_str(), value.length() + 1);
93 destination += value.length() + 1;
94 }
95
96 void Read(unsigned char*& source, unsigned char*& value, uint64_t& size)
97 {
98 memcpy(&size, source, 8);
99 boost::endian::little_to_native_inplace(size);
100 source += 8;
101 value = new unsigned char[size];
102 memcpy(value, source, size);
103 source += size;
104 }
105 void Write(unsigned char*& destination, unsigned char* value, uint64_t size)
106 {
107 if (size > 0) {
108 uint64_t size_little = boost::endian::native_to_little(size);
109 memcpy(destination, &size_little, 8);
110 destination += 8;
111 memcpy(destination, value, size);
112 destination += size;
113 } else
114 cout << "ERROR: count of bytes for the value should be greater than zero. The value was not written to the "
115 "database!"
116 << endl;
117 }
118
119 void Read(unsigned char*& source, double* value, const uint64_t count)
120 {
121 memcpy(value, source, count * 8);
122 source += count * 8;
123 }
124 void Write(unsigned char*& destination, double* value, const uint64_t count)
125 {
126 memcpy(destination, value, count * 8);
127 destination += count * 8;
128 }
129
130 template<size_t rows, size_t cols>
131 void Read(unsigned char*& source, double (&value)[rows][cols])
132 {
133 memcpy(value, source, rows * cols * 8);
134 source += rows * cols * 8;
135 }
136 template<size_t rows, size_t cols>
137 void Write(unsigned char*& destination, double (&value)[rows][cols])
138 {
139 memcpy(destination, value, rows * cols * 8);
140 destination += rows * cols * 8;
141 }
142
143 void Read(unsigned char*& source, double*** value, uint8_t& size1, uint8_t& size2, uint8_t& size3)
144 {
145 memcpy(&size1, source, 1);
146 boost::endian::little_to_native_inplace(size1);
147 source += 1;
148 memcpy(&size2, source, 1);
149 boost::endian::little_to_native_inplace(size2);
150 source += 1;
151 memcpy(&size3, source, 1);
152 boost::endian::little_to_native_inplace(size3);
153 source += 1;
154
155 value = new double**[size1];
156 for (int i = 0; i < size1; i++) {
157 value[i] = new double*[size2];
158 for (int j = 0; j < size2; j++)
159 value[i][j] = new double[size3];
160 }
161 uint32_t full_size = size1 * size2 * size3 * 8;
162 memcpy(value, source, full_size);
163 source += full_size;
164 }
165 void Write(unsigned char*& destination, double*** value, uint8_t size1, uint8_t size2, uint8_t size3)
166 {
167 uint32_t full_size = size1 * size2 * size3 * 8;
168 if (full_size > 0) {
169 uint8_t size1_little = boost::endian::native_to_little(size1);
170 memcpy(destination, &size1_little, 1);
171 destination += 1;
172 uint8_t size2_little = boost::endian::native_to_little(size2);
173 memcpy(destination, &size2_little, 1);
174 destination += 1;
175 uint8_t size3_little = boost::endian::native_to_little(size3);
176 memcpy(destination, &size3_little, 1);
177 destination += 1;
178
179 memcpy(destination, value, full_size);
180 destination += full_size;
181 } else
182 cout << "ERROR: count of bytes for the value should be greater than zero. The value was not written to the "
183 "database!"
184 << endl;
185 }
186
187 void Read(unsigned char*& source, vector<double>& value)
188 {
189 uint64_t size;
190 memcpy(&size, source, 8);
191 boost::endian::little_to_native_inplace(size);
192 source += 8;
193 value.assign((double*)source, (double*)(source + size));
194 source += size;
195 }
196 void Write(unsigned char*& destination, vector<double>& value)
197 {
198 uint64_t size = value.size() * 8;
199 if (size > 0) {
200 uint64_t size_little = boost::endian::native_to_little(size);
201 memcpy(destination, &size_little, 8);
202 destination += 8;
203 memcpy(destination, value.data(), size);
204 destination += size;
205 } else
206 cout << "ERROR: vector size of the value should be greater than zero. The value was not written to the "
207 "database!"
208 << endl;
209 }
210
211 void Read(unsigned char*& source, json& value)
212 {
213 uint64_t size;
214 memcpy(&size, source, 8);
215 boost::endian::little_to_native_inplace(size);
216 source += 8;
217
218 vector<uint8_t> uint8_vec((uint8_t*)source, (uint8_t*)(source + size));
219 value = json::from_cbor(uint8_vec);
220 source += size;
221 }
222 void Write(unsigned char*& destination, json& value)
223 {
224 vector<uint8_t> uint8_vec = json::to_cbor(value);
225 uint64_t size = uint8_vec.size();
226 if (size > 0) {
227 uint64_t size_little = boost::endian::native_to_little(size);
228 memcpy(destination, &size_little, 8);
229 destination += 8;
230 memcpy(destination, uint8_vec.data(), size);
231 destination += size;
232 } else
233 cout << "ERROR: json size should be greater than zero. The value was not written to the database!" << endl;
234 }
235};
236
237#endif
int i
Definition P4_F32vec4.h:22
a class to store JSON values
Definition json.hpp:17282
static std::vector< std::uint8_t > to_cbor(const basic_json &j)
create a CBOR serialization of a given JSON value
Definition json.hpp:21010
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json from_cbor(InputType &&i, const bool strict=true, const bool allow_exceptions=true, const cbor_tag_handler_t tag_handler=cbor_tag_handler_t::error)
create a JSON value from an input in CBOR format
Definition json.hpp:21108
enumValueType
basic_json<> json
default specialization
Definition json.hpp:3337
STL namespace.
void Write(unsigned char *&destination, vector< double > &value)
Definition UniValue.h:196
void Write(unsigned char *&destination, uint8_t &value)
Definition UniValue.h:42
void Read(unsigned char *&source, uint8_t &value)
Definition UniValue.h:37
virtual ~UniValue()
Definition UniValue.h:30
virtual enumValueType GetType()=0
void Read(unsigned char *&source, json &value)
Definition UniValue.h:211
virtual void ReadValue(unsigned char *source)=0
void Read(unsigned char *&source, double &value)
Definition UniValue.h:74
void Read(unsigned char *&source, unsigned char *&value, uint64_t &size)
Definition UniValue.h:96
void Write(unsigned char *&destination, uint32_t &value)
Definition UniValue.h:67
void Read(unsigned char *&source, string &value)
Definition UniValue.h:85
void Read(unsigned char *&source, vector< double > &value)
Definition UniValue.h:187
void Read(unsigned char *&source, double *value, const uint64_t count)
Definition UniValue.h:119
void Write(unsigned char *&destination, double ***value, uint8_t size1, uint8_t size2, uint8_t size3)
Definition UniValue.h:165
void Write(unsigned char *&destination, double *value, const uint64_t count)
Definition UniValue.h:124
virtual void WriteValue(unsigned char *destination)=0
void Read(unsigned char *&source, double(&value)[rows][cols])
Definition UniValue.h:131
void Write(unsigned char *&destination, double &value)
Definition UniValue.h:79
void Write(unsigned char *&destination, json &value)
Definition UniValue.h:222
void Write(unsigned char *&destination, unsigned char *value, uint64_t size)
Definition UniValue.h:105
virtual size_t GetStorageSize()=0
void Read(unsigned char *&source, uint32_t &value)
Definition UniValue.h:61
void Write(unsigned char *&destination, double(&value)[rows][cols])
Definition UniValue.h:137
void Read(unsigned char *&source, int32_t &value)
Definition UniValue.h:48
void Write(unsigned char *&destination, int32_t &value)
Definition UniValue.h:54
void Read(unsigned char *&source, double ***value, uint8_t &size1, uint8_t &size2, uint8_t &size3)
Definition UniValue.h:143
void Write(unsigned char *&destination, string &value)
Definition UniValue.h:90