libnick 2024.12.0
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
datafilemanager.h
Go to the documentation of this file.
1
23#ifndef DATAFILEMANAGER_H
24#define DATAFILEMANAGER_H
25
26#include <memory>
27#include <string>
28#include <type_traits>
29#include <unordered_map>
30#include "datafilebase.h"
31
32namespace Nickvision::App
33{
34 template<typename T>
35 concept DerivedDataFileBase = std::is_base_of_v<DataFileBase, T>;
36
41 {
42 public:
47 DataFileManager(const std::string& appName);
48 // Delete copy and move constructors and assignment operators
51 void operator=(const DataFileManager&) = delete;
52 void operator=(DataFileManager&&) = delete;
60 template<DerivedDataFileBase T>
61 T& get(const std::string& key)
62 {
63 if (key.empty())
64 {
65 throw std::invalid_argument("Key must not be empty.");
66 }
67 if (!m_files.contains(key))
68 {
69 m_files[key] = std::make_unique<T>(key, m_appName);
70 }
71 return *static_cast<T*>(m_files[key].get());
72 }
73
74 private:
75 std::string m_appName;
76 std::unordered_map<std::string, std::unique_ptr<DataFileBase>> m_files;
77 };
78}
79
80#endif //DATAFILEMANAGER_H
A manager of data files for an application.
Definition datafilemanager.h:41
void operator=(DataFileManager &&)=delete
DataFileManager(DataFileManager &&)=delete
DataFileManager(const DataFileManager &)=delete
T & get(const std::string &key)
Gets a data object.
Definition datafilemanager.h:61
DataFileManager(const std::string &appName)
Constructs a DataFileManager.
void operator=(const DataFileManager &)=delete
Definition datafilemanager.h:35
Definition appinfo.h:32