SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
KV1Binary.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <filesystem>
5#include <optional>
6#include <span>
7#include <string>
8#include <string_view>
9#include <variant>
10#include <vector>
11
12namespace kvpp {
13
25
26using KV1BinaryPointer = uint32_t;
27
28using KV1BinaryValue = std::variant<
29 std::monostate,
30 std::string,
31 int32_t,
32 float,
34 std::wstring,
35 sourcepp::math::Vec4ui8,
36 uint64_t
37>;
38
39template<typename V>
40concept KV1BinaryValueNoChildren = std::same_as<V, std::string>
41 || std::same_as<V, int32_t>
42 || std::same_as<V, float>
43 || std::same_as<V, KV1BinaryPointer>
44 || std::same_as<V, std::wstring>
45 || std::same_as<V, sourcepp::math::Vec4ui8>
46 || std::same_as<V, uint64_t>;
47
49public:
50 KV1BinaryElement() = default;
51
53 [[nodiscard]] std::string_view getKey() const;
54
56 void setKey(std::string_view key_);
57
59 [[nodiscard]] const KV1BinaryValue& getValue() const;
60
62 template<KV1BinaryValueNoChildren V>
63 [[nodiscard]] std::optional<V> getValue() const {
64 if (!std::holds_alternative<V>(this->value)) {
65 return std::nullopt;
66 }
67 return std::get<V>(this->value);
68 }
69
71 void setValue(KV1BinaryValue value_);
72
75
77 template<KV1BinaryValueNoChildren V>
78 void setValue(V value_) {
79 this->value = std::move(value_);
80 }
81
83 template<KV1BinaryValueNoChildren V>
85 this->setValue(std::move(value_));
86 return *this;
87 }
88
90 [[nodiscard]] bool hasChild(std::string_view childKey) const;
91
93 KV1BinaryElement& addChild(std::string_view key_, KV1BinaryValue value_ = {});
94
96 template<KV1BinaryValueNoChildren V>
97 KV1BinaryElement& addChild(std::string_view key_, V value_ = {}) {
99 elem.setKey(key_);
100 elem.setValue(std::move(value_));
101 this->children.push_back(std::move(elem));
102 return this->children.back();
103 }
104
106 [[nodiscard]] uint64_t getChildCount() const;
107
109 [[nodiscard]] uint64_t getChildCount(std::string_view childKey) const;
110
112 [[nodiscard]] const std::vector<KV1BinaryElement>& getChildren() const;
113
115 [[nodiscard]] std::vector<KV1BinaryElement>& getChildren();
116
117 using iterator = std::vector<KV1BinaryElement>::iterator;
118
119 [[nodiscard]] constexpr iterator begin() {
120 return this->children.begin();
121 }
122
123 [[nodiscard]] constexpr iterator end() {
124 return this->children.end();
125 }
126
127 using const_iterator = std::vector<KV1BinaryElement>::const_iterator;
128
129 [[nodiscard]] constexpr const_iterator begin() const {
130 return this->children.begin();
131 }
132
133 [[nodiscard]] constexpr const_iterator end() const {
134 return this->children.end();
135 }
136
137 [[nodiscard]] constexpr const_iterator cbegin() const {
138 return this->children.cbegin();
139 }
140
141 [[nodiscard]] constexpr const_iterator cend() const {
142 return this->children.cend();
143 }
144
146 [[nodiscard]] const KV1BinaryElement& operator[](unsigned int n) const;
147
149 [[nodiscard]] KV1BinaryElement& operator[](unsigned int n);
150
152 [[nodiscard]] const KV1BinaryElement& operator[](std::string_view childKey) const;
153
155 [[nodiscard]] KV1BinaryElement& operator[](std::string_view childKey);
156
158 [[nodiscard]] const KV1BinaryElement& operator()(std::string_view childKey) const;
159
161 [[nodiscard]] KV1BinaryElement& operator()(std::string_view childKey);
162
164 [[nodiscard]] const KV1BinaryElement& operator()(std::string_view childKey, unsigned int n) const;
165
167 [[nodiscard]] KV1BinaryElement& operator()(std::string_view childKey, unsigned int n);
168
170 void removeChild(unsigned int n);
171
173 void removeChild(std::string_view childKey, int n = -1);
174
176 [[nodiscard]] bool isInvalid() const;
177
178 static const KV1BinaryElement& getInvalid();
179
180 [[nodiscard]] explicit operator bool() const;
181
182protected:
183 std::string key;
185 std::vector<KV1BinaryElement> children;
186};
187
189public:
190 explicit KV1Binary(std::span<const std::byte> kv1Data = {});
191
192 explicit KV1Binary(const std::filesystem::path& kv1Path);
193
194 [[nodiscard]] std::vector<std::byte> bake() const;
195
196 void bake(const std::filesystem::path& kv1Path) const;
197
198 [[nodiscard]] std::string bakeText() const;
199
200 void bakeText(const std::filesystem::path& kv1Path) const;
201};
202
203} // namespace kvpp
const std::vector< KV1BinaryElement > & getChildren() const
Get the child elements of the element.
Definition KV1Binary.cpp:64
constexpr const_iterator cend() const
Definition KV1Binary.h:141
std::vector< KV1BinaryElement > children
Definition KV1Binary.h:185
const KV1BinaryElement & operator()(std::string_view childKey) const
Get the first child element of the element with the given key.
Definition KV1Binary.cpp:88
KV1BinaryElement & operator=(V value_)
Set the value associated with the element.
Definition KV1Binary.h:84
constexpr iterator begin()
Definition KV1Binary.h:119
std::optional< V > getValue() const
Get the value associated with the element as the given type.
Definition KV1Binary.h:63
KV1BinaryElement & addChild(std::string_view key_, KV1BinaryValue value_={})
Add a child element to the element.
Definition KV1Binary.cpp:42
static const KV1BinaryElement & getInvalid()
void removeChild(unsigned int n)
Remove a child element from the element.
bool hasChild(std::string_view childKey) const
Check if the element has one or more children with the given name.
Definition KV1Binary.cpp:38
KV1BinaryValue value
Definition KV1Binary.h:184
constexpr iterator end()
Definition KV1Binary.h:123
void setValue(V value_)
Set the value associated with the element.
Definition KV1Binary.h:78
void setKey(std::string_view key_)
Set the key associated with the element.
Definition KV1Binary.cpp:21
const KV1BinaryValue & getValue() const
Get the value associated with the element.
Definition KV1Binary.cpp:25
KV1BinaryElement & addChild(std::string_view key_, V value_={})
Add a child element to the element.
Definition KV1Binary.h:97
uint64_t getChildCount() const
Get the number of child elements.
Definition KV1Binary.cpp:50
constexpr const_iterator cbegin() const
Definition KV1Binary.h:137
KV1BinaryElement & operator=(KV1BinaryValue value_)
Set the value associated with the element.
Definition KV1Binary.cpp:33
std::vector< KV1BinaryElement >::iterator iterator
Definition KV1Binary.h:117
void setValue(KV1BinaryValue value_)
Set the value associated with the element.
Definition KV1Binary.cpp:29
const KV1BinaryElement & operator[](unsigned int n) const
Get the child element of the element at the given index.
Definition KV1Binary.cpp:72
constexpr const_iterator end() const
Definition KV1Binary.h:133
bool isInvalid() const
Check if the given element is invalid.
std::vector< KV1BinaryElement >::const_iterator const_iterator
Definition KV1Binary.h:127
constexpr const_iterator begin() const
Definition KV1Binary.h:129
std::string_view getKey() const
Get the key associated with the element.
Definition KV1Binary.cpp:17
std::vector< std::byte > bake() const
KV1Binary(std::span< const std::byte > kv1Data={})
std::string bakeText() const
Definition DMX.h:13
std::variant< std::monostate, std::string, int32_t, float, KV1BinaryPointer, std::wstring, sourcepp::math::Vec4ui8, uint64_t > KV1BinaryValue
Definition KV1Binary.h:28
KV1BinaryValueType
Definition KV1Binary.h:14
uint32_t KV1BinaryPointer
Definition KV1Binary.h:26