SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
EntityLump.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <format>
5#include <string>
6#include <string_view>
7#include <vector>
8
9#include <BufferStream.h>
11#include <sourcepp/String.h>
12
13namespace bsppp {
14
15template<typename V>
16concept BSPEntityKeyValueType = std::convertible_to<V, std::string_view>
17 || std::same_as<V, bool>
18 || std::same_as<V, int32_t>
19 || std::same_as<V, int64_t>
20 || std::same_as<V, float>;
21
23public:
24 class Element {
25 friend class BSPEntityKeyValues;
26
27 public:
29 [[nodiscard]] std::string_view getKey() const;
30
32 void setKey(std::string_view key_);
33
35 [[nodiscard]] std::string_view getValue() const;
36
38 template<BSPEntityKeyValueType V>
39 [[nodiscard]] V getValue() const {
40 if constexpr (std::convertible_to<V, std::string_view>) {
41 return this->value;
42 } else if constexpr (std::same_as<V, bool>) {
43 return static_cast<bool>(this->getValue<int32_t>());
44 } else if constexpr (std::same_as<V, int32_t>) {
45 return std::stoi(std::string{this->value});
46 } else if constexpr (std::same_as<V, int64_t>) {
47 return std::stoll(std::string{this->value});
48 } else if constexpr (std::same_as<V, float>) {
49 return std::stof(std::string{this->value});
50 }
51 return V{};
52 }
53
55 template<BSPEntityKeyValueType V>
56 void setValue(V value_) {
57 if constexpr (std::convertible_to<V, std::string_view>) {
58 this->value = std::string_view{value_};
59 } else if constexpr (std::same_as<V, bool>) {
60 this->setValue(std::format("{:b}", value_));
61 } else {
62 this->setValue(std::format("{}", value_));
63 }
64 }
65
67 template<BSPEntityKeyValueType V>
68 Element& operator=(V value_) {
69 this->setValue(value_);
70 return *this;
71 }
72
74 [[nodiscard]] bool isInvalid() const;
75
76 protected:
77 Element() = default;
78
79 static const Element& getInvalid();
80
81 std::string key;
82 std::string value;
83 };
84
85 BSPEntityKeyValues() = default;
86
88 [[nodiscard]] bool hasChild(std::string_view childKey) const;
89
91 [[nodiscard]] uint64_t getKeyValuesCount() const;
92
94 [[nodiscard]] uint64_t getKeyValuesCount(std::string_view childKey) const;
95
97 [[nodiscard]] const std::vector<Element>& getKeyValues() const;
98
100 [[nodiscard]] const Element& operator[](unsigned int n) const;
101
103 [[nodiscard]] Element& operator[](unsigned int n);
104
106 [[nodiscard]] const Element& operator[](std::string_view childKey) const;
107
109 [[nodiscard]] Element& operator[](std::string_view childKey);
110
112 [[nodiscard]] const Element& operator()(std::string_view childKey) const;
113
115 [[nodiscard]] Element& operator()(std::string_view childKey);
116
118 [[nodiscard]] const Element& operator()(std::string_view childKey, unsigned int n) const;
119
121 [[nodiscard]] Element& operator()(std::string_view childKey, unsigned int n);
122
124 template<BSPEntityKeyValueType V = std::string_view>
125 Element& addKeyValue(std::string_view key_, V value_ = {}) {
126 Element elem;
127 elem.setKey(key_);
128 elem.setValue(value_);
129 this->keyvalues.push_back(elem);
130 return this->keyvalues.back();
131 }
132
134 void removeKeyValue(std::string_view childKey, int n = -1);
135
136 [[nodiscard]] std::string bake(bool useEscapes) const;
137
138protected:
139 std::vector<Element> keyvalues;
140};
141
142} // namespace bsppp
static const Element & getInvalid()
V getValue() const
Get the value associated with the element as the given type.
Definition EntityLump.h:39
void setValue(V value_)
Set the value associated with the element.
Definition EntityLump.h:56
Element & operator=(V value_)
Set the value associated with the element.
Definition EntityLump.h:68
std::string_view getKey() const
Get the key associated with the element.
Definition EntityLump.cpp:6
std::string_view getValue() const
Get the value associated with the element.
bool isInvalid() const
Check if the given element is invalid.
void setKey(std::string_view key_)
Set the key associated with the element.
const Element & operator()(std::string_view childKey) const
Get the first keyvalue of the entity with the given key.
bool hasChild(std::string_view childKey) const
Check if this entity has one or more keyvalues with the given name.
Element & addKeyValue(std::string_view key_, V value_={})
Add a new keyvalue to the entity.
Definition EntityLump.h:125
uint64_t getKeyValuesCount() const
Get the number of keyvalues.
std::string bake(bool useEscapes) const
const std::vector< Element > & getKeyValues() const
Get the keyvalues of the entity.
void removeKeyValue(std::string_view childKey, int n=-1)
Remove a keyvalue from the entity. -1 means all keyvalues with the given key.
std::vector< Element > keyvalues
Definition EntityLump.h:139
const Element & operator[](unsigned int n) const
Get the keyvalue of the entity at the given index.
Definition BSP.h:18