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 
32 namespace Atlas {
33 namespace Objects {
34 
35 template<class T>
36 static SmartPtr <RootData> factory(const std::string&, int) {
37  return SmartPtr<T>();
38 }
39 
40 template<class T>
41 static SmartPtr <RootData> defaultInstance(const std::string&, int) {
42  return T::allocator.getDefaultObjectInstance();
43 }
44 
45 
46 SmartPtr <RootData> generic_factory(const std::string& name, int no);
47 
48 SmartPtr <RootData> anonymous_factory(const std::string& name, int no);
49 
50 typedef Root (* FactoryMethod)(const std::string&, int);
51 
52 typedef Root (* DefaultInstanceMethod)(const std::string&, int);
53 
57 struct Factory {
58 public:
62  FactoryMethod factory_method;
63 
67  DefaultInstanceMethod default_instance_method;
68 
72  int classno;
73 };
74 typedef std::map<const std::string, Factory> FactoryMap;
75 
76 class Factories {
77 public:
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 
122 private:
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 
155 template<typename T>
156 void 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
Atlas::Objects::Factory::factory_method
FactoryMethod factory_method
Definition: Factories.h:62
Atlas::Objects::Factory
Definition: Factories.h:57
Atlas::Objects::Factory::classno
int classno
Definition: Factories.h:72
Atlas::Objects::Factory::default_instance_method
DefaultInstanceMethod default_instance_method
Definition: Factories.h:67
Atlas::Objects::Factories
Definition: Factories.h:76
Atlas::Objects::Factories::parseListOfObjects
std::vector< Root > parseListOfObjects(const Atlas::Message::ListType &val) const
Definition: Factories.cpp:151
Atlas::Objects::SmartPtr
Definition: Decoder.h:17
Atlas
Definition: Bridge.h:20