larra/library/include/larra/stream.hpp

71 lines
2.6 KiB
C++
Raw Normal View History

2024-08-30 13:01:35 +00:00
#pragma once
2024-09-28 19:15:31 +00:00
#include <libxml++/libxml++.h>
2024-08-30 13:01:35 +00:00
#include <larra/jid.hpp>
2024-08-29 16:42:58 +00:00
namespace larra::xmpp {
2024-08-30 13:01:35 +00:00
namespace impl {
template <bool JidFrom, bool JidTo>
struct BasicStream {
static constexpr bool kJidFrom = JidFrom;
static constexpr bool kJidTo = JidTo;
using FromType = std::optional<std::conditional_t<JidFrom, BareJid, std::string>>;
using ToType = std::optional<std::conditional_t<JidTo, BareJid, std::string>>;
2024-09-28 19:15:31 +00:00
static inline const std::string kDefaultNamespace = JidFrom || JidTo ? "jabber:client" : "jabber:server";
static constexpr auto kRemoveEnd = true;
static constexpr auto kAddXmlDecl = true;
static inline const std::string kDefaultPrefix = "";
static inline const std::string kDefaultName = "stream:stream";
2024-10-21 11:21:26 +00:00
FromType from{};
ToType to{};
2024-08-29 16:42:58 +00:00
std::optional<std::string> id;
std::optional<std::string> version;
std::optional<std::string> xmlLang;
template <typename Self>
2024-09-28 19:15:31 +00:00
[[nodiscard]] constexpr auto From(this Self&& self, FromType value) -> BasicStream {
return utils::FieldSetHelper::With<"from", BasicStream>(std::forward<Self>(self), std::move(value));
2024-09-03 15:36:08 +00:00
}
2024-08-29 16:42:58 +00:00
template <typename Self>
2024-09-28 19:15:31 +00:00
[[nodiscard]] constexpr auto To(this Self&& self, ToType value) -> BasicStream {
return utils::FieldSetHelper::With<"to", BasicStream>(std::forward<Self>(self), std::move(value));
2024-09-03 15:36:08 +00:00
}
2024-08-29 16:42:58 +00:00
template <typename Self>
2024-09-28 19:15:31 +00:00
[[nodiscard]] constexpr auto Id(this Self&& self, std::optional<std::string> value) -> BasicStream {
return utils::FieldSetHelper::With<"id", BasicStream>(std::forward<Self>(self), std::move(value));
2024-09-03 15:36:08 +00:00
}
2024-08-29 16:42:58 +00:00
template <typename Self>
2024-09-28 19:15:31 +00:00
[[nodiscard]] constexpr auto Version(this Self&& self, std::optional<std::string> value) -> BasicStream {
return utils::FieldSetHelper::With<"version", BasicStream>(std::forward<Self>(self), std::move(value));
2024-09-03 15:36:08 +00:00
}
2024-08-29 16:42:58 +00:00
template <typename Self>
2024-10-21 11:21:26 +00:00
[[nodiscard]] constexpr auto XmlLang(this Self&& self, std::optional<std::string> value) // NOLINT(cppcoreguidelines-missing-std-forward)
-> BasicStream {
return utils::FieldSetHelper::With<"xmlLang", BasicStream>(std::forward_like<Self>(self), std::move(value));
2024-09-03 15:36:08 +00:00
}
2024-08-29 16:42:58 +00:00
2024-09-28 19:15:31 +00:00
auto SerializeStream(xmlpp::Element* node) const -> void;
friend auto operator<<(xmlpp::Element* node, const BasicStream& stream) -> xmlpp::Element* {
2024-09-03 15:36:08 +00:00
stream.SerializeStream(node);
2024-09-28 19:15:31 +00:00
return node;
2024-09-03 15:36:08 +00:00
}
friend auto ToString(const BasicStream<JidFrom, JidTo>&) -> std::string;
2024-09-28 19:15:31 +00:00
[[nodiscard]] static auto Parse(const xmlpp::Element*) -> BasicStream;
2024-08-29 16:42:58 +00:00
};
2024-08-30 13:01:35 +00:00
} // namespace impl
using UserStream = impl::BasicStream<true, false>;
using ServerStream = impl::BasicStream<false, false>;
using ServerToUserStream = impl::BasicStream<false, true>;
2024-08-29 16:42:58 +00:00
} // namespace larra::xmpp