Atlas-C++
objectFactory.h
1 // This file may be redistributed and modified only under the terms of
2 // the GNU Lesser General Public License (See COPYING for details).
3 // Copyright (C) 2000 Aloril
4 // Copyright (C) 2005 Al Riddoch
5 
6 // $Id$
7 
8 #ifndef ATLAS_OBJECTS_OBJECTFACTORY_H
9 #define ATLAS_OBJECTS_OBJECTFACTORY_H
10 
11 #include <Atlas/Objects/Root.h>
12 #include <Atlas/Objects/SmartPtr.h>
13 
14 #include <string>
15 #include <list>
16 #include <map>
17 #include <utility>
18 
19 namespace Atlas { namespace Objects {
20 
22 {
23  protected:
24  std::string name;
25  public:
26  explicit NoSuchFactoryException(std::string factoryName) noexcept :
27  Atlas::Exception("No factory for Objects type"),
28  name(std::move(factoryName)) { }
29 
30  ~NoSuchFactoryException() noexcept override = default;
31  const std::string & getName() {
32  return name;
33  }
34 };
35 
36 template <class T>
37 static SmartPtr<RootData> factory(const std::string &, int)
38 {
39  return SmartPtr<T>();
40 }
41 
42 template <class T>
43 static SmartPtr<RootData> defaultInstance(const std::string &, int)
44 {
45  return T::allocator.getDefaultObjectInstance();
46 }
47 
48 
49 SmartPtr<RootData> generic_factory(const std::string & name, int no);
50 SmartPtr<RootData> anonymous_factory(const std::string & name, int no);
51 
52 typedef Root (*FactoryMethod)(const std::string &, int);
53 typedef Root (*DefaultInstanceMethod)(const std::string &, int);
54 
58 struct Factory
59 {
60 public:
64  FactoryMethod factory_method;
65 
69  DefaultInstanceMethod default_instance_method;
70 
74  int classno;
75 };
76 typedef std::map<const std::string, Factory > FactoryMap;
77 
78 class Factories
79 {
80 public:
81  friend class AddFactories;
82 
83  Factories() = default;
84  Factories(const Factories &) = default;
85 
86  bool hasFactory(const std::string& name);
87  Root createObject(const std::string& name);
88  Root createObject(const Atlas::Message::MapType & msg);
89  Root getDefaultInstance(const std::string& name);
90  std::list<std::string> getKeys();
91  int addFactory(const std::string& name, FactoryMethod method, DefaultInstanceMethod defaultInstanceMethod);
92 
93  static Factories * instance();
94 private:
95  FactoryMap m_factories;
96 
105  void addFactory(const std::string& name, FactoryMethod method, DefaultInstanceMethod defaultInstanceMethod, int classno);
106 
116  template <typename T>
117  void addFactory(const std::string& name, int classno);
118 };
119 
120 extern std::map<const std::string, Root> objectDefinitions;
121 
122 
123 template <typename T>
124 void Factories::addFactory(const std::string& name, int classno)
125 {
126  addFactory(name, &factory<T>, &defaultInstance<T>, classno);
127 }
128 
129 } } // namespace Atlas::Objects
130 
131 #endif // ATLAS_OBJECTS_OBJECTFACTORY_H
DefaultInstanceMethod default_instance_method
Method for accessing the default instance.
Definition: objectFactory.h:69
Definition: objectFactory.h:78
The Atlas namespace.
Definition: Bridge.h:20
Holds methods for creating new instances and accessing the default instance.
Definition: objectFactory.h:58
Definition: objectFactory.h:21
FactoryMethod factory_method
Method for creating a new instance.
Definition: objectFactory.h:64
int classno
The class number for the objects created by this factory.
Definition: objectFactory.h:74
Definition: Decoder.h:16
Base class for all exceptions thrown by Atlas-C++.
Definition: Exception.h:17

Copyright 2000-2004 the respective authors.

This document can be licensed under the terms of the GNU Free Documentation License or the GNU General Public License and may be freely distributed under the terms given by one of these licenses.