Skip to content

Commit ab533c7

Browse files
toolpp: take a path object in ctor/bake methods
1 parent 74822f5 commit ab533c7

File tree

5 files changed

+96
-70
lines changed

5 files changed

+96
-70
lines changed

include/toolpp/CmdSeq.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <cstddef>
3+
#include <filesystem>
44
#include <string>
55
#include <vector>
66

@@ -51,10 +51,10 @@ class CmdSeq {
5151
KEYVALUES_STRATA,
5252
};
5353

54-
explicit CmdSeq(const std::string& path);
55-
5654
explicit CmdSeq(Type type_);
5755

56+
explicit CmdSeq(const std::filesystem::path& cmdSeqPath);
57+
5858
[[nodiscard]] explicit operator bool() const;
5959

6060
[[nodiscard]] Type getType() const;
@@ -71,12 +71,12 @@ class CmdSeq {
7171

7272
[[nodiscard]] std::vector<std::byte> bake() const;
7373

74-
bool bake(const std::string& path) const; // NOLINT(*-use-nodiscard)
74+
bool bake(const std::filesystem::path& path) const; // NOLINT(*-use-nodiscard)
7575

7676
protected:
77-
void parseBinary(const std::string& path);
77+
void parseBinary(const std::filesystem::path& path);
7878

79-
void parseKeyValuesStrata(const std::string& path);
79+
void parseKeyValuesStrata(const std::filesystem::path& path);
8080

8181
[[nodiscard]] std::vector<std::byte> bakeBinary() const;
8282

include/toolpp/FGD.h

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

3+
#include <initializer_list>
34
#include <list>
5+
#include <span>
46
#include <string>
57
#include <string_view>
68
#include <unordered_map>
@@ -87,14 +89,14 @@ class FGD {
8789

8890
FGD() = default;
8991

90-
explicit FGD(const std::string& fgdPath);
92+
explicit FGD(const std::filesystem::path& fgdPath);
9193

9294
/**
9395
* Can be called multiple times in succession to load multiple FGD files.
9496
* The FGD file data will be merged with previously loaded data.
9597
* @param fgdPath The path to the FGD to load
9698
*/
97-
void load(const std::string& fgdPath);
99+
void load(const std::filesystem::path& fgdPath);
98100

99101
[[nodiscard]] int getVersion() const;
100102

@@ -107,7 +109,7 @@ class FGD {
107109
[[nodiscard]] const std::vector<AutoVisGroup>& getAutoVisGroups() const;
108110

109111
protected:
110-
void readEntities(BufferStreamReadOnly& stream, const std::string& path, std::vector<std::string>& seenPaths);
112+
void readEntities(BufferStreamReadOnly& stream, const std::filesystem::path& path, std::vector<std::filesystem::path>& seenPaths);
111113

112114
std::list<std::string> backingData;
113115

@@ -124,7 +126,9 @@ class FGDWriter {
124126
public:
125127
explicit AutoVisGroupWriter(FGDWriter& parent_);
126128

127-
AutoVisGroupWriter& visGroup(const std::string& name, const std::vector<std::string>& entities);
129+
AutoVisGroupWriter& visGroup(std::string_view name, std::initializer_list<std::string_view> entities);
130+
131+
AutoVisGroupWriter& visGroup(std::string_view name, std::span<const std::string_view> entities);
128132

129133
FGDWriter& endAutoVisGroup() const; // NOLINT(*-use-nodiscard)
130134

@@ -138,7 +142,7 @@ class FGDWriter {
138142
public:
139143
explicit KeyValueChoicesWriter(EntityWriter& parent_);
140144

141-
KeyValueChoicesWriter& choice(const std::string& value, const std::string& displayName);
145+
KeyValueChoicesWriter& choice(std::string_view value, std::string_view displayName);
142146

143147
EntityWriter& endKeyValueChoices() const; // NOLINT(*-use-nodiscard)
144148

@@ -150,7 +154,7 @@ class FGDWriter {
150154
public:
151155
explicit KeyValueFlagsWriter(EntityWriter& parent_);
152156

153-
KeyValueFlagsWriter& flag(uint64_t value, const std::string& displayName, bool enabledByDefault, const std::string& description = "");
157+
KeyValueFlagsWriter& flag(uint64_t value, std::string_view displayName, bool enabledByDefault, std::string_view description = "");
154158

155159
EntityWriter& endKeyValueFlags() const; // NOLINT(*-use-nodiscard)
156160

@@ -160,15 +164,15 @@ class FGDWriter {
160164

161165
explicit EntityWriter(FGDWriter& parent_);
162166

163-
EntityWriter& keyValue(const std::string& name, const std::string& valueType, const std::string& displayName = "", const std::string& valueDefault = "", const std::string& description = "", bool readOnly = false, bool report = false);
167+
EntityWriter& keyValue(std::string_view name, std::string_view valueType, std::string_view displayName = "", std::string_view valueDefault = "", std::string_view description = "", bool readOnly = false, bool report = false);
164168

165-
KeyValueChoicesWriter beginKeyValueChoices(const std::string& name, const std::string& displayName = "", const std::string& valueDefault = "", const std::string& description = "", bool readOnly = false, bool report = false);
169+
KeyValueChoicesWriter beginKeyValueChoices(std::string_view name, std::string_view displayName = "", std::string_view valueDefault = "", std::string_view description = "", bool readOnly = false, bool report = false);
166170

167-
KeyValueFlagsWriter beginKeyValueFlags(const std::string& name, const std::string& displayName = "", const std::string& description = "", bool readOnly = false, bool report = false);
171+
KeyValueFlagsWriter beginKeyValueFlags(std::string_view name, std::string_view displayName = "", std::string_view description = "", bool readOnly = false, bool report = false);
168172

169-
EntityWriter& input(const std::string& name, const std::string& valueType, const std::string& description = "");
173+
EntityWriter& input(std::string_view name, std::string_view valueType, std::string_view description = "");
170174

171-
EntityWriter& output(const std::string& name, const std::string& valueType, const std::string& description = "");
175+
EntityWriter& output(std::string_view name, std::string_view valueType, std::string_view description = "");
172176

173177
FGDWriter& endEntity() const; // NOLINT(*-use-nodiscard)
174178

@@ -178,21 +182,25 @@ class FGDWriter {
178182

179183
[[nodiscard]] static FGDWriter begin();
180184

181-
FGDWriter& include(const std::string& fgdPath);
185+
FGDWriter& include(const std::filesystem::path& fgdPath);
182186

183187
FGDWriter& version(int version);
184188

185189
FGDWriter& mapSize(sourcepp::math::Vec2i mapSize);
186190

187-
FGDWriter& materialExclusionDirs(const std::vector<std::string>& dirs);
191+
FGDWriter& materialExclusionDirs(std::initializer_list<std::string_view> dirs);
192+
193+
FGDWriter& materialExclusionDirs(std::span<const std::string_view> dirs);
194+
195+
AutoVisGroupWriter beginAutoVisGroup(std::string_view parentName);
188196

189-
AutoVisGroupWriter beginAutoVisGroup(const std::string& parentName);
197+
EntityWriter beginEntity(std::string_view classType, std::initializer_list<std::string_view> classProperties, std::string_view name, std::string_view description = "", std::string_view docsURL = "");
190198

191-
EntityWriter beginEntity(const std::string& classType, const std::vector<std::string>& classProperties, const std::string& name, const std::string& description = "", const std::string& docsURL = "");
199+
EntityWriter beginEntity(std::string_view classType, std::span<const std::string_view> classProperties, std::string_view name, std::string_view description = "", std::string_view docsURL = "");
192200

193201
[[nodiscard]] std::string bake() const;
194202

195-
bool bake(const std::string& fgdPath) const; // NOLINT(*-use-nodiscard)
203+
bool bake(const std::filesystem::path& fgdPath) const; // NOLINT(*-use-nodiscard)
196204

197205
protected:
198206
FGDWriter();

lang/python/src/toolpp.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ inline void register_python(py::module_& m) {
4848
.value("KEYVALUES_STRATA", CmdSeq::Type::KEYVALUES_STRATA);
4949

5050
cCmdSeq
51-
.def(py::init<const std::string&>(), "path"_a)
51+
.def(py::init<const std::filesystem::path&>(), "path"_a)
5252
.def(py::init<CmdSeq::Type>(), "type"_a)
5353
.def("__bool__", &CmdSeq::operator bool, py::is_operator())
5454
.def_prop_rw("type", &CmdSeq::getType, &CmdSeq::setType)
@@ -59,7 +59,7 @@ inline void register_python(py::module_& m) {
5959
const auto d = self.bake();
6060
return py::bytes{d.data(), d.size()};
6161
})
62-
.def("bake_to_file", py::overload_cast<const std::string&>(&CmdSeq::bake, py::const_), "path"_a);
62+
.def("bake_to_file", py::overload_cast<const std::filesystem::path&>(&CmdSeq::bake, py::const_), "path"_a);
6363

6464
auto cFGD = py::class_<FGD>(toolpp, "FGD");
6565
auto cFGDEntity = py::class_<FGD::Entity>(cFGD, "Entity");
@@ -131,7 +131,7 @@ inline void register_python(py::module_& m) {
131131

132132
cFGD
133133
.def(py::init())
134-
.def(py::init<const std::string&>(), "fgd_path"_a)
134+
.def(py::init<const std::filesystem::path&>(), "fgd_path"_a)
135135
.def("load", &FGD::load, "fgd_path"_a)
136136
.def_prop_ro("version", &FGD::getVersion)
137137
.def_prop_ro("map_size", &FGD::getMapSize)
@@ -142,8 +142,8 @@ inline void register_python(py::module_& m) {
142142
auto cFGDWriter = py::class_<FGDWriter>(toolpp, "FGDWriter");
143143

144144
py::class_<FGDWriter::AutoVisGroupWriter>(cFGDWriter, "AutoVisGroupWriter")
145-
.def("visgroup", &FGDWriter::AutoVisGroupWriter::visGroup, "name"_a, "entities"_a, py::rv_policy::reference)
146-
.def("end_auto_visgroup", &FGDWriter::AutoVisGroupWriter::endAutoVisGroup, py::rv_policy::reference);
145+
.def("visgroup", py::overload_cast<std::string_view, std::span<const std::string_view>>(&FGDWriter::AutoVisGroupWriter::visGroup), "name"_a, "entities"_a, py::rv_policy::reference)
146+
.def("end_auto_visgroup", &FGDWriter::AutoVisGroupWriter::endAutoVisGroup, py::rv_policy::reference);
147147

148148
auto cFGDWriterEntityWriter = py::class_<FGDWriter::EntityWriter>(cFGDWriter, "EntityWriter");
149149

@@ -168,14 +168,14 @@ inline void register_python(py::module_& m) {
168168
.def("include", &FGDWriter::include, "fgd_path"_a, py::rv_policy::reference)
169169
.def("version", &FGDWriter::version, "version"_a, py::rv_policy::reference)
170170
.def("map_size", &FGDWriter::mapSize, "map_size"_a, py::rv_policy::reference)
171-
.def("material_exclusion_dirs", &FGDWriter::materialExclusionDirs, "material_exclusion_dirs"_a, py::rv_policy::reference)
171+
.def("material_exclusion_dirs", py::overload_cast<std::span<const std::string_view>>(&FGDWriter::materialExclusionDirs), "material_exclusion_dirs"_a, py::rv_policy::reference)
172172
.def("begin_auto_visgroup", &FGDWriter::beginAutoVisGroup, "parent_name"_a)
173-
.def("begin_entity", &FGDWriter::beginEntity, "class_type"_a, "class_properties"_a, "name"_a, "description"_a = "", "docs_url"_a = "")
173+
.def("begin_entity", py::overload_cast<std::string_view, std::span<const std::string_view>, std::string_view, std::string_view, std::string_view>(&FGDWriter::beginEntity), "class_type"_a, "class_properties"_a, "name"_a, "description"_a = "", "docs_url"_a = "")
174174
.def("bake", [](const FGDWriter& self) {
175175
const auto d = self.bake();
176176
return py::bytes{d.data(), d.size()};
177177
})
178-
.def("bake_to_file", py::overload_cast<const std::string&>(&FGDWriter::bake, py::const_), "path"_a);
178+
.def("bake_to_file", py::overload_cast<const std::filesystem::path&>(&FGDWriter::bake, py::const_), "path"_a);
179179
}
180180

181181
} // namespace vcryptpp

src/toolpp/CmdSeq.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ReSharper disable CppRedundantQualifier
2+
13
#include <toolpp/CmdSeq.h>
24

35
#include <cstring>
@@ -59,15 +61,19 @@ std::string CmdSeq::Command::getExecutableDisplayName() const {
5961
return this->executable;
6062
}
6163

62-
CmdSeq::CmdSeq(const std::string& path)
64+
CmdSeq::CmdSeq(Type type_)
65+
: type(type_)
66+
, version(0.2f) {}
67+
68+
CmdSeq::CmdSeq(const std::filesystem::path& cmdSeqPath)
6369
: type(Type::INVALID)
6470
, version(0.2f) {
6571
{
66-
FileStream reader{path};
72+
FileStream reader{cmdSeqPath};
6773
if (!reader) {
6874
return;
6975
}
70-
if (auto binStr = reader.seek_in(0).read_string(10); binStr == "Worldcraft") {
76+
if (const auto binStr = reader.seek_in(0).read_string(10); binStr == "Worldcraft") {
7177
this->type = Type::BINARY;
7278
} else {
7379
auto kvStr = reader.seek_in(0).read_string(19);
@@ -84,18 +90,14 @@ CmdSeq::CmdSeq(const std::string& path)
8490
case INVALID:
8591
break;
8692
case BINARY:
87-
this->parseBinary(path);
93+
this->parseBinary(cmdSeqPath);
8894
break;
8995
case KEYVALUES_STRATA:
90-
this->parseKeyValuesStrata(path);
96+
this->parseKeyValuesStrata(cmdSeqPath);
9197
break;
9298
}
9399
}
94100

95-
CmdSeq::CmdSeq(Type type_)
96-
: type(type_)
97-
, version(0.2f) {}
98-
99101
CmdSeq::operator bool() const {
100102
return this->type != Type::INVALID;
101103
}
@@ -120,7 +122,7 @@ void CmdSeq::setVersion(bool isV02) {
120122
}
121123
}
122124

123-
void CmdSeq::parseBinary(const std::string& path) {
125+
void CmdSeq::parseBinary(const std::filesystem::path& path) {
124126
FileStream reader{path};
125127
if (!reader) {
126128
return;
@@ -154,7 +156,7 @@ void CmdSeq::parseBinary(const std::string& path) {
154156
}
155157
}
156158

157-
void CmdSeq::parseKeyValuesStrata(const std::string& path) {
159+
void CmdSeq::parseKeyValuesStrata(const std::filesystem::path& path) {
158160
this->version = 0.2f;
159161

160162
const KV1 cmdSeq{fs::readFileText(path)};
@@ -266,7 +268,7 @@ std::vector<std::byte> CmdSeq::bake() const {
266268
return {};
267269
}
268270

269-
bool CmdSeq::bake(const std::string& path) const {
271+
bool CmdSeq::bake(const std::filesystem::path& path) const {
270272
FileStream writer{path};
271273
if (!writer) {
272274
return false;

0 commit comments

Comments
 (0)