SERiF 0.0.1a
3+1D Stellar Structure and Evolution
Loading...
Searching...
No Matches
resourceManager.cpp
Go to the documentation of this file.
1/* ***********************************************************************
2//
3// Copyright (C) 2025 -- The 4D-STAR Collaboration
4// File Author: Emily Boudreaux
5// Last Modified: March 20, 2025
6//
7// 4DSSE is free software; you can use it and/or modify
8// it under the terms and restrictions the GNU General Library Public
9// License version 3 (GPLv3) as published by the Free Software Foundation.
10//
11// 4DSSE is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14// See the GNU Library General Public License for more details.
15//
16// You should have received a copy of the GNU Library General Public License
17// along with this software; if not, write to the Free Software
18// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19//
20// *********************************************************************** */
21#include <iostream>
22#include <vector>
23#include <string>
24#include <filesystem>
25
26#include "quill/LogMacros.h"
27
28#include "resourceManager.h"
30#include "debug.h"
31
32#include "config.h"
33
34#define STRINGIFY(x) #x
35#define TOSTRING(x) STRINGIFY(x)
36
37namespace serif::resource {
39 const std::string defaultDataDir = TOSTRING(DATA_DIR);
40 m_dataDir = m_config.get<std::string>("Data:Dir", defaultDataDir);
41 // -- Get the index file path using filesystem to make it a system safe path
42 const std::string indexFilePath = m_dataDir + "/index.yaml";
43 // TODO Add checks to make sure data dir exists and index.yaml exists
44 const std::filesystem::path indexFile(indexFilePath);
45
46 m_resourceConfig.loadConfig(indexFile.string());
47 for (const auto& key : m_resourceConfig.keys() ) {
48 load(key);
49 }
50 }
51
52
53 std::vector<std::string> ResourceManager::getAvailableResources() const {
54 const std::vector<std::string> resources = m_resourceConfig.keys();
55 return resources;
56 }
57
58 const serif::resource::types::Resource& ResourceManager::getResource(const std::string &name) const {
59 if (const auto it = m_resources.find(name); it != m_resources.end()) {
60 return it->second;
61 }
62 throw std::runtime_error("Resource " + name + " not found");
63 }
64
65
66 bool ResourceManager::loadResource(std::string& name) {
67 return load(name);
68 }
69
70 bool ResourceManager::load(const std::string& name) {
71 // TODO : make this path system more system agnostic
72 const std::string resourcePath = m_dataDir + "/" + m_resourceConfig.get<std::string>(name);
73 const std::filesystem::path resourceFile(resourcePath);
74 if (!std::filesystem::exists(resourceFile)) {
75 LOG_ERROR(m_logger, "Resource file not found: {}", resourceFile.string());
76 return false;
77 }
78 LOG_INFO(m_logger, "Loading resource: {}", resourceFile.string());
79 if (m_resources.contains(name)) {
80 LOG_INFO(m_logger, "Resource already loaded: {}", name);
81 return true;
82 }
84 m_resources[name] = std::move(resource);
85 // -- Check if the resource is already in the map
86 return true;
87 }
88}
89
serif::config::Config & m_config
const types::Resource & getResource(const std::string &name) const
Gets a resource by name.
serif::config::Config m_resourceConfig
ResourceManager()
Private constructor to prevent instantiation.
std::unordered_map< std::string, types::Resource > m_resources
std::vector< std::string > getAvailableResources() const
Gets a list of available resources.
bool load(const std::string &name)
Loads a resource by name.
bool loadResource(std::string &name)
Loads a resource by name.
Defines a macro for triggering a breakpoint in different compilers and platforms.
Resource createResource(const std::string &type, const std::string &path)
Creates a resource based on the specified type and path.
std::variant< std::unique_ptr< opat::OPAT >, std::unique_ptr< serif::mesh::MeshIO >, std::unique_ptr< serif::eos::EOSio > > Resource
A variant type that can hold different types of resources.
#define TOSTRING(x)
Defines types and functions for managing resources.