1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/common/include/villas/buffer.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

38 lines
916 B
C++
Raw Permalink Normal View History

/* A simple buffer for encoding streamed JSON messages.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <vector>
#include <cstdlib>
#include <jansson.h>
2019-10-27 20:23:47 +01:00
namespace villas {
class Buffer : public std::vector<char> {
2019-10-27 20:23:47 +01:00
protected:
static int callback(const char *data, size_t len, void *ctx);
public:
Buffer(const char *buf, size_type len) : std::vector<char>(buf, buf + len) {}
Buffer(size_type count = 0) : std::vector<char>(count, 0) {}
// Encode JSON document /p j and append it to the buffer
int encode(json_t *j, int flags = 0);
// Decode JSON document from the beginning of the buffer
json_t *decode();
2019-10-27 20:23:47 +01:00
void append(const char *data, size_t len) { insert(end(), data, data + len); }
2019-10-27 20:23:47 +01:00
};
} // namespace villas