aboutsummaryrefslogtreecommitdiff
path: root/src/utils.h
diff options
context:
space:
mode:
authorYour Name <you@example.com>2021-05-06 14:13:28 -0400
committerYour Name <you@example.com>2021-05-06 14:13:28 -0400
commit9f3802690f9dd9452e96d1d7a879291978d66e35 (patch)
tree6d6c17b39abdb9490119241bc4fc061744b46d7d /src/utils.h
parent2a9f262e6db5906db445d465e500d7ba8c90fab3 (diff)
downloaddmtool-9f3802690f9dd9452e96d1d7a879291978d66e35.tar.gz
dmtool-9f3802690f9dd9452e96d1d7a879291978d66e35.tar.bz2
dmtool-9f3802690f9dd9452e96d1d7a879291978d66e35.zip
Refactoring
Diffstat (limited to 'src/utils.h')
-rw-r--r--src/utils.h58
1 files changed, 55 insertions, 3 deletions
diff --git a/src/utils.h b/src/utils.h
index bf789e1..dea052b 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -1,15 +1,17 @@
#pragma once
#include "json.hpp"
+#include "entry.h"
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <memory>
#include <stdexcept>
+#include <filesystem>
namespace utils {
- nlohmann::json loadJson(const std::string& path);
-
+ nlohmann::json loadJson(const std::filesystem::path& path);
+
// Recursively loads all .json files under directory
// If called multiple times with same directory, returns a cached result.
std::vector<nlohmann::json> loadAllJson(const std::string& directory);
@@ -20,11 +22,31 @@ namespace utils {
// goes through the available types and searches for the one matching name.
nlohmann::json findByName(const std::string& name);
- void saveJson(const nlohmann::json& data, const std::string& path);
+ void saveJson(const nlohmann::json& data, const std::filesystem::path& path);
// converts in-place
std::string lower(std::string& in);
+ template<typename T> std::shared_ptr<T> instantiate(const std::filesystem::path& path) {
+ std::shared_ptr<entry::Entry> ent;
+ try {
+ ent = entry::Entry::create(loadJson(path));
+ } catch(std::exception& e) {
+ if(std::filesystem::directory_entry(path).exists()) {
+ throw std::runtime_error("Invalid json: " + path.string());
+ } else {
+ throw std::runtime_error("No such file nor directory: " + path.string());
+ }
+ }
+ std::shared_ptr<T> t = std::dynamic_pointer_cast<T>(ent);
+ if(! t) {
+ throw std::runtime_error("Wrong instance type: " + ent->getType());
+ }
+ return t;
+ }
+
+ int parseInt(const std::string& s);
+
template<typename S, typename D> std::shared_ptr<S> loadDFromJson(const nlohmann::json& data) {
try {
return std::shared_ptr<S>(new D(loadJson(data["type"], data["name"]), data));
@@ -78,4 +100,34 @@ namespace utils {
std::string getCostString(int coppers);
std::string toOrdinal(std::size_t number);
+
+ template<typename T> std::vector<T> json2vec(const nlohmann::json& data) {
+ using std::begin; using std::end;
+ return std::vector<T>(begin(data), end(data));
+ }
+
+ template<typename T> std::vector<std::shared_ptr<T>> jsonList2ptrvec(const std::string& type, const std::vector<std::string>& names) {
+ std::vector<std::shared_ptr<T>> ret;
+ for(auto name : names) {
+ auto j = utils::loadJson(type, name);
+ ret.push_back(std::shared_ptr<T>(new T(j, j)));
+ }
+ return ret;
+ }
+
+ template<typename T> std::vector<std::shared_ptr<T>> json2ptrvec(const nlohmann::json& data) {
+ std::vector<std::shared_ptr<T>> ret;
+ for(nlohmann::json d : data) {
+ ret.push_back(T::create(d));
+ }
+ return ret;
+ }
+
+ template<typename T> std::vector<nlohmann::json> ptrvec2json(std::vector<T> src) {
+ std::vector<nlohmann::json> ret;
+ for(T i : src) {
+ ret.push_back(i->toJson());
+ }
+ return ret;
+ }
}