V-Gears 0
Free Final Fantasy VII engine.
ObjectFactory.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The V-Gears Team
3 *
4 * This file is part of V-Gears
5 *
6 * V-Gears is free software: you can redistribute it and/or modify it under
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, version 3.0 (GPLv3) of the License.
9 *
10 * V-Gears is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#pragma once
17
18#include <map>
19#include <string>
20#include "common/scummsys.h"
21
25template<typename BaseType, typename Type> BaseType *CreateObject(){return new Type();}
26
30template<typename KeyType, typename BaseType> class ObjectFactory{
31
32 public:
38 template<typename Type> void AddEntry(const KeyType &key) {
39 registry_[key] = &CreateObject<BaseType, Type>;
40 }
41
49 BaseType *Create(const KeyType &key) const{
50 typename RegistryMap::const_iterator entry = registry_.find(key);
51 if (entry == registry_.end()) return NULL;
52 return (entry->second)();
53 }
54
55 private:
56
60 typedef BaseType *(*CreateFunc)();
61
65 typedef std::map<KeyType, CreateFunc> RegistryMap;
66
71};
72
BaseType * CreateObject()
Template function for creating an instance of Type.
Definition: ObjectFactory.h:25
Generic factory for a class and its subclasses.
Definition: ObjectFactory.h:30
std::map< KeyType, CreateFunc > RegistryMap
Type used to store registered entries.
Definition: ObjectFactory.h:65
RegistryMap registry_
Map from an identifier to a creation function.
Definition: ObjectFactory.h:70
BaseType * Create(const KeyType &key) const
Creates an instance of some registered class.
Definition: ObjectFactory.h:49
void AddEntry(const KeyType &key)
Register a new entry.
Definition: ObjectFactory.h:38