563 lines
14 KiB
C++
563 lines
14 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <tuple>
|
|
#include <type_traits>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
#include <cassert>
|
|
|
|
#include "common.h"
|
|
|
|
#include "message_container.h"
|
|
#include "schema.h"
|
|
#include "string_literal.h"
|
|
|
|
namespace saw {
|
|
class message_base {
|
|
protected:
|
|
bool set_explicitly_ = false;
|
|
|
|
public:
|
|
template <class T> T &as() {
|
|
static_assert(std::is_base_of<message_base, T>());
|
|
return dynamic_cast<T &>(*this);
|
|
}
|
|
|
|
template <class T> const T &as() const {
|
|
static_assert(std::is_base_of<message_base, T>());
|
|
return dynamic_cast<const T &>(*this);
|
|
}
|
|
};
|
|
/*
|
|
* Representing all message types
|
|
* Description which type to use happens through the use of the schema classes
|
|
* in schema.h The message classes are wrapper classes which store the data
|
|
* according to the specified container class.
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
/*
|
|
* Struct Message
|
|
*/
|
|
template <class... V, string_literal... Keys, class Container>
|
|
class message<schema::Struct<schema::NamedMember<V, Keys>...>, Container> final
|
|
: public message_base {
|
|
private:
|
|
using SchemaType = schema::Struct<schema::NamedMember<V, Keys>...>;
|
|
using MessageType = message<SchemaType, Container>;
|
|
Container container_;
|
|
|
|
static_assert(std::is_same_v<SchemaType, typename Container::SchemaType>,
|
|
"Container should have same the schema as Message");
|
|
|
|
friend class builder;
|
|
friend class reader;
|
|
|
|
public:
|
|
class reader;
|
|
class builder {
|
|
private:
|
|
MessageType &message_;
|
|
|
|
public:
|
|
builder(MessageType &msg) : message_{msg} {}
|
|
|
|
reader as_reader() { return reader{message_}; }
|
|
|
|
/*
|
|
* Initialize a member by index
|
|
*/
|
|
template <size_t i>
|
|
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{
|
|
message_.container_.template get<i>()};
|
|
}
|
|
|
|
/*
|
|
* Initialize a member by their name
|
|
* This is the preferred method for schema::Struct messages
|
|
*/
|
|
template <string_literal 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() {
|
|
constexpr size_t i =
|
|
MessageParameterKeyPackIndex<Literal, Keys...>::Value;
|
|
|
|
return init<i>();
|
|
}
|
|
|
|
template <size_t i>
|
|
typename std::enable_if<
|
|
SchemaIsArray<
|
|
typename MessageParameterPackType<i, V...>::Type>::Value,
|
|
typename Container::template ElementType<i>::builder>::type
|
|
init(size_t size = 0) {
|
|
auto array_builder =
|
|
typename Container::template ElementType<i>::builder{
|
|
message_.container_.template get<i>(), size};
|
|
return array_builder;
|
|
}
|
|
|
|
/*
|
|
* Version for array schema type
|
|
*/
|
|
template <string_literal 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:
|
|
MessageType &message_;
|
|
|
|
public:
|
|
reader(MessageType &msg) : message_{msg} {}
|
|
|
|
builder as_builder() { return builder{message_}; }
|
|
|
|
/*
|
|
* Get member by index
|
|
*/
|
|
template <size_t i>
|
|
typename Container::template ElementType<i>::reader get() {
|
|
return typename Container::template ElementType<i>::reader{
|
|
message_.container_.template get<i>()};
|
|
}
|
|
|
|
/*
|
|
* Get member by name
|
|
* This is the preferred method for schema::Struct messages
|
|
*/
|
|
template <string_literal 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>();
|
|
}
|
|
};
|
|
|
|
builder build() { return builder{*this}; }
|
|
|
|
reader read() { return reader{*this}; }
|
|
};
|
|
|
|
/*
|
|
* Union message class. Wrapper object
|
|
*/
|
|
template <class... V, string_literal... Keys, class Container>
|
|
class message<schema::Union<schema::NamedMember<V, Keys>...>, Container> final
|
|
: public message_base {
|
|
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>,
|
|
"Container should have same the schema as Message");
|
|
|
|
friend class builder;
|
|
friend class reader;
|
|
|
|
public:
|
|
class reader;
|
|
class builder {
|
|
private:
|
|
MessageType &message_;
|
|
|
|
public:
|
|
builder(MessageType &msg) : message_{msg} {}
|
|
|
|
reader as_reader() { return reader{message_}; }
|
|
|
|
template <size_t i>
|
|
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{
|
|
message_.container_.template get<i>()};
|
|
}
|
|
|
|
template <string_literal 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() {
|
|
constexpr size_t i =
|
|
MessageParameterKeyPackIndex<Literal, Keys...>::Value;
|
|
|
|
return init<i>();
|
|
}
|
|
|
|
/*
|
|
* 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
|
|
init(size_t size = 0) {
|
|
return typename Container::template ElementType<i>::builder{
|
|
message_.container_.template get<i>(), size};
|
|
}
|
|
|
|
template <string_literal 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:
|
|
MessageType &message_;
|
|
|
|
public:
|
|
reader(MessageType &msg) : message_{msg} {}
|
|
|
|
builder as_builder() { return builder{message_}; }
|
|
|
|
template <size_t i>
|
|
typename Container::template ElementType<i>::reader get() {
|
|
return typename Container::template ElementType<i>::reader{
|
|
message_.container_.template get<i>()};
|
|
}
|
|
|
|
template <string_literal 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 <string_literal Literal>
|
|
static constexpr size_t toIndex() noexcept {
|
|
return MessageParameterKeyPackIndex<Literal, Keys...>::Value;
|
|
}
|
|
|
|
size_t index() const noexcept { return message_.container_.index(); }
|
|
|
|
template <string_literal Literal> bool hasAlternative() const {
|
|
return index() == toIndex<Literal>();
|
|
}
|
|
};
|
|
};
|
|
|
|
/*
|
|
* Array message class. Wrapper around an array schema element
|
|
*/
|
|
template <class T, class Container>
|
|
class message<schema::Array<T>, Container> final : public message_base {
|
|
private:
|
|
using SchemaType = schema::Array<T>;
|
|
using MessageType = message<SchemaType, Container>;
|
|
|
|
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:
|
|
MessageType &message_;
|
|
|
|
public:
|
|
builder(MessageType &msg, size_t size) : message_{msg} {
|
|
if (size > 0) {
|
|
message_.container_.resize(size);
|
|
}
|
|
}
|
|
|
|
reader as_reader() { return reader{message_}; }
|
|
|
|
typename Container::ElementType::builder init(size_t i) {
|
|
return typename Container::ElementType::builder{
|
|
message_.container_.get(i)};
|
|
}
|
|
|
|
size_t size() const { return message_.container_.size(); }
|
|
|
|
void resize(size_t size) { message_.container_.resize(size); }
|
|
};
|
|
|
|
class reader {
|
|
private:
|
|
MessageType &message_;
|
|
|
|
public:
|
|
reader(MessageType &msg) : message_{msg} {}
|
|
|
|
builder as_builder() { return builder{message_, 0}; }
|
|
|
|
typename Container::ElementType::reader get(size_t i) {
|
|
return typename Container::ElementType::reader{
|
|
message_.container_.get(i)};
|
|
}
|
|
|
|
size_t size() const { return message_.container_.size(); }
|
|
};
|
|
};
|
|
|
|
/*
|
|
* Tuple message class. Wrapper around a tuple schema
|
|
*/
|
|
template <class... T, class Container>
|
|
class message<schema::Tuple<T...>, Container> final : public message_base {
|
|
private:
|
|
using SchemaType = schema::Tuple<T...>;
|
|
using MessageType = message<SchemaType, Container>;
|
|
|
|
Container container_;
|
|
|
|
static_assert(std::is_same_v<SchemaType, typename Container::SchemaType>,
|
|
"Container should have same the schema as Message");
|
|
|
|
friend class builder;
|
|
friend class reader;
|
|
|
|
public:
|
|
class reader;
|
|
class builder {
|
|
MessageType &message_;
|
|
|
|
public:
|
|
builder(MessageType &msg) : message_{msg} {}
|
|
|
|
reader as_reader() { return reader{message_}; }
|
|
|
|
template <size_t i>
|
|
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{
|
|
message_.container_.template get<i>()};
|
|
}
|
|
|
|
template <size_t i>
|
|
typename std::enable_if<
|
|
SchemaIsArray<
|
|
typename MessageParameterPackType<i, T...>::Type>::Value,
|
|
typename Container::template ElementType<i>::builder>::type
|
|
init(size_t size = 0) {
|
|
return typename Container::template ElementType<i>::builder{
|
|
message_.container_.template get<i>(), size};
|
|
}
|
|
};
|
|
class reader {
|
|
private:
|
|
MessageType &message_;
|
|
|
|
public:
|
|
reader(MessageType &msg) : message_{msg} {}
|
|
|
|
builder as_builder() { return builder{message_}; }
|
|
|
|
template <size_t i>
|
|
typename Container::template ElementType<i>::reader get() {
|
|
return typename Container::template ElementType<i>::reader{
|
|
message_.container_.template get<i>()};
|
|
}
|
|
};
|
|
};
|
|
|
|
/*
|
|
* Primitive type (float, double, uint8_t, uint16_t, uint32_t, uint64_t, int8_t,
|
|
* int16_t, int32_t, int64_t) message class
|
|
*/
|
|
template <class T, size_t N, class Container>
|
|
class message<schema::Primitive<T, N>, Container> final : public message_base {
|
|
private:
|
|
using SchemaType = schema::Primitive<T, N>;
|
|
using MessageType = message<SchemaType, Container>;
|
|
|
|
Container container_;
|
|
|
|
static_assert(std::is_same_v<SchemaType, typename Container::SchemaType>,
|
|
"Container should have same the schema as Message");
|
|
|
|
friend class builder;
|
|
friend class reader;
|
|
|
|
public:
|
|
class reader;
|
|
class builder {
|
|
private:
|
|
MessageType &message_;
|
|
|
|
public:
|
|
builder(MessageType &msg) : message_{msg} {}
|
|
|
|
reader as_reader() { return reader{message_}; }
|
|
|
|
void set(const typename Container::ValueType &value) {
|
|
message_.container_.set(value);
|
|
}
|
|
};
|
|
|
|
class reader {
|
|
private:
|
|
MessageType &message_;
|
|
|
|
public:
|
|
reader(message &msg) : message_{msg} {}
|
|
|
|
builder as_builder() { return builder{message_}; }
|
|
|
|
const typename Container::ValueType &get() const {
|
|
return message_.container_.get();
|
|
}
|
|
};
|
|
};
|
|
|
|
template <class Container>
|
|
class message<schema::String, Container> final : public message_base {
|
|
private:
|
|
using SchemaType = schema::String;
|
|
using MessageType = message<SchemaType, Container>;
|
|
|
|
Container container_;
|
|
|
|
static_assert(std::is_same_v<SchemaType, typename Container::SchemaType>,
|
|
"Container should have same the schema as Message");
|
|
|
|
friend class builder;
|
|
friend class reader;
|
|
|
|
public:
|
|
class reader;
|
|
class builder {
|
|
private:
|
|
MessageType &message_;
|
|
|
|
public:
|
|
builder(MessageType &msg) : message_{msg} {}
|
|
|
|
reader as_reader() { return reader{message_}; }
|
|
|
|
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}); }
|
|
};
|
|
|
|
class reader {
|
|
private:
|
|
MessageType &message_;
|
|
|
|
public:
|
|
reader(MessageType &msg) : message_{msg} {}
|
|
|
|
builder as_builder() { return builder{message_}; }
|
|
|
|
const std::string_view get() const { return message_.container_.get(); }
|
|
};
|
|
};
|
|
|
|
template <class Schema, class Container = message_container<Schema>>
|
|
class HeapMessageRoot {
|
|
private:
|
|
own<message<Schema, Container>> root_;
|
|
|
|
public:
|
|
HeapMessageRoot(own<message<Schema, Container>> r) : root_{std::move(r)} {}
|
|
|
|
typename message<Schema, Container>::builder build() {
|
|
assert(root_);
|
|
return typename message<Schema, Container>::builder{*root_};
|
|
}
|
|
|
|
typename message<Schema, Container>::reader read() {
|
|
assert(root_);
|
|
return typename message<Schema, Container>::reader{*root_};
|
|
}
|
|
};
|
|
|
|
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_};
|
|
}
|
|
};
|
|
|
|
/*
|
|
* Minor helper for creating a message root
|
|
*/
|
|
template <class Schema, class Container = message_container<Schema>>
|
|
inline HeapMessageRoot<Schema, Container> heapMessageRoot() {
|
|
own<message<Schema, Container>> root = heap<message<Schema, Container>>();
|
|
return HeapMessageRoot<Schema, Container>{std::move(root)};
|
|
}
|
|
} // namespace saw
|