-
Notifications
You must be signed in to change notification settings - Fork 2
/
Encoder.hpp
75 lines (61 loc) · 2.57 KB
/
Encoder.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
// Copyright 2021 Rainer Schoenberger
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <inttypes.h>
#include <string.h>
namespace ZCMessagePack
{
class Encoder
{
public:
/// Constructs the encoder.
/// The given buffer f_out_borrow_messageBuffer will be used to write
/// encoded data to.
/// NOTE: The Encoder does not yet support generic memory backend.
/// (The Decoder does support it)
Encoder(uint8_t * f_out_borrow_messageBuffer, uint8_t f_bufferSize);
/// Encodes an unsigned integer into the buffer.
bool addUint(uint32_t f_number);
// EXPERIMENTAL
bool addInt(int32_t f_number);
/// Encodes a string into the buffer.
/// f_string needs to be null terminated.
bool addString(const char * f_string);
/// Encodes given binary data into the buffer.
bool addBinary(const uint8_t * f_data, uint8_t f_size);
/// Encodes given boolean value into the buffer.
bool addBool(bool f_value);
/// Encodes "Nil" into the buffer.
bool addNil();
/// Add a Map header for the given number of elements.
/// NOTE: The user needs to ensure that the resulting message is well formed.
/// After this header the following tuple needs to be added
/// f_numElements times:
/// - a string
/// - any MessagePack element
bool addMap(uint8_t f_numElements);
/// Add an Array header for the given number of elements.
/// NOTE: The user needs to ensure that the resulting message is well formed.
/// After this header f_numElements need to be encoded.
bool addArray(uint8_t f_numElements);
/// Returns the size of the encoded message
uint8_t getMessageSize() const;
private:
uint8_t sizeLeft();
bool addNestedStructure(uint8_t f_numElements, uint8_t f_smallPrefix, uint8_t f_bigPrefix);
uint8_t * m_messageBuffer;
uint8_t m_bufferSize;
uint8_t m_position = 0;
};
}