Luanti 5.16.0-dev
 
Loading...
Searching...
No Matches
sscsm_irequest.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 Luanti authors
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#pragma once
6
7#include "exceptions.h"
8#include <memory>
9#include <type_traits>
10
11class SSCSMController;
12class Client;
13
14// FIXME: remove once we have actual serialization
15// this is just here so we can instead put them into unique_ptr
17{
18 virtual ~ISSCSMAnswer() = default;
19};
20
21// FIXME: actually serialize, and replace this with a std::vector<u8>.
22// also update function argument declarations, to take
23// `const SerializedSSCSMAnswer &` or whatever
24// (not polymorphic. the receiving side will know the answer type that is in here)
25using SerializedSSCSMAnswer = std::unique_ptr<ISSCSMAnswer>;
26
27// Request made by the sscsm env to the main env.
29{
30 virtual ~ISSCSMRequest() = default;
31
33};
34
35// FIXME: as above, actually serialize
36// (polymorphic. this can be any ISSCSMRequest. ==> needs type tag)
37using SerializedSSCSMRequest = std::unique_ptr<ISSCSMRequest>;
38
39template <typename T>
41{
42 static_assert(std::is_base_of_v<ISSCSMRequest, T>);
43
44 // FIXME: this will need to use a type tag for T
45
46 return std::make_unique<T>(request);
47}
48
49template <typename T>
51{
52 static_assert(std::is_base_of_v<ISSCSMAnswer, T>);
53
54 // FIXME: should look something like this:
55 // return sscsm::Serializer<T>{}.deserialize(answer_serialized);
56 // (note: answer_serialized does not need a type tag)
57
58 // dynamic cast in place of actual deserialization
59 auto ptr = dynamic_cast<T *>(answer_serialized.get());
60 if (!ptr) {
61 throw SerializationError("deserializeSSCSMAnswer failed");
62 }
63 return std::move(*ptr);
64}
65
66template <typename T>
68{
69 static_assert(std::is_base_of_v<ISSCSMAnswer, T>);
70
71 // FIXME: should look something like this:
72 // return sscsm::Serializer<T>{}.serialize(request);
73
74 return std::make_unique<T>(std::move(answer));
75}
76
77inline std::unique_ptr<ISSCSMRequest> deserializeSSCSMRequest(SerializedSSCSMRequest request_serialized)
78{
79 // FIXME: The actual deserialization will have to use a type tag, and then
80 // choose the appropriate deserializer.
81 return request_serialized;
82}
Definition client.h:107
The purpose of this class is to:
Definition sscsm_controller.h:25
Definition exceptions.h:51
Definition activeobjectmgr.cpp:11
std::unique_ptr< ISSCSMRequest > deserializeSSCSMRequest(SerializedSSCSMRequest request_serialized)
Definition sscsm_irequest.h:77
T deserializeSSCSMAnswer(SerializedSSCSMAnswer answer_serialized)
Definition sscsm_irequest.h:50
SerializedSSCSMAnswer serializeSSCSMAnswer(T &&answer)
Definition sscsm_irequest.h:67
std::unique_ptr< ISSCSMAnswer > SerializedSSCSMAnswer
Definition sscsm_irequest.h:25
std::unique_ptr< ISSCSMRequest > SerializedSSCSMRequest
Definition sscsm_irequest.h:37
SerializedSSCSMRequest serializeSSCSMRequest(const T &request)
Definition sscsm_irequest.h:40
Definition sscsm_irequest.h:17
virtual ~ISSCSMAnswer()=default
Definition sscsm_irequest.h:29
virtual ~ISSCSMRequest()=default
virtual SerializedSSCSMAnswer exec(Client *client)=0