C/C++ API Reference
Loading...
Searching...
No Matches
encode_args.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <stdarg.h>
17#include <stddef.h>
18#include <stdint.h>
19
20#include "pw_polyfill/standard.h"
21#include "pw_preprocessor/util.h"
22#include "pw_tokenizer/internal/argument_types.h"
23#include "pw_varint/varint.h"
24
25#ifdef __cplusplus
26
27#include <cstring>
28
29#include "pw_polyfill/standard.h"
30#include "pw_span/span.h"
31#include "pw_tokenizer/config.h"
32#include "pw_tokenizer/tokenize.h"
33
34namespace pw::tokenizer {
35namespace internal {
36
37// Returns the maximum encoded size of an argument of the specified type.
38template <typename T>
39constexpr size_t ArgEncodedSizeBytes() {
40 constexpr pw_tokenizer_ArgTypes kType = VarargsType<T>();
41 if constexpr (kType == PW_TOKENIZER_ARG_TYPE_DOUBLE) {
42 return sizeof(float);
43 } else if constexpr (kType == PW_TOKENIZER_ARG_TYPE_STRING) {
44 return 1; // Size of the length byte only
45 } else if constexpr (kType == PW_TOKENIZER_ARG_TYPE_INT64) {
46 return 10; // Max size of a varint-encoded 64-bit integer
47 } else if constexpr (kType == PW_TOKENIZER_ARG_TYPE_INT) {
48 return sizeof(T) + 1; // Max size of zig-zag varint integer <= 32-bits
49 } else {
50 static_assert(sizeof(T) != sizeof(T), "Unsupported argument type");
51 }
52}
53
54} // namespace internal
55
57
72template <typename... ArgTypes>
73constexpr size_t MinEncodingBufferSizeBytes() {
74 return (sizeof(pw_tokenizer_Token) + ... +
75 internal::ArgEncodedSizeBytes<ArgTypes>());
76}
77
84size_t EncodeArgs(pw_tokenizer_ArgTypes types,
85 va_list args,
86 span<std::byte> output);
87
108template <size_t kMaxSizeBytes>
110 public:
111 // Encodes a tokenized message to an internal buffer.
113 pw_tokenizer_ArgTypes types,
114 va_list args) {
115 std::memcpy(data_, &token, sizeof(token));
116 size_ =
117 sizeof(token) +
118 EncodeArgs(types, args, span<std::byte>(data_).subspan(sizeof(token)));
119 }
120
122 const std::byte* data() const { return data_; }
123
125 const uint8_t* data_as_uint8() const {
126 return reinterpret_cast<const uint8_t*>(data());
127 }
128
130 size_t size() const { return size_; }
131
132 private:
133 static_assert(kMaxSizeBytes >= sizeof(pw_tokenizer_Token),
134 "The encoding buffer must be at least large enough for a token "
135 "(4 bytes)");
136
137 std::byte data_[kMaxSizeBytes];
138 size_t size_;
139};
140
142
143} // namespace pw::tokenizer
144
145#endif // __cplusplus
146
147PW_EXTERN_C_START
148
150
153size_t pw_tokenizer_EncodeArgs(pw_tokenizer_ArgTypes types,
154 va_list args,
155 void* output_buffer,
156 size_t output_buffer_size);
157
160static inline size_t pw_tokenizer_EncodeInt(int value,
161 void* output,
162 size_t output_size_bytes) {
163 return pw_varint_Encode32(
164 pw_varint_ZigZagEncode32(value), output, output_size_bytes);
165}
166
169static inline size_t pw_tokenizer_EncodeInt64(int64_t value,
170 void* output,
171 size_t output_size_bytes) {
172 return pw_varint_Encode64(
173 pw_varint_ZigZagEncode64(value), output, output_size_bytes);
174}
175
177
178PW_EXTERN_C_END
Definition: span_impl.h:235
Definition: encode_args.h:109
const std::byte * data() const
The binary-encoded tokenized message.
Definition: encode_args.h:122
size_t size() const
The size of the encoded tokenized message in bytes.
Definition: encode_args.h:130
const uint8_t * data_as_uint8() const
Returns data() as a pointer to uint8_t instead of std::byte.
Definition: encode_args.h:125
size_t pw_tokenizer_EncodeArgs(pw_tokenizer_ArgTypes types, va_list args, void *output_buffer, size_t output_buffer_size)
uint32_t pw_tokenizer_Token
Definition: tokenize.h:41
size_t EncodeArgs(pw_tokenizer_ArgTypes types, va_list args, span< std::byte > output)
static size_t pw_tokenizer_EncodeInt(int value, void *output, size_t output_size_bytes)
Definition: encode_args.h:160
static size_t pw_tokenizer_EncodeInt64(int64_t value, void *output, size_t output_size_bytes)
Definition: encode_args.h:169
constexpr size_t MinEncodingBufferSizeBytes()
Definition: encode_args.h:73
size_t pw_varint_Encode64(uint64_t integer, void *output, size_t output_size_bytes)
static uint32_t pw_varint_ZigZagEncode32(int32_t n)
Zig-zag encodes an int32_t, returning it as a uint32_t.
Definition: varint.h:65
size_t pw_varint_Encode32(uint32_t integer, void *output, size_t output_size_bytes)
static uint64_t pw_varint_ZigZagEncode64(int64_t n)
Zig-zag encodes an int64_t, returning it as a uint64_t.
Definition: varint.h:70