|
1 |
| -/** |
2 |
| -* |
3 |
| -* buttonrpc library |
4 |
| -* Copyright 2018-04-28 Button |
5 |
| -* |
6 |
| -*/ |
7 |
| - |
8 |
| -#pragma once |
9 |
| -#include <vector> |
10 |
| -#include <sstream> |
11 |
| -#include <algorithm> |
12 |
| -#include <cstdint> |
13 |
| -using namespace std; |
14 |
| - |
15 |
| -class StreamBuffer : public vector<char> |
16 |
| -{ |
17 |
| -public: |
18 |
| - StreamBuffer(){ m_curpos = 0; } |
19 |
| - StreamBuffer(const char* in, size_t len){ |
20 |
| - m_curpos = 0; |
21 |
| - insert(begin(), in, in+len); |
22 |
| - } |
23 |
| - ~StreamBuffer(){ }; |
24 |
| - |
25 |
| - void reset(){ m_curpos = 0; } |
26 |
| - const char* data(){ return &(*this)[0]; } |
27 |
| - const char* current(){ return&(*this)[m_curpos]; } |
28 |
| - void offset(int k){ m_curpos += k; } |
29 |
| - bool is_eof(){ return (m_curpos >= size()); } |
30 |
| - void input( char* in, size_t len){ insert(end(), in, in+len); } |
31 |
| - int findc(char c){ |
32 |
| - iterator itr = find(begin()+m_curpos, end(), c); |
33 |
| - if (itr != end()) |
34 |
| - { |
35 |
| - return itr - (begin()+m_curpos); |
36 |
| - } |
37 |
| - return -1; |
38 |
| - } |
39 |
| - |
40 |
| -private: |
41 |
| - // 当前字节流位置 |
42 |
| - unsigned int m_curpos; |
43 |
| -}; |
44 |
| - |
45 |
| -class Serializer |
46 |
| -{ |
47 |
| -public: |
48 |
| - Serializer() { m_byteorder = LittleEndian; }; |
49 |
| - ~Serializer(){ }; |
50 |
| - |
51 |
| - Serializer(StreamBuffer dev, int byteorder=LittleEndian){ |
52 |
| - m_byteorder = byteorder; |
53 |
| - m_iodevice = dev; |
54 |
| - } |
55 |
| - |
56 |
| -public: |
57 |
| - enum ByteOrder { |
58 |
| - BigEndian, |
59 |
| - LittleEndian |
60 |
| - }; |
61 |
| - |
62 |
| -public: |
63 |
| - void reset(){ |
64 |
| - m_iodevice.reset(); |
65 |
| - } |
66 |
| - int size(){ |
67 |
| - return m_iodevice.size(); |
68 |
| - } |
69 |
| - void skip_raw_date(int k){ |
70 |
| - m_iodevice.offset(k); |
71 |
| - } |
72 |
| - const char* data(){ |
73 |
| - return m_iodevice.data(); |
74 |
| - } |
75 |
| - void byte_orser(char* in, int len){ |
76 |
| - if (m_byteorder == BigEndian){ |
77 |
| - reverse(in, in+len); |
78 |
| - } |
79 |
| - } |
80 |
| - void write_raw_data(char* in, int len){ |
81 |
| - m_iodevice.input(in, len); |
82 |
| - m_iodevice.offset(len); |
83 |
| - } |
84 |
| - const char* current(){ |
85 |
| - return m_iodevice.current(); |
86 |
| - } |
87 |
| - void clear(){ |
88 |
| - m_iodevice.clear(); |
89 |
| - reset(); |
90 |
| - } |
91 |
| - |
92 |
| - template<typename T> |
93 |
| - void output_type(T& t); |
94 |
| - |
95 |
| - template<typename T> |
96 |
| - void input_type(T t); |
97 |
| - |
98 |
| - // 直接给一个长度, 返回当前位置以后x个字节数据 |
99 |
| - void get_length_mem(char* p, int len){ |
100 |
| - memcpy(p, m_iodevice.current(), len); |
101 |
| - m_iodevice.offset(len); |
102 |
| - } |
103 |
| - |
104 |
| -public: |
105 |
| - template<typename Tuple, std::size_t Id> |
106 |
| - void getv(Serializer& ds, Tuple& t) { |
107 |
| - ds >> std::get<Id>(t); |
108 |
| - } |
109 |
| - |
110 |
| - template<typename Tuple, std::size_t... I> |
111 |
| - Tuple get_tuple(std::index_sequence<I...>) { |
112 |
| - Tuple t; |
113 |
| - initializer_list<int>{((getv<Tuple, I>(*this, t)), 0)...}; |
114 |
| - return t; |
115 |
| - } |
116 |
| - |
117 |
| - template<typename T> |
118 |
| - Serializer &operator >> (T& i){ |
119 |
| - output_type(i); |
120 |
| - return *this; |
121 |
| - } |
122 |
| - |
123 |
| - template<typename T> |
124 |
| - Serializer &operator << (T i){ |
125 |
| - input_type(i); |
126 |
| - return *this; |
127 |
| - } |
128 |
| - |
129 |
| -private: |
130 |
| - int m_byteorder; |
131 |
| - StreamBuffer m_iodevice; |
132 |
| -}; |
133 |
| - |
134 |
| -template<typename T> |
135 |
| -inline void Serializer::output_type(T& t) |
136 |
| -{ |
137 |
| - int len = sizeof(T); |
138 |
| - char* d = new char[len]; |
139 |
| - if (!m_iodevice.is_eof()){ |
140 |
| - memcpy(d, m_iodevice.current(), len); |
141 |
| - m_iodevice.offset(len); |
142 |
| - byte_orser(d, len); |
143 |
| - t = *reinterpret_cast<T*>(&d[0]); |
144 |
| - } |
145 |
| - delete [] d; |
146 |
| -} |
147 |
| - |
148 |
| -template<> |
149 |
| -inline void Serializer::output_type(std::string& in) |
150 |
| -{ |
151 |
| - int marklen = sizeof(uint16_t); |
152 |
| - char* d = new char[marklen]; |
153 |
| - memcpy(d, m_iodevice.current(), marklen); |
154 |
| - byte_orser(d, marklen); |
155 |
| - int len = *reinterpret_cast<uint16_t*>(&d[0]); |
156 |
| - m_iodevice.offset(marklen); |
157 |
| - delete [] d; |
158 |
| - if (len == 0) return; |
159 |
| - in.insert(in.begin(), m_iodevice.current(), m_iodevice.current() + len); |
160 |
| - m_iodevice.offset(len); |
161 |
| -} |
162 |
| - |
163 |
| -template<typename T> |
164 |
| -inline void Serializer::input_type(T t) |
165 |
| -{ |
166 |
| - int len = sizeof(T); |
167 |
| - char* d = new char[len]; |
168 |
| - const char* p = reinterpret_cast<const char*>(&t); |
169 |
| - memcpy(d, p, len); |
170 |
| - byte_orser(d, len); |
171 |
| - m_iodevice.input(d, len); |
172 |
| - delete [] d; |
173 |
| -} |
174 |
| - |
175 |
| -template<> |
176 |
| -inline void Serializer::input_type(std::string in) |
177 |
| -{ |
178 |
| - // 先存入字符串长度 |
179 |
| - uint16_t len = in.size(); |
180 |
| - char* p = reinterpret_cast< char*>(&len); |
181 |
| - byte_orser(p, sizeof(uint16_t)); |
182 |
| - m_iodevice.input(p, sizeof(uint16_t)); |
183 |
| - |
184 |
| - // 存入字符串 |
185 |
| - if (len == 0) return; |
186 |
| - char* d = new char[len]; |
187 |
| - memcpy(d, in.c_str(), len); |
188 |
| - m_iodevice.input(d, len); |
189 |
| - delete [] d; |
190 |
| -} |
191 |
| - |
192 |
| -template<> |
193 |
| -inline void Serializer::input_type(const char* in) |
194 |
| -{ |
195 |
| - input_type<std::string>(std::string(in)); |
196 |
| -} |
| 1 | +/** |
| 2 | +* |
| 3 | +* buttonrpc library |
| 4 | +* Copyright 2018-04-28 Button |
| 5 | +* |
| 6 | +*/ |
| 7 | + |
| 8 | +#pragma once |
| 9 | +#include <vector> |
| 10 | +#include <sstream> |
| 11 | +#include <algorithm> |
| 12 | +#include <cstdint> |
| 13 | +#include <utility> |
| 14 | +#include <tuple> |
| 15 | +using namespace std; |
| 16 | + |
| 17 | +class StreamBuffer : public vector<char> |
| 18 | +{ |
| 19 | +public: |
| 20 | + StreamBuffer(){ m_curpos = 0; } |
| 21 | + StreamBuffer(const char* in, size_t len){ |
| 22 | + m_curpos = 0; |
| 23 | + insert(begin(), in, in+len); |
| 24 | + } |
| 25 | + ~StreamBuffer(){ } |
| 26 | + |
| 27 | + void reset(){ m_curpos = 0; } |
| 28 | + const char* data(){ return &(*this)[0]; } |
| 29 | + const char* current(){ return&(*this)[m_curpos]; } |
| 30 | + void offset(int k){ m_curpos += k; } |
| 31 | + bool is_eof(){ return (m_curpos >= size()); } |
| 32 | + void input( char* in, size_t len){ insert(end(), in, in+len); } |
| 33 | + int findc(char c){ |
| 34 | + iterator itr = find(begin()+m_curpos, end(), c); |
| 35 | + if (itr != end()) |
| 36 | + { |
| 37 | + return itr - (begin()+m_curpos); |
| 38 | + } |
| 39 | + return -1; |
| 40 | + } |
| 41 | + |
| 42 | +private: |
| 43 | + // 当前字节流位置 |
| 44 | + unsigned int m_curpos; |
| 45 | +}; |
| 46 | + |
| 47 | +class Serializer |
| 48 | +{ |
| 49 | +public: |
| 50 | + Serializer() { m_byteorder = LittleEndian; } |
| 51 | + ~Serializer(){ } |
| 52 | + |
| 53 | + Serializer(StreamBuffer dev, int byteorder=LittleEndian){ |
| 54 | + m_byteorder = byteorder; |
| 55 | + m_iodevice = dev; |
| 56 | + } |
| 57 | + |
| 58 | +public: |
| 59 | + enum ByteOrder { |
| 60 | + BigEndian, |
| 61 | + LittleEndian |
| 62 | + }; |
| 63 | + |
| 64 | +public: |
| 65 | + void reset(){ |
| 66 | + m_iodevice.reset(); |
| 67 | + } |
| 68 | + int size(){ |
| 69 | + return m_iodevice.size(); |
| 70 | + } |
| 71 | + void skip_raw_date(int k){ |
| 72 | + m_iodevice.offset(k); |
| 73 | + } |
| 74 | + const char* data(){ |
| 75 | + return m_iodevice.data(); |
| 76 | + } |
| 77 | + void byte_orser(char* in, int len){ |
| 78 | + if (m_byteorder == BigEndian){ |
| 79 | + reverse(in, in+len); |
| 80 | + } |
| 81 | + } |
| 82 | + void write_raw_data(char* in, int len){ |
| 83 | + m_iodevice.input(in, len); |
| 84 | + m_iodevice.offset(len); |
| 85 | + } |
| 86 | + const char* current(){ |
| 87 | + return m_iodevice.current(); |
| 88 | + } |
| 89 | + void clear(){ |
| 90 | + m_iodevice.clear(); |
| 91 | + reset(); |
| 92 | + } |
| 93 | + |
| 94 | + template<typename T> |
| 95 | + void output_type(T& t); |
| 96 | + |
| 97 | + template<typename T> |
| 98 | + void input_type(T t); |
| 99 | + |
| 100 | + // 直接给一个长度, 返回当前位置以后x个字节数据 |
| 101 | + void get_length_mem(char* p, int len){ |
| 102 | + memcpy(p, m_iodevice.current(), len); |
| 103 | + m_iodevice.offset(len); |
| 104 | + } |
| 105 | + |
| 106 | +public: |
| 107 | + template<typename Tuple, std::size_t Id> |
| 108 | + void getv(Serializer& ds, Tuple& t) { |
| 109 | + ds >> std::get<Id>(t); |
| 110 | + } |
| 111 | + |
| 112 | + template<typename Tuple, std::size_t... I> |
| 113 | + Tuple get_tuple(std::index_sequence<I...>) { |
| 114 | + Tuple t; |
| 115 | + initializer_list<int>{((getv<Tuple, I>(*this, t)), 0)...}; |
| 116 | + return t; |
| 117 | + } |
| 118 | + |
| 119 | + template<typename T> |
| 120 | + Serializer &operator >> (T& i){ |
| 121 | + output_type(i); |
| 122 | + return *this; |
| 123 | + } |
| 124 | + |
| 125 | + template<typename T> |
| 126 | + Serializer &operator << (T i){ |
| 127 | + input_type(i); |
| 128 | + return *this; |
| 129 | + } |
| 130 | + |
| 131 | +private: |
| 132 | + int m_byteorder; |
| 133 | + StreamBuffer m_iodevice; |
| 134 | +}; |
| 135 | + |
| 136 | +template<typename T> |
| 137 | +inline void Serializer::output_type(T& t) |
| 138 | +{ |
| 139 | + int len = sizeof(T); |
| 140 | + char* d = new char[len]; |
| 141 | + if (!m_iodevice.is_eof()){ |
| 142 | + memcpy(d, m_iodevice.current(), len); |
| 143 | + m_iodevice.offset(len); |
| 144 | + byte_orser(d, len); |
| 145 | + t = *reinterpret_cast<T*>(&d[0]); |
| 146 | + } |
| 147 | + delete [] d; |
| 148 | +} |
| 149 | + |
| 150 | +template<> |
| 151 | +inline void Serializer::output_type(std::string& in) |
| 152 | +{ |
| 153 | + int marklen = sizeof(uint16_t); |
| 154 | + char* d = new char[marklen]; |
| 155 | + memcpy(d, m_iodevice.current(), marklen); |
| 156 | + byte_orser(d, marklen); |
| 157 | + int len = *reinterpret_cast<uint16_t*>(&d[0]); |
| 158 | + m_iodevice.offset(marklen); |
| 159 | + delete [] d; |
| 160 | + if (len == 0) return; |
| 161 | + in.insert(in.begin(), m_iodevice.current(), m_iodevice.current() + len); |
| 162 | + m_iodevice.offset(len); |
| 163 | +} |
| 164 | + |
| 165 | +template<typename T> |
| 166 | +inline void Serializer::input_type(T t) |
| 167 | +{ |
| 168 | + int len = sizeof(T); |
| 169 | + char* d = new char[len]; |
| 170 | + const char* p = reinterpret_cast<const char*>(&t); |
| 171 | + memcpy(d, p, len); |
| 172 | + byte_orser(d, len); |
| 173 | + m_iodevice.input(d, len); |
| 174 | + delete [] d; |
| 175 | +} |
| 176 | + |
| 177 | +template<> |
| 178 | +inline void Serializer::input_type(std::string in) |
| 179 | +{ |
| 180 | + // 先存入字符串长度 |
| 181 | + uint16_t len = in.size(); |
| 182 | + char* p = reinterpret_cast< char*>(&len); |
| 183 | + byte_orser(p, sizeof(uint16_t)); |
| 184 | + m_iodevice.input(p, sizeof(uint16_t)); |
| 185 | + |
| 186 | + // 存入字符串 |
| 187 | + if (len == 0) return; |
| 188 | + char* d = new char[len]; |
| 189 | + memcpy(d, in.c_str(), len); |
| 190 | + m_iodevice.input(d, len); |
| 191 | + delete [] d; |
| 192 | +} |
| 193 | + |
| 194 | +template<> |
| 195 | +inline void Serializer::input_type(const char* in) |
| 196 | +{ |
| 197 | + input_type<std::string>(std::string(in)); |
| 198 | +} |
0 commit comments