forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarybasic.hpp
148 lines (121 loc) · 3.36 KB
/
binarybasic.hpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// solid/serialization/binarybasic.hpp
//
// Copyright (c) 2012 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#ifndef SOLID_SERIALIZATION_BINARY_BASIC_HPP
#define SOLID_SERIALIZATION_BINARY_BASIC_HPP
#include "solid/utility/algorithm.hpp"
#include <cstring>
#include <array>
namespace solid{
namespace serialization{
namespace binary{
#define BASIC_DECL(tp) \
template <class S>\
void serialize(S &_s, tp &_t){\
_s.push(_t, "basic");\
}\
template <class S, class Ctx>\
void serialize(S &_s, tp &_t, Ctx &){\
_s.push(_t, "basic");\
}
inline char *store(char *_pd, const uint8_t _val){
uint8_t *pd = reinterpret_cast<uint8_t*>(_pd);
*pd = _val;
return _pd + 1;
}
inline char *store(char *_pd, const uint16_t _val){
uint8_t *pd = reinterpret_cast<uint8_t*>(_pd);
*(pd) = ((_val >> 8) & 0xff);
*(pd + 1) = (_val & 0xff);
return _pd + 2;
}
inline char *store(char *_pd, const uint32_t _val){
_pd = store(_pd, static_cast<uint16_t>(_val >> 16));
return store(_pd, static_cast<uint16_t>(_val & 0xffff));;
}
inline char *store(char *_pd, const uint64_t _val){
_pd = store(_pd, static_cast<uint32_t>(_val >> 32));
return store(_pd, static_cast<uint32_t>(_val & 0xffffffffULL));;
}
template <size_t S>
inline char *store(char *_pd, const std::array<uint8_t, S> _val){
memcpy(_pd, _val.data(), S);
return _pd + S;
}
inline const char* load(const char *_ps, uint8_t &_val){
const uint8_t *ps = reinterpret_cast<const uint8_t*>(_ps);
_val = *ps;
return _ps + 1;
}
inline const char* load(const char *_ps, uint16_t &_val){
const uint8_t *ps = reinterpret_cast<const uint8_t*>(_ps);
_val = *ps;
_val <<= 8;
_val |= *(ps + 1);
return _ps + 2;
}
inline const char* load(const char *_ps, uint32_t &_val){
uint16_t upper;
uint16_t lower;
_ps = load(_ps, upper);
_ps = load(_ps, lower);
_val = upper;
_val <<= 16;
_val |= lower;
return _ps;
}
inline const char* load(const char *_ps, uint64_t &_val){
uint32_t upper;
uint32_t lower;
_ps = load(_ps, upper);
_ps = load(_ps, lower);
_val = upper;
_val <<= 32;
_val |= lower;
return _ps;
}
template <size_t S>
inline const char *load(const char *_ps, std::array<uint8_t, S> &_val){
memcpy(_val.data, _ps, S);
return _ps + S;
}
//cross integer serialization
inline size_t crossSize(const char *_ps){
const uint8_t *ps = reinterpret_cast<const uint8_t*>(_ps);
uint8_t v = *ps;
const bool ok = check_value_with_crc(v, v);
if(ok){
return v + 1;
}
return -1;
}
inline size_t crossSize(uint8_t _v){
return max_padded_byte_cout(_v) + 1;
}
inline size_t crossSize(uint16_t _v){
return max_padded_byte_cout(_v) + 1;
}
inline size_t crossSize(uint32_t _v){
return max_padded_byte_cout(_v) + 1;
}
inline size_t crossSize(uint64_t _v){
return max_padded_byte_cout(_v) + 1;
}
char* crossStore(char *_pd, uint8_t _v);
char* crossStore(char *_pd, uint16_t _v);
char* crossStore(char *_pd, uint32_t _v);
char* crossStore(char *_pd, uint64_t _v);
const char* crossLoad(const char *_ps, uint8_t &_val);
const char* crossLoad(const char *_ps, uint16_t &_val);
const char* crossLoad(const char *_ps, uint32_t &_val);
const char* crossLoad(const char *_ps, uint64_t &_val);
}//namespace binary
}//namespace serialization
}//namespace solid
#endif