Add iq types

This commit is contained in:
sha512sum 2024-10-09 19:19:32 +00:00
parent eafdecba7b
commit ad36468b84

View file

@ -0,0 +1,56 @@
#pragma once
#include <larra/serialization.hpp>
#include <larra/stream_error.hpp>
#include <larra/utils.hpp>
#include <string>
namespace larra::xmpp {
namespace iq {
template <auto& Name, typename PayloadType>
struct BaseImplWithPayload {
std::string id;
PayloadType payload;
static const inline std::string kName = Name;
template <typename Self>
[[nodiscard]] constexpr auto Id(this Self&& self, std::string id) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"id", BaseImplWithPayload>(std::forward<Self>(self), std::move(id));
}
template <typename NewPayloadType, typename Self>
[[nodiscard]] constexpr auto Payload(this Self&& self, NewPayloadType value) {
return utils::FieldSetHelper::With<"payload", BaseImplWithPayload, false>(std::forward<Self>(self), std::move(value));
}
friend constexpr auto operator<<(xmlpp::Element* element, const BaseImplWithPayload& self) {
element->set_attribute("id", self.id);
element->set_attribute("type", kName);
using S = Serialization<PayloadType>;
S::Serialize(element->add_child_element(S::kDefaultName, S::kPrefix), self.payload);
}
};
static constexpr auto kGetName = "get";
template <typename Payload>
using Get = BaseImplWithPayload<kGetName, Payload>;
static constexpr auto kSetName = "set";
template <typename Payload>
using Set = BaseImplWithPayload<kSetName, Payload>;
static constexpr auto kResultName = "result";
template <typename Payload>
using Result = BaseImplWithPayload<kResultName, Payload>;
static constexpr auto kErrorName = "error";
template <typename Payload>
using Error = BaseImplWithPayload<kErrorName, Payload>;
} // namespace iq
template <typename Payload>
using Iq = std::variant<iq::Get<Payload>, iq::Set<Payload>, iq::Result<Payload>, iq::Error<Payload>>;
} // namespace larra::xmpp