Usermode/AxWin4 - Send mouse/keyboard events to client
[tpg/acess2.git] / Usermode / Applications / axwin4_src / Common / include / serialisation.hpp
1 /*
2  * Acess2 GUI v4
3  * - By John Hodge (thePowersGang) 
4  *
5  * serialisation.hpp
6  * - Generic (de)serialisation code
7  */
8 #ifndef _SERIALISATION_H_
9 #define _SERIALISATION_H_
10
11 #include <cstdint>
12 #include <cstddef>
13 #include <exception>
14 #include <string>
15 #include <vector>
16
17 namespace AxWin {
18
19 class CDeserialiseException:
20         public ::std::exception
21 {
22 };
23
24 class CDeserialiser
25 {
26         ::std::vector<uint8_t>  m_vect;
27         size_t  m_offset;
28 public:
29         CDeserialiser():
30                 CDeserialiser(::std::vector<uint8_t>())
31         {}
32         CDeserialiser(const ::std::vector<uint8_t>& vect);
33         CDeserialiser(::std::vector<uint8_t>&& vect);
34         CDeserialiser(const CDeserialiser& x) { *this = x; };
35         CDeserialiser& operator=(const CDeserialiser& x);
36         bool    IsConsumed() const;
37         ::uint8_t       ReadU8();
38         ::uint16_t      ReadU16();
39         ::int16_t       ReadS16();
40         ::uint32_t      ReadU32();
41         ::uint64_t      ReadU64();
42         const ::std::vector<uint8_t>    ReadBuffer();
43         const ::std::string     ReadString();
44 private:
45         void RangeCheck(const char *Method, size_t bytes) throw(::std::out_of_range);
46 };
47
48 class CSerialiser
49 {
50         ::std::vector<uint8_t>  m_data;
51 public:
52         CSerialiser();
53         void WriteU8(::uint8_t val);
54         void WriteU16(::uint16_t val);
55         void WriteS16(::int16_t val);
56         void WriteU32(::uint32_t val);
57         void WriteU64(::uint64_t val);
58         void WriteBuffer(size_t n, const void* val);
59         void WriteString(const char* val, size_t n);
60         void WriteString(const char* val) {
61                 WriteString(val, ::std::char_traits<char>::length(val));
62         }
63         void WriteString(const ::std::string& val) {
64                 WriteString(val.data(), val.size());
65         }
66         void WriteSub(const CSerialiser& val);
67         
68         const ::std::vector<uint8_t>& Compact();
69 };
70
71 };
72
73 #endif
74

UCC git Repository :: git.ucc.asn.au