Botcraft 1.21.4
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 virtual ~Holder()
14 {
15
16 }
17
18
19 int GetId() const
20 {
21 return id;
22 }
23
24 const std::optional<T>& GetValue() const
25 {
26 return value;
27 }
28
29
30 auto& SetId(const int id_)
31 {
32 id = id_;
33 return *this;
34 }
35
36 auto& SetValue(const std::optional<T>& value_)
37 {
38 value = value_;
39 return *this;
40 }
41
42 protected:
43 virtual void ReadImpl(ReadIterator& iter, size_t& length) override
44 {
45 id = ReadData<VarInt>(iter, length);
46 if (id == 0)
47 {
48 value = ReadData<T>(iter, length);
49 }
50 else
51 {
52 id -= 1;
53 value = std::optional<T>();
54 }
55 }
56
57 virtual void WriteImpl(WriteContainer& container) const override
58 {
59 if (value.has_value())
60 {
61 WriteData<VarInt>(0, container);
62 WriteData<T>(value.value(), container);
63 }
64 else
65 {
66 WriteData<VarInt>(id + 1, container);
67 }
68 }
69
70 virtual Json::Value SerializeImpl() const override
71 {
72 Json::Value output;
73
74 if (value.has_value())
75 {
76 output["value"] = value.value();
77 }
78 else
79 {
80 output["id"] = id;
81 }
82
83 return output;
84 }
85
86 private:
87 int id = 0;
88 std::optional<T> value;
89
90 };
91}
92#endif
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
Definition Holder.hpp:43
auto & SetId(const int id_)
Definition Holder.hpp:30
int GetId() const
Definition Holder.hpp:19
std::optional< T > value
Definition Holder.hpp:88
virtual Json::Value SerializeImpl() const override
Definition Holder.hpp:70
auto & SetValue(const std::optional< T > &value_)
Definition Holder.hpp:36
virtual void WriteImpl(WriteContainer &container) const override
Definition Holder.hpp:57
const std::optional< T > & GetValue() const
Definition Holder.hpp:24
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