Botcraft 1.21.7
Loading...
Searching...
No Matches
Holder.hpp
Go to the documentation of this file.
1#if PROTOCOL_VERSION > 760 /* > 1.19.2 */
2#pragma once
4
5#include <optional>
6
7namespace ProtocolCraft
8{
9 template<typename T>
10 class Holder : public NetworkType
11 {
12 public:
13 Holder() = default;
14
15 template <typename U>
16 Holder(const Holder<U>& other)
17 {
18 id = other.GetId();
19 if (other.GetValue().has_value())
20 {
21 value = static_cast<T>(other.GetValue().value());
22 }
23 }
24
25 virtual ~Holder()
26 {
27
28 }
29
30
31 int GetId() const
32 {
33 return id;
34 }
35
36 const std::optional<T>& GetValue() const
37 {
38 return value;
39 }
40
41
42 auto& SetId(const int id_)
43 {
44 id = id_;
45 return *this;
46 }
47
48 auto& SetValue(const std::optional<T>& value_)
49 {
50 value = value_;
51 return *this;
52 }
53
54 protected:
55 virtual void ReadImpl(ReadIterator& iter, size_t& length) override
56 {
57 id = ReadData<VarInt>(iter, length);
58 if (id == 0)
59 {
60 value = ReadData<T>(iter, length);
61 }
62 else
63 {
64 id -= 1;
65 value = std::optional<T>();
66 }
67 }
68
69 virtual void WriteImpl(WriteContainer& container) const override
70 {
71 if (value.has_value())
72 {
73 WriteData<VarInt>(0, container);
74 WriteData<T>(value.value(), container);
75 }
76 else
77 {
78 WriteData<VarInt>(id + 1, container);
79 }
80 }
81
82 virtual Json::Value SerializeImpl() const override
83 {
84 Json::Value output;
85
86 if (value.has_value())
87 {
88 output["value"] = value.value();
89 }
90 else
91 {
92 output["id"] = id;
93 }
94
95 return output;
96 }
97
98 private:
99 int id = 0;
100 std::optional<T> value;
101
102 };
103}
104#endif
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
Definition Holder.hpp:55
auto & SetId(const int id_)
Definition Holder.hpp:42
int GetId() const
Definition Holder.hpp:31
Holder(const Holder< U > &other)
Definition Holder.hpp:16
std::optional< T > value
Definition Holder.hpp:100
virtual Json::Value SerializeImpl() const override
Definition Holder.hpp:82
auto & SetValue(const std::optional< T > &value_)
Definition Holder.hpp:48
virtual void WriteImpl(WriteContainer &container) const override
Definition Holder.hpp:69
const std::optional< T > & GetValue() const
Definition Holder.hpp:36
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45
std::vector< unsigned char > WriteContainer
std::vector< unsigned char >::const_iterator ReadIterator