RavEngine
Loading...
Searching...
No Matches
RPCMsgUnpacker.hpp
1#pragma once
2#include <string>
3#include <optional>
4#include <cstring>
5
6namespace RavEngine {
8 std::string message;
9 uint32_t offset = header_size; //advance past the RPC message header
10
11 template<typename T>
12 inline T Deserialize() const{
13 T val;
14 std::memcpy(&val, message.data() + offset+sizeof(ctti_t), SerializedSize<T>());
15 return val;
16 }
17
18 public:
19 static constexpr size_t code_offset = 16 + 1;
20 static constexpr size_t header_size = code_offset + sizeof(uint16_t); //uuid, command code, method ID
21
22 RPCMsgUnpacker(const std::string& msg) : message(msg) {}
23
24 template<typename T>
25 constexpr inline std::optional<T> Get() {
26 std::optional<T> result;
27 //is the current parameter the same type as T?
28 ctti_t enc_type = 0;
29 std::memcpy(&enc_type,message.data()+offset,sizeof(enc_type));
30 if (enc_type == CTTI<T>()){
31 //deserialize the type using template specialization
32 auto val = Deserialize<T>();
33
34 //advance the offset pointer
36
37 //emplace into the optional
38 result.emplace(std::move(val));
39 }
40
41 return result;
42 }
43
44 template<typename T>
45 static inline constexpr size_t SerializedSize(){
46 return sizeof(T);
47 }
48
49 template<typename T>
50 static inline constexpr size_t TotalSerializedSize(){
51 return sizeof(CTTI<T>()) + SerializedSize<T>(); //encode what type it is + its size
52 }
53 };
54}
Definition RPCMsgUnpacker.hpp:7
Definition concurrentqueue.h:747
Definition Animation.hpp:6