LeviLamina
Loading...
Searching...
No Matches
UniqueTagPtr.h
1#pragma once
2
3#include "mc/nbt/Tag.h"
4
5#include "ll/api/Expected.h"
6#include "ll/api/base/Concepts.h"
7#include "ll/api/base/Meta.h"
8
9class ByteTag;
10class ShortTag;
11class IntTag;
12class Int64Tag;
13class FloatTag;
14class DoubleTag;
15class ByteArrayTag;
16class StringTag;
17class ListTag;
18class CompoundTag;
19class IntArrayTag;
21
23 Tag* ptr{};
24
25
27 EndTag,
28 ByteTag,
30 IntTag,
36 ListTag,
39
40public:
41 [[nodiscard]] LL_CONSTEXPR23 UniqueTagPtr() noexcept {}
42
43 [[nodiscard]] LL_CONSTEXPR23 UniqueTagPtr(nullptr_t) noexcept {}
44
45 [[nodiscard]] LL_CONSTEXPR23 explicit UniqueTagPtr(Tag* p) noexcept : ptr(p) {}
46
47 [[nodiscard]] LL_CONSTEXPR23 UniqueTagPtr(std::unique_ptr<Tag>&& ptr) noexcept : ptr(ptr.release()) {}
48
49 [[nodiscard]] LL_CONSTEXPR23 UniqueTagPtr(UniqueTagPtr&& r) noexcept : ptr(r.release()) {}
50
51 [[nodiscard]] LL_CONSTEXPR23 UniqueTagPtr(UniqueTagPtr const& r) : ptr(r ? (r->copy().release()) : nullptr) {}
52
53 [[nodiscard]] UniqueTagPtr(CompoundTagVariant&& r);
54 [[nodiscard]] UniqueTagPtr(CompoundTagVariant const& r);
55
56 UniqueTagPtr& operator=(CompoundTagVariant&& r);
57 UniqueTagPtr& operator=(CompoundTagVariant const& r);
58
59 LL_CONSTEXPR23 UniqueTagPtr& operator=(nullptr_t) noexcept {
60 reset();
61 return *this;
62 }
63
64 LL_CONSTEXPR23 UniqueTagPtr& operator=(std::unique_ptr<Tag>&& r) noexcept {
65 reset(r.release());
66 return *this;
67 }
68
69 LL_CONSTEXPR23 UniqueTagPtr& operator=(UniqueTagPtr&& r) noexcept {
70 reset(r.release());
71 return *this;
72 }
73
74 LL_CONSTEXPR23 UniqueTagPtr& operator=(UniqueTagPtr const& r) {
75 if (r && &r != this) ptr = r->copy().release();
76 return *this;
77 }
78 LL_CONSTEXPR23 void swap(UniqueTagPtr& r) noexcept { std::swap(ptr, r.ptr); }
79
80 LL_CONSTEXPR23 ~UniqueTagPtr() noexcept { delete ptr; }
81
82 [[nodiscard]] LL_CONSTEXPR23 Tag& operator*() const noexcept { return *ptr; }
83
84 [[nodiscard]] LL_CONSTEXPR23 Tag* operator->() const noexcept { return ptr; }
85
86 [[nodiscard]] LL_CONSTEXPR23 Tag* get() const noexcept { return ptr; }
87
88 LL_CONSTEXPR23 explicit operator bool() const noexcept { return static_cast<bool>(ptr); }
89
90 LL_CONSTEXPR23 operator std::unique_ptr<Tag>() && noexcept { return std::unique_ptr<Tag>{release()}; }
91
92 LL_CONSTEXPR23 operator std::unique_ptr<Tag>() const& noexcept { return ptr ? ptr->copy() : nullptr; }
93
94 LL_CONSTEXPR23 Tag* release() noexcept { return std::exchange(ptr, nullptr); }
95
96 LL_CONSTEXPR23 void reset(Tag* p = nullptr) noexcept {
97 Tag* old = std::exchange(ptr, p);
98 delete old;
99 }
100
101 [[nodiscard]] UniqueTagPtr& operator[](size_t index);
102 [[nodiscard]] UniqueTagPtr const& operator[](size_t index) const;
103 [[nodiscard]] CompoundTagVariant& operator[](std::string_view index);
104 [[nodiscard]] CompoundTagVariant const& operator[](std::string_view index) const;
105 template <size_t N>
106 [[nodiscard]] CompoundTagVariant& operator[](char const (&index)[N]) { // make EDG happy
107 return operator[](std::string_view{index});
108 }
109 template <size_t N>
110 [[nodiscard]] CompoundTagVariant const& operator[](char const (&index)[N]) const { // make EDG happy
111 return operator[](std::string_view{index});
112 }
113
114public:
115 [[nodiscard]] std::unique_ptr<Tag> copy() const { return ptr ? ptr->copy() : nullptr; }
116
117 [[nodiscard]] ::Tag::Type index() const { return ptr ? ptr->getId() : ::Tag::End; }
118 [[nodiscard]] Tag::Type getId() const noexcept { return index(); }
119
120 [[nodiscard]] inline bool operator==(UniqueTagPtr const& r) const {
121 return (ptr == r.get()) || (ptr && r && *ptr == *r);
122 }
123 template <std::derived_from<Tag> T>
124 [[nodiscard]] bool hold() const noexcept {
125 return ptr && (Types::index<T> == (size_t)getId());
126 }
127 [[nodiscard]] bool hold(::Tag::Type type) const noexcept { return ptr && (getId() == type); }
128
129 // consistency with json
130 [[nodiscard]] bool is_array() const noexcept { return hold(Tag::List); }
131 [[nodiscard]] bool is_binary() const noexcept { return hold(Tag::ByteArray) || hold(Tag::IntArray); }
132 [[nodiscard]] bool is_boolean() const noexcept { return hold(Tag::Byte); }
133 [[nodiscard]] bool is_null() const noexcept { return hold(Tag::End); }
134 [[nodiscard]] bool is_number_float() const noexcept { return hold(Tag::Float) || hold(Tag::Double); }
135 [[nodiscard]] bool is_number_integer() const noexcept {
136 return hold(Tag::Byte) || hold(Tag::Short) || hold(Tag::Int) || hold(Tag::Int64);
137 }
138 [[nodiscard]] bool is_object() const noexcept { return hold(Tag::Compound); }
139 [[nodiscard]] bool is_string() const noexcept { return hold(Tag::String); }
140 [[nodiscard]] bool is_number() const noexcept { return is_number_float() || is_number_integer(); }
141 [[nodiscard]] bool is_primitive() const noexcept { return is_null() || is_string() || is_number() || is_binary(); }
142 [[nodiscard]] bool is_structured() const noexcept { return is_array() || is_object(); }
143
144 [[nodiscard]] std::string
145 toSnbt(SnbtFormat snbtFormat = SnbtFormat::PrettyFilePrint, uchar indent = 4) const noexcept {
146 return ptr ? ptr->toSnbt(snbtFormat, indent) : "";
147 }
148 template <std::derived_from<Tag> T>
149 T& emplace() {
150 reset(new T());
151 }
152 template <std::derived_from<Tag> T>
153 [[nodiscard]] bool contains(std::string_view key) const noexcept {
154 return contains(key, (Tag::Type)Types::index<T>);
155 }
156
157public:
158 template <std::derived_from<Tag> T>
159 [[nodiscard]] T& get() const;
160
161 [[nodiscard]] bool contains(std::string_view key) const noexcept;
162 [[nodiscard]] bool contains(std::string_view key, Tag::Type type) const noexcept;
163 [[nodiscard]] size_t size() const noexcept;
164
165 [[nodiscard]] operator std::string const&() const;
166 [[nodiscard]] operator std::string&() &;
167 [[nodiscard]] operator std::string&&() &&;
168 [[nodiscard]] operator std::string_view() const;
169
170 template <class T>
171 requires(std::is_arithmetic_v<T> && !ll::traits::is_char_v<T>)
172 [[nodiscard]] operator T() const;
173};
Definition ByteArrayTag.h:9
Definition ByteTag.h:9
Definition CompoundTagVariant.h:38
Definition CompoundTag.h:13
Definition DoubleTag.h:9
Definition EndTag.h:9
Definition FloatTag.h:9
Definition Int64Tag.h:9
Definition IntArrayTag.h:9
Definition IntTag.h:9
Definition ListTag.h:12
Definition ShortTag.h:9
Definition StringTag.h:9
Definition Tag.h:39
Definition UniqueTagPtr.h:22
Definition Meta.h:197