aboutsummaryrefslogtreecommitdiff
path: root/src/utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.h')
-rw-r--r--src/utils.h31
1 files changed, 19 insertions, 12 deletions
diff --git a/src/utils.h b/src/utils.h
index f7366dc..9f4cf5c 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -1,16 +1,34 @@
#pragma once
#include "json.hpp"
+#include "settings.h"
#include <string>
#include <vector>
#include <map>
#include <sstream>
-
+#include <memory>
+#include <stdexcept>
namespace utils {
nlohmann::json loadJson(const std::string& 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);
+
+ // looks up directory in settings. Returns element matching name.
+ nlohmann::json loadJson(const std::string& type, const std::string& name);
void saveJson(const nlohmann::json& data, const std::string& path);
+ 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));
+ } catch(std::invalid_argument& e) {
+ // Fall back on the data passed in
+ return std::shared_ptr<S>(new D(data, data));
+ }
+ }
+
template<typename Container> std::string join(Container parts, std::string joiner) {
std::stringstream out;
bool isFirst = true;
@@ -40,15 +58,4 @@ namespace utils {
std::vector<std::pair<std::string, int>> copper2coins(int coppers);
std::string getCostString(int coppers);
-
- // Recursively loads all .json files under directory
- /*std::vector<Item> loadAll(std::string directory) {
- std::vector<Item> items;
- for(auto path : std::filesystem::recursive_directory_iterator(directory)) {
- if(path.path().extension() == ".json") {
- items.push_back(Item::instance(loadJson(path.path())));
- }
- }
- return items;
- }*/
}