forstio/source/forstio/message.h

563 lines
14 KiB
C
Raw Normal View History

#pragma once
#include <cstdint>
#include <tuple>
2021-12-05 16:17:23 +01:00
#include <type_traits>
#include <variant>
2021-05-09 15:19:09 +02:00
#include <vector>
2021-12-21 16:27:29 +01:00
#include <cassert>
#include "common.h"
2021-12-18 17:26:14 +01:00
#include "message_container.h"
2021-11-29 15:08:18 +01:00
#include "schema.h"
#include "string_literal.h"
2021-12-29 19:26:22 +01:00
namespace saw {
2021-11-29 15:08:18 +01:00
class MessageBase {
protected:
bool set_explicitly = false;
public:
2021-11-29 15:08:18 +01:00
template <class T> T &as() {
static_assert(std::is_base_of<MessageBase, T>());
return dynamic_cast<T &>(*this);
}
2021-11-29 15:08:18 +01:00
template <class T> const T &as() const {
static_assert(std::is_base_of<MessageBase, T>());
return dynamic_cast<const T &>(*this);
}
};
/*
2021-11-29 15:08:18 +01:00
* Representing all message types
2021-12-05 16:17:23 +01:00
* Description which type to use happens through the use of the schema classes
2021-12-12 19:46:49 +01:00
* in schema.h The message classes are wrapper classes which store the data
* according to the specified container class.
2021-12-12 19:14:52 +01:00
*
2021-12-12 19:46:49 +01:00
* The reader and builder classe exist to create some clarity while implementing
* parsers. Also minor guarantess are provided if the message is used as a
* template parameter.
*/
2021-12-05 18:21:50 +01:00
/*
* Struct Message
*/
2021-11-29 15:08:18 +01:00
template <class... V, StringLiteral... Keys, class Container>
class Message<schema::Struct<schema::NamedMember<V, Keys>...>, Container> final
: public MessageBase {
private:
2021-12-05 16:17:23 +01:00
using SchemaType = schema::Struct<schema::NamedMember<V, Keys>...>;
using MessageType = Message<SchemaType, Container>;
2021-11-29 15:08:18 +01:00
Container container;
2021-12-05 16:17:23 +01:00
static_assert(std::is_same_v<SchemaType, typename Container::SchemaType>,
2021-12-14 11:26:25 +01:00
"Container should have same the schema as Message");
friend class Builder;
friend class Reader;
public:
class Reader;
class Builder {
private:
2021-11-29 15:08:18 +01:00
MessageType &message;
public:
2021-11-29 15:08:18 +01:00
Builder(MessageType &msg) : message{msg} {}
2021-12-18 20:54:51 +01:00
Reader asReader() { return Reader{message}; }
2021-12-05 18:21:50 +01:00
/*
* Initialize a member by index
*/
template <size_t i>
2021-12-27 18:15:42 +01:00
typename std::enable_if<
!SchemaIsArray<
typename MessageParameterPackType<i, V...>::Type>::Value,
typename Container::template ElementType<i>::Builder>::type
init() {
return typename Container::template ElementType<i>::Builder{
2021-12-18 20:54:51 +01:00
message.container.template get<i>()};
}
2021-12-05 18:21:50 +01:00
/*
* Initialize a member by their name
* This is the preferred method for schema::Struct messages
*/
2021-12-05 16:17:23 +01:00
template <StringLiteral Literal>
2021-12-27 18:15:42 +01:00
typename std::enable_if<
!SchemaIsArray<typename MessageParameterPackType<
MessageParameterKeyPackIndex<Literal, Keys...>::Value,
V...>::Type>::Value,
typename Container::template ElementType<
MessageParameterKeyPackIndex<Literal,
Keys...>::Value>::Builder>::type
2021-12-12 19:46:49 +01:00
init() {
2021-12-05 16:17:23 +01:00
constexpr size_t i =
2021-12-18 20:54:51 +01:00
MessageParameterKeyPackIndex<Literal, Keys...>::Value;
2021-11-29 15:08:18 +01:00
return init<i>();
}
2021-12-27 18:15:42 +01:00
template <size_t i>
typename std::enable_if<
SchemaIsArray<
typename MessageParameterPackType<i, V...>::Type>::Value,
typename Container::template ElementType<i>::Builder>::type
2021-12-29 10:52:37 +01:00
init(size_t size = 0) {
2021-12-27 18:15:42 +01:00
auto array_builder =
typename Container::template ElementType<i>::Builder{
message.container.template get<i>(), size};
return array_builder;
}
/*
* Version for array schema type
*/
template <StringLiteral Literal>
typename std::enable_if<
SchemaIsArray<typename MessageParameterPackType<
MessageParameterKeyPackIndex<Literal, Keys...>::Value,
V...>::Type>::Value,
typename Container::template ElementType<
MessageParameterKeyPackIndex<Literal,
Keys...>::Value>::Builder>::type
init(size_t size) {
constexpr size_t i =
MessageParameterKeyPackIndex<Literal, Keys...>::Value;
return init<i>(size);
}
};
class Reader {
private:
2021-11-29 15:08:18 +01:00
MessageType &message;
public:
2021-11-29 15:08:18 +01:00
Reader(MessageType &msg) : message{msg} {}
2021-12-05 18:21:50 +01:00
Builder asBuilder() { return Builder{message}; }
2021-12-05 18:21:50 +01:00
/*
* Get member by index
*/
template <size_t i>
typename Container::template ElementType<i>::Reader get() {
return typename Container::template ElementType<i>::Reader{
2021-12-18 20:54:51 +01:00
message.container.template get<i>()};
}
2021-12-05 18:21:50 +01:00
/*
* Get member by name
* This is the preferred method for schema::Struct messages
*/
2021-12-05 16:17:23 +01:00
template <StringLiteral Literal>
typename Container::template ElementType<
2021-12-18 20:54:51 +01:00
MessageParameterKeyPackIndex<Literal, Keys...>::Value>::Reader
2021-12-12 19:46:49 +01:00
get() {
2021-12-12 19:14:52 +01:00
// The index of the first match
2021-12-05 16:17:23 +01:00
constexpr size_t i =
2021-12-18 20:54:51 +01:00
MessageParameterKeyPackIndex<Literal, Keys...>::Value;
2021-11-29 15:08:18 +01:00
return get<i>();
}
};
2021-12-17 16:16:59 +01:00
2021-12-18 14:25:02 +01:00
Builder build() { return Builder{*this}; }
2021-12-17 16:16:59 +01:00
2021-12-18 14:25:02 +01:00
Reader read() { return Reader{*this}; }
};
2021-12-12 02:04:32 +01:00
/*
2021-12-12 19:46:49 +01:00
* Union message class. Wrapper object
2021-12-12 02:04:32 +01:00
*/
2021-12-12 19:46:49 +01:00
template <class... V, StringLiteral... Keys, class Container>
class Message<schema::Union<schema::NamedMember<V, Keys>...>, Container> final
: public MessageBase {
2021-12-12 02:04:32 +01:00
private:
using SchemaType = schema::Union<schema::NamedMember<V, Keys>...>;
using MessageType = Message<SchemaType, Container>;
Container container;
static_assert(std::is_same_v<SchemaType, typename Container::SchemaType>,
2021-12-14 11:26:25 +01:00
"Container should have same the schema as Message");
2021-12-12 02:04:32 +01:00
friend class Builder;
friend class Reader;
public:
class Reader;
class Builder {
private:
2021-12-12 19:46:49 +01:00
MessageType &message;
2021-12-12 02:04:32 +01:00
public:
2021-12-12 19:46:49 +01:00
Builder(MessageType &msg) : message{msg} {}
2021-12-12 02:04:32 +01:00
2021-12-12 19:46:49 +01:00
Reader asReader() { return Reader{message}; }
template <size_t i>
2021-12-27 18:15:42 +01:00
typename std::enable_if<
!SchemaIsArray<
typename MessageParameterPackType<i, V...>::Type>::Value,
typename Container::template ElementType<i>::Builder>::type
init() {
return typename Container::template ElementType<i>::Builder{
2021-12-18 20:54:51 +01:00
message.container.template get<i>()};
2021-12-12 12:17:10 +01:00
}
template <StringLiteral Literal>
2021-12-27 18:15:42 +01:00
typename std::enable_if<
!SchemaIsArray<typename MessageParameterPackType<
MessageParameterKeyPackIndex<Literal, Keys...>::Value,
V...>::Type>::Value,
typename Container::template ElementType<
MessageParameterKeyPackIndex<Literal,
Keys...>::Value>::Builder>::type
init() {
constexpr size_t i =
MessageParameterKeyPackIndex<Literal, Keys...>::Value;
return init<i>();
}
2021-12-27 18:15:42 +01:00
/*
* If Schema is Array
*/
template <size_t i>
typename std::enable_if<
SchemaIsArray<
typename MessageParameterPackType<i, V...>::Type>::Value,
typename Container::template ElementType<i>::Builder>::type
2021-12-29 10:52:37 +01:00
init(size_t size = 0) {
2021-12-27 18:15:42 +01:00
return typename Container::template ElementType<i>::Builder{
message.container.template get<i>(), size};
}
template <StringLiteral Literal>
typename std::enable_if<
SchemaIsArray<typename MessageParameterPackType<
MessageParameterKeyPackIndex<Literal, Keys...>::Value,
V...>::Type>::Value,
typename Container::template ElementType<
MessageParameterKeyPackIndex<Literal,
Keys...>::Value>::Builder>::type
init(size_t size) {
constexpr size_t i =
MessageParameterKeyPackIndex<Literal, Keys...>::Value;
return init<i>(size);
}
2021-12-12 02:04:32 +01:00
};
class Reader {
private:
2021-12-12 19:46:49 +01:00
MessageType &message;
2021-12-12 02:04:32 +01:00
public:
2021-12-12 19:46:49 +01:00
Reader(MessageType &msg) : message{msg} {}
2021-12-12 02:04:32 +01:00
2021-12-12 19:46:49 +01:00
Builder asBuilder() { return Builder{message}; }
2021-12-12 02:04:32 +01:00
template <size_t i>
typename Container::template ElementType<i>::Reader get() {
return typename Container::template ElementType<i>::Reader{
2021-12-18 20:54:51 +01:00
message.container.template get<i>()};
2021-12-12 12:17:10 +01:00
}
2021-12-12 19:46:49 +01:00
template <StringLiteral Literal>
typename Container::template ElementType<
MessageParameterKeyPackIndex<Literal, Keys...>::Value>::Reader
get() {
// The index of the first match
constexpr size_t i =
MessageParameterKeyPackIndex<Literal, Keys...>::Value;
return get<i>();
}
template <StringLiteral Literal>
constexpr size_t toIndex() const noexcept {
2021-12-18 20:54:51 +01:00
return MessageParameterKeyPackIndex<Literal, Keys...>::Value;
2021-12-12 12:17:10 +01:00
}
size_t index() const noexcept { return message.container.index(); }
template <StringLiteral Literal> bool hasAlternative() const {
return index() == toIndex<Literal>();
}
2021-12-12 12:17:10 +01:00
};
};
2021-12-12 19:14:52 +01:00
/*
* Array message class. Wrapper around an array schema element
*/
2021-12-27 18:15:42 +01:00
template <class T, class Container>
2021-12-12 19:14:52 +01:00
class Message<schema::Array<T>, Container> final : public MessageBase {
2021-12-12 12:17:10 +01:00
private:
2021-12-12 19:14:52 +01:00
using SchemaType = schema::Array<T>;
2021-12-12 12:17:10 +01:00
using MessageType = Message<SchemaType, Container>;
2021-12-12 19:46:49 +01:00
2021-12-12 12:17:10 +01:00
Container container;
static_assert(std::is_same_v<SchemaType, typename Container::SchemaType>,
"Container should have same Schema as Message");
friend class Builder;
friend class Reader;
public:
class Reader;
class Builder {
private:
2021-12-27 18:15:42 +01:00
MessageType &message;
2021-12-12 12:17:10 +01:00
public:
2021-12-27 18:15:42 +01:00
Builder(MessageType &msg, size_t size) : message{msg} {
if (size > 0) {
message.container.resize(size);
}
}
2021-12-12 12:17:10 +01:00
2021-12-27 18:15:42 +01:00
Reader asReader() { return Reader{message}; }
2021-12-12 02:04:32 +01:00
2021-12-27 18:15:42 +01:00
typename Container::ElementType::Builder init(size_t i) {
return typename Container::ElementType::Builder{
2021-12-27 18:15:42 +01:00
message.container.get(i)};
2021-12-12 12:17:10 +01:00
}
2021-12-27 18:15:42 +01:00
size_t size() const { return message.container.size(); }
2021-12-29 10:52:37 +01:00
void resize(size_t size) { message.container.resize(size); }
2021-12-12 12:17:10 +01:00
};
2021-12-12 19:46:49 +01:00
2021-12-12 12:17:10 +01:00
class Reader {
private:
2021-12-27 18:15:42 +01:00
MessageType &message;
2021-12-12 12:17:10 +01:00
public:
2021-12-27 18:15:42 +01:00
Reader(MessageType &msg) : message{msg} {}
2021-12-12 12:17:10 +01:00
2021-12-27 18:15:42 +01:00
Builder asBuilder() { return Builder{message, 0}; }
2021-12-12 12:17:10 +01:00
2021-12-27 18:15:42 +01:00
typename Container::ElementType::Reader get(size_t i) {
return typename Container::ElementType::Reader{
message.container.get(i)};
2021-12-12 12:17:10 +01:00
}
2021-12-27 18:15:42 +01:00
size_t size() const { return message.container.size(); }
2021-12-12 02:04:32 +01:00
};
};
2021-12-12 19:14:52 +01:00
/*
* Tuple message class. Wrapper around a tuple schema
*/
2021-12-18 20:54:51 +01:00
template <class... T, class Container>
2021-12-12 19:14:52 +01:00
class Message<schema::Tuple<T...>, Container> final : public MessageBase {
private:
using SchemaType = schema::Tuple<T...>;
using MessageType = Message<SchemaType, Container>;
2021-12-12 19:46:49 +01:00
2021-12-12 19:14:52 +01:00
Container container;
static_assert(std::is_same_v<SchemaType, typename Container::SchemaType>,
2021-12-14 11:26:25 +01:00
"Container should have same the schema as Message");
2021-12-12 19:14:52 +01:00
friend class Builder;
friend class Reader;
2021-12-12 19:46:49 +01:00
2021-12-12 19:14:52 +01:00
public:
class Reader;
class Builder {
2021-12-12 19:46:49 +01:00
MessageType &message;
2021-12-12 19:14:52 +01:00
public:
2021-12-12 19:46:49 +01:00
Builder(MessageType &msg) : message{msg} {}
2021-12-12 19:14:52 +01:00
2021-12-12 19:46:49 +01:00
Reader asReader() { return Reader{message}; }
2021-12-12 19:14:52 +01:00
template <size_t i>
2021-12-27 18:39:47 +01:00
typename std::enable_if<
!SchemaIsArray<
typename MessageParameterPackType<i, T...>::Type>::Value,
typename Container::template ElementType<i>::Builder>::type
init() {
return typename Container::template ElementType<i>::Builder{
2021-12-18 20:54:51 +01:00
message.container.template get<i>()};
2021-12-12 19:14:52 +01:00
}
2021-12-27 18:39:47 +01:00
template <size_t i>
typename std::enable_if<
SchemaIsArray<
typename MessageParameterPackType<i, T...>::Type>::Value,
typename Container::template ElementType<i>::Builder>::type
2021-12-29 10:52:37 +01:00
init(size_t size = 0) {
2021-12-27 18:39:47 +01:00
return typename Container::template ElementType<i>::Builder{
message.container.template get<i>(), size};
}
2021-12-12 19:14:52 +01:00
};
class Reader {
private:
2021-12-12 19:46:49 +01:00
MessageType &message;
2021-12-12 19:14:52 +01:00
public:
2021-12-12 19:46:49 +01:00
Reader(MessageType &msg) : message{msg} {}
2021-12-12 19:14:52 +01:00
2021-12-12 19:46:49 +01:00
Builder asBuilder() { return Builder{message}; }
2021-12-12 19:14:52 +01:00
template <size_t i>
typename Container::template ElementType<i>::Reader get() {
return typename Container::template ElementType<i>::Reader{
2021-12-18 20:54:51 +01:00
message.container.template get<i>()};
2021-12-12 19:14:52 +01:00
}
};
};
2021-12-12 02:04:32 +01:00
/*
2021-12-12 19:46:49 +01:00
* Primitive type (float, double, uint8_t, uint16_t, uint32_t, uint64_t, int8_t,
* int16_t, int32_t, int64_t) message class
2021-12-12 02:04:32 +01:00
*/
2021-12-05 16:17:23 +01:00
template <class T, size_t N, class Container>
class Message<schema::Primitive<T, N>, Container> final : public MessageBase {
private:
2021-12-05 18:21:50 +01:00
using SchemaType = schema::Primitive<T, N>;
using MessageType = Message<SchemaType, Container>;
Container container;
static_assert(std::is_same_v<SchemaType, typename Container::SchemaType>,
2021-12-14 11:26:25 +01:00
"Container should have same the schema as Message");
2021-12-05 18:21:50 +01:00
friend class Builder;
friend class Reader;
2021-12-12 19:46:49 +01:00
2021-12-05 18:21:50 +01:00
public:
class Reader;
class Builder {
private:
MessageType &message;
public:
2021-12-12 19:46:49 +01:00
Builder(MessageType &msg) : message{msg} {}
2021-12-05 18:21:50 +01:00
Reader asReader() { return Reader{message}; }
2021-12-21 16:27:29 +01:00
void set(const typename Container::ValueType &value) {
message.container.set(value);
}
2021-12-05 18:21:50 +01:00
};
class Reader {
private:
MessageType &message;
public:
2021-12-12 19:46:49 +01:00
Reader(Message &msg) : message{msg} {}
2021-12-05 18:21:50 +01:00
Builder asBuilder() { return Builder{message}; }
2021-12-21 16:27:29 +01:00
const typename Container::ValueType &get() const {
return message.container.get();
}
2021-12-05 18:21:50 +01:00
};
};
template <class Container>
class Message<schema::String, Container> final : public MessageBase {
private:
using SchemaType = schema::String;
using MessageType = Message<SchemaType, Container>;
2021-11-29 15:08:18 +01:00
Container container;
2021-12-05 18:21:50 +01:00
static_assert(std::is_same_v<SchemaType, typename Container::SchemaType>,
2021-12-14 11:26:25 +01:00
"Container should have same the schema as Message");
2021-12-05 18:21:50 +01:00
friend class Builder;
friend class Reader;
2021-12-12 19:46:49 +01:00
2021-12-05 16:17:23 +01:00
public:
2021-12-05 18:21:50 +01:00
class Reader;
class Builder {
private:
MessageType &message;
public:
2021-12-12 19:46:49 +01:00
Builder(MessageType &msg) : message{msg} {}
2021-12-05 18:21:50 +01:00
Reader asReader() { return Reader{message}; }
2021-12-27 16:08:35 +01:00
void set(std::string &&str) { message.container.set(std::move(str)); }
void set(const std::string_view str) { message.container.set(str); }
void set(const char *str) { set(std::string_view{str}); }
2021-12-05 18:21:50 +01:00
};
class Reader {
private:
MessageType &message;
public:
2021-12-12 19:46:49 +01:00
Reader(MessageType &msg) : message{msg} {}
2021-12-05 18:21:50 +01:00
Builder asBuilder() { return Builder{message}; }
2021-12-27 16:20:35 +01:00
const std::string_view get() const { return message.container.get(); }
2021-12-18 20:54:51 +01:00
};
};
2021-12-18 14:25:02 +01:00
template <class Schema, class Container = MessageContainer<Schema>>
2021-12-18 14:24:06 +01:00
class HeapMessageRoot {
private:
2021-12-18 14:24:06 +01:00
Own<Message<Schema, Container>> root;
2021-12-18 14:25:02 +01:00
public:
2021-12-18 14:25:02 +01:00
HeapMessageRoot(Own<Message<Schema, Container>> r) : root{std::move(r)} {}
2021-12-18 14:25:02 +01:00
typename Message<Schema, Container>::Builder build() {
2021-12-18 14:24:06 +01:00
assert(root);
2021-12-21 16:27:29 +01:00
return typename Message<Schema, Container>::Builder{*root};
2021-12-18 14:24:06 +01:00
}
2021-12-18 14:25:02 +01:00
typename Message<Schema, Container>::Reader read() {
2021-12-18 14:24:06 +01:00
assert(root);
2021-12-21 16:27:29 +01:00
return typename Message<Schema, Container>::Reader{*root};
}
};
2021-12-27 18:15:42 +01:00
template <class T, class Container>
class HeapMessageRoot<schema::Array<T>, Container> {
public:
using Schema = schema::Array<T>;
private:
Own<Message<Schema, Container>> root;
public:
HeapMessageRoot(Own<Message<Schema, Container>> r) : root{std::move(r)} {}
typename Message<Schema, Container>::Builder build(size_t size) {
assert(root);
return typename Message<Schema, Container>::Builder{*root, size};
}
typename Message<Schema, Container>::Reader read() {
assert(root);
return typename Message<Schema, Container>::Reader{*root};
}
};
2021-12-18 14:24:06 +01:00
/*
* Minor helper for creating a message root
*/
2021-12-18 14:25:02 +01:00
template <class Schema, class Container = MessageContainer<Schema>>
2021-12-19 17:11:57 +01:00
inline HeapMessageRoot<Schema, Container> heapMessageRoot() {
2021-12-18 14:24:06 +01:00
Own<Message<Schema, Container>> root = heap<Message<Schema, Container>>();
return HeapMessageRoot<Schema, Container>{std::move(root)};
}
2021-12-29 19:26:22 +01:00
} // namespace saw