Symphony Of Empires
manager.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // manager.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <stdexcept>
28 #include <unordered_set>
29 
30 // Generic manager for any resource type, the manager will call "load" if an element
31 // with the same ident already exists
32 template<typename K, typename V>
33 class Manager {
34  std::unordered_set<std::pair<K, std::shared_ptr<V>>> elems;
35 public:
38 
40  inline static Manager<K, V>& get_instance() {
41  return *singleton;
42  }
43 
45  virtual std::shared_ptr<V> load(const K& key) {}
46 
49  virtual const T& get(const K& key) {
50  auto it = elems.find(name);
51  if(it != elems.end()) return it->second;
52  std::shared_ptr<V> new_elem;
53  new_elem = elems[key] = load(key);
54  return new_elem;
55  }
56 };
virtual const T & get(const K &key)
Obtain an element or construct a new one from a provided construct which accepts key.
Definition: manager.hpp:49
static Manager< K, V > & get_instance()
Return the singleton.
Definition: manager.hpp:40
static Manager< K, V > * singleton
Global manager object.
Definition: manager.hpp:37
virtual std::shared_ptr< V > load(const K &key)
Load an element, this is the function that must be defined by the inheritor.
Definition: manager.hpp:45