SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Serializable.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #ifndef swcdb_core_comm_Serializable_h
8 #define swcdb_core_comm_Serializable_h
9 
10 
11 #include "swcdb/core/Exception.h"
13 
14 
15 namespace SWC { namespace Comm {
16 
17 
18 class Serializable {
19 
20  public:
21 
22  size_t encoded_length() const;
23 
24  void encode(uint8_t** bufp) const;
25 
26  void decode(const uint8_t** bufp, size_t* remainp);
27 
28 
29  protected:
30 
31  virtual ~Serializable() noexcept { };
32 
33  virtual size_t internal_encoded_length() const = 0;
34 
35  virtual void internal_encode(uint8_t** bufp) const = 0;
36 
37  virtual void internal_decode(const uint8_t** bufp, size_t* remainp) = 0;
38 
39 
40 };
41 
42 
44 extern
48 }
49 
51 extern
52 void Serializable::encode(uint8_t** bufp) const {
54  internal_encode(bufp);
55 }
56 
58 extern
59 void Serializable::decode(const uint8_t** bufp, size_t* remainp) {
60  size_t len = Serialization::decode_vi32(bufp, remainp);
61  if(len > *remainp)
63  "Buffer-Trunclated remain=" SWC_FMT_LU " len=" SWC_FMT_LU,
64  *remainp, len);
65  *remainp -= len;
66 
67  const uint8_t* end = *bufp + len;
68  internal_decode(bufp, &len);
69 
70  if(len)
72  "Bad Decode missing=" SWC_FMT_LU " in buffer",
73  len);
74  if(*bufp > end)
76  "Bad Decode buffer overrun by=" SWC_FMT_LD,
77  *bufp - end);
78 }
79 
80 
81 }} // namespace SWC::Comm
82 
83 
84 
85 #endif // swcdb_core_comm_Serializable_h
SWC::Comm::Serializable::encoded_length
size_t encoded_length() const
Definition: Serializable.h:45
SWC::Serialization::encoded_length_vi32
constexpr SWC_CAN_INLINE uint8_t encoded_length_vi32(uint32_t val) noexcept
Definition: Serialization.h:234
SWC::Comm::Serializable::encode
void encode(uint8_t **bufp) const
Definition: Serializable.h:52
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
Exception.h
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Comm::Serializable::internal_decode
virtual void internal_decode(const uint8_t **bufp, size_t *remainp)=0
Serialization.h
SWC_THROWF
#define SWC_THROWF(_code_, _fmt_,...)
Definition: Exception.h:136
SWC::Comm::Serializable
Definition: Serializable.h:18
SWC::Comm::Protocol::FsBroker::Handler::length
void length(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: Length.h:17
SWC_FMT_LU
#define SWC_FMT_LU
Definition: Compat.h:98
SWC::Comm::Serializable::internal_encoded_length
virtual size_t internal_encoded_length() const =0
SWC::Serialization::encode_vi32
constexpr SWC_CAN_INLINE void encode_vi32(uint8_t **bufp, uint32_t val)
Definition: Serialization.h:243
SWC::Comm::Serializable::decode
void decode(const uint8_t **bufp, size_t *remainp)
Definition: Serializable.h:59
SWC::Comm::Serializable::internal_encode
virtual void internal_encode(uint8_t **bufp) const =0
SWC_FMT_LD
#define SWC_FMT_LD
Definition: Compat.h:99
SWC::Comm::Serializable::~Serializable
virtual ~Serializable() noexcept
Definition: Serializable.h:31
SWC::Error::PROTOCOL_ERROR
@ PROTOCOL_ERROR
Definition: Error.h:70
SWC::Serialization::decode_vi32
constexpr SWC_CAN_INLINE uint32_t decode_vi32(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:254