Atlas 0.7.0
Networking protocol for the Worldforge system.
Factories.h
1/*
2 Copyright 2000-2001 Aloril.
3 Copyright 2001-2005 Alistair Riddoch.
4 Copyright (C) 2019 Erik Ogenvik
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program 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. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#ifndef ATLAS_C_FACTORIES_H
22#define ATLAS_C_FACTORIES_H
23
24
25#include <Atlas/Objects/Root.h>
26#include <Atlas/Objects/SmartPtr.h>
27
28#include <string>
29#include <list>
30#include <map>
31
32namespace Atlas {
33namespace Objects {
34
35template<class T>
36static SmartPtr <RootData> factory(const std::string&, int) {
37 return SmartPtr<T>();
38}
39
40template<class T>
41static SmartPtr <RootData> defaultInstance(const std::string&, int) {
42 return T::allocator.getDefaultObjectInstance();
43}
44
45
46SmartPtr <RootData> generic_factory(const std::string& name, int no);
47
48SmartPtr <RootData> anonymous_factory(const std::string& name, int no);
49
50typedef Root (* FactoryMethod)(const std::string&, int);
51
52typedef Root (* DefaultInstanceMethod)(const std::string&, int);
53
57struct Factory {
58public:
62 FactoryMethod factory_method;
63
67 DefaultInstanceMethod default_instance_method;
68
73};
74typedef std::map<const std::string, Factory> FactoryMap;
75
76class Factories {
77public:
78
79 Factories();
80
81 Factories(const Factories&) = default;
82
83 ~Factories();
84
85 bool hasFactory(const std::string& name) const;
86
87 Root createObject(const std::string& name) const;
88
96 Root createObject(const Atlas::Message::MapType& msg) const;
97
103 Root createObject(Atlas::Message::MapType&& msg) const;
104
105 Root getDefaultInstance(const std::string& name) const;
106
107 std::list<std::string> getKeys() const;
108
109 int addFactory(const std::string& name, FactoryMethod method, DefaultInstanceMethod defaultInstanceMethod);
110
111 void installStandardTypes();
112
118 std::vector<Root> parseListOfObjects(const Atlas::Message::ListType& val) const;
119
120 std::vector<Root> parseListOfObjects(Atlas::Message::ListType&& val) const;
121
122private:
123
124 static int enumMax;
125
126 FactoryMap m_factories;
127
128
129 Root instantiateObject(const Atlas::Message::MapType& msg) const;
130
131
140 void addFactory(const std::string& name, FactoryMethod method, DefaultInstanceMethod defaultInstanceMethod, int classno);
141
151 template<typename T>
152 void addFactory(const std::string& name, int classno);
153};
154
155template<typename T>
156void Factories::addFactory(const std::string& name, int classno) {
157 addFactory(name, &factory<T>, &defaultInstance<T>, classno);
158}
159
160}
161} // namespace Atlas::Objects
162
163#endif //ATLAS_C_FACTORIES_H
Root createObject(const Atlas::Message::MapType &msg) const
std::vector< Root > parseListOfObjects(const Atlas::Message::ListType &val) const
Definition: Factories.cpp:151
Definition: Bridge.h:20
FactoryMethod factory_method
Definition: Factories.h:62
DefaultInstanceMethod default_instance_method
Definition: Factories.h:67