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 <string>
5#include <string_view>
6#include <vector>
7
8#include <BufferStream.h>
10#include <sourcepp/String.h>
11
12namespace bsppp {
13
14template<typename V>
15concept BSPEntityKeyValueType = std::convertible_to<V, std::string_view>
16 || std::same_as<V, bool>
17 || std::same_as<V, int32_t>
18 || std::same_as<V, int64_t>
19 || std::same_as<V, float>;
20
22public:
23 class Element {
24 friend class BSPEntityKeyValues;
25
26 public:
28 [[nodiscard]] std::string_view getKey() const;
29
31 void setKey(std::string_view key_);
32
34 [[nodiscard]] std::string_view getValue() const;
35
37 template<BSPEntityKeyValueType V>
38 [[nodiscard]] V getValue() const {
39 if constexpr (std::convertible_to<V, std::string_view>) {
40 return this->value;
41 } else if constexpr (std::same_as<V, bool>) {
42 return static_cast<bool>(this->getValue<int32_t>());
43 } else if constexpr (std::same_as<V, int32_t>) {
44 return std::stoi(std::string{this->value});
45 } else if constexpr (std::same_as<V, int64_t>) {
46 return std::stoll(std::string{this->value});
47 } else if constexpr (std::same_as<V, float>) {
48 return std::stof(std::string{this->value});
49 }
50 return V{};
51 }
52
54 template<BSPEntityKeyValueType V>
55 void setValue(V value_) {
56 if constexpr (std::convertible_to<V, std::string_view>) {
57 this->value = std::string_view{value_};
58 } else {
59 this->setValue(std::to_string(value_));
60 }
61 }
62
64 template<BSPEntityKeyValueType V>
65 Element& operator=(V value_) {
66 this->setValue(value_);
67 return *this;
68 }
69
71 [[nodiscard]] bool isInvalid() const;
72
73 protected:
74 Element() = default;
75
76 static const Element& getInvalid();
77
78 std::string key;
79 std::string value;
80 };
81
82 BSPEntityKeyValues() = default;
83
85 [[nodiscard]] bool hasChild(std::string_view childKey) const;
86
88 [[nodiscard]] uint64_t getKeyValuesCount() const;
89
91 [[nodiscard]] uint64_t getKeyValuesCount(std::string_view childKey) const;
92
94 [[nodiscard]] const std::vector<Element>& getKeyValues() const;
95
97 [[nodiscard]] const Element& operator[](unsigned int n) const;
98
100 [[nodiscard]] Element& operator[](unsigned int n);
101
103 [[nodiscard]] const Element& operator[](std::string_view childKey) const;
104
106 [[nodiscard]] Element& operator[](std::string_view childKey);
107
109 [[nodiscard]] const Element& operator()(std::string_view childKey) const;
110
112 [[nodiscard]] Element& operator()(std::string_view childKey);
113
115 [[nodiscard]] const Element& operator()(std::string_view childKey, unsigned int n) const;
116
118 [[nodiscard]] Element& operator()(std::string_view childKey, unsigned int n);
119
121 template<BSPEntityKeyValueType V = std::string_view>
122 Element& addKeyValue(std::string_view key_, V value_ = {}) {
123 Element elem;
124 elem.setKey(key_);
125 elem.setValue(value_);
126 this->keyvalues.push_back(elem);
127 return this->keyvalues.back();
128 }
129
131 void removeKeyValue(std::string_view childKey, int n = -1);
132
133 [[nodiscard]] std::string bake(bool useEscapes) const;
134
135protected:
136 std::vector<Element> keyvalues;
137};
138
139} // namespace bsppp
static const Element & getInvalid()
Definition: EntityLump.cpp:22
V getValue() const
Get the value associated with the element as the given type.
Definition: EntityLump.h:38
void setValue(V value_)
Set the value associated with the element.
Definition: EntityLump.h:55
Element & operator=(V value_)
Set the value associated with the element.
Definition: EntityLump.h:65
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.
Definition: EntityLump.cpp:14
bool isInvalid() const
Check if the given element is invalid.
Definition: EntityLump.cpp:18
void setKey(std::string_view key_)
Set the key associated with the element.
Definition: EntityLump.cpp:10
const Element & operator()(std::string_view childKey) const
Get the first keyvalue of the entity with the given key.
Definition: EntityLump.cpp:65
bool hasChild(std::string_view childKey) const
Check if this entity has one or more keyvalues with the given name.
Definition: EntityLump.cpp:27
Element & addKeyValue(std::string_view key_, V value_={})
Add a new keyvalue to the entity.
Definition: EntityLump.h:122
uint64_t getKeyValuesCount() const
Get the number of keyvalues.
Definition: EntityLump.cpp:31
std::string bake(bool useEscapes) const
Definition: EntityLump.cpp:124
const std::vector< Element > & getKeyValues() const
Get the keyvalues of the entity.
Definition: EntityLump.cpp:45
void removeKeyValue(std::string_view childKey, int n=-1)
Remove a keyvalue from the entity. -1 means all keyvalues with the given key.
Definition: EntityLump.cpp:109
std::vector< Element > keyvalues
Definition: EntityLump.h:136
const Element & operator[](unsigned int n) const
Get the keyvalue of the entity at the given index.
Definition: EntityLump.cpp:49
Definition: BSP.h:18