larra/library/include/larra/message.hpp
sha512sum 64e2aeb8aa
All checks were successful
PR Check / on-push-commit-check (push) Successful in 12m18s
Add message serialization and deserialization
2024-12-21 19:17:11 +11:00

49 lines
1.5 KiB
C++

#pragma once
#include <libxml++/libxml++.h>
#include <larra/serialization/auto.hpp>
#include <larra/xml_language.hpp>
#include <string>
namespace larra::xmpp {
template <typename From, typename To>
struct Message {
static constexpr auto kDefaultName = "message";
struct Body : std::string {
using std::string::basic_string;
constexpr Body(std::string str) : std::string{std::move(str)} {
}
static constexpr auto kDefaultName = "body";
friend constexpr auto operator<<(xmlpp::Element* node, const Body& message) -> void {
node->add_child_text(message);
}
static constexpr auto Parse(xmlpp::Element* node) -> Body {
auto ptr = node->get_first_child_text();
if(!ptr) {
throw std::runtime_error("Message::Body: [ Text node not found ]");
}
return {ptr->get_content()};
}
};
static auto Parse(xmlpp::Element* element) -> Message {
return serialization::Parse<Message>(element);
}
friend auto operator<<(xmlpp::Element* element, const Message& message) -> void {
serialization::Serialize(element, message);
}
constexpr auto operator==(const Message& other) const -> bool = default;
From from;
To to;
std::string type;
std::optional<std::string> id;
XmlLanguage language;
Body body;
};
template <typename From, typename To>
struct serialization::SerializationConfigT<Message<From, To>> {
static constexpr auto kValue = serialization::SerializationConfig<Message<From, To>>{};
};
} // namespace larra::xmpp