Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafDefaultObjectFactory.h
1 // ##################################################################################################
2 //
3 // Caffa
4 // Copyright (C) 2011- Ceetron AS (Changes up until April 2021)
5 // Copyright (C) 2021- Kontur AS (Changes from April 2021 and onwards)
6 //
7 // GNU Lesser General Public License Usage
8 // This library is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation; either version 2.1 of the License, or
11 // (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful, but WITHOUT ANY
14 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE.
16 //
17 // See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
18 // for more details.
19 //
20 // ##################################################################################################
21 
22 #pragma once
23 
24 #include "cafObjectFactory.h"
25 
26 #include "cafAssert.h"
27 
28 #include <map>
29 #include <string>
30 #include <vector>
31 
32 namespace caffa
33 {
34 //==================================================================================================
38 //==================================================================================================
39 
41 {
42 public:
43  static DefaultObjectFactory* instance();
44 
45  std::string name() const override { return "Default ObjectFactory"; }
46 
47  std::list<std::string> classes() const;
48 
49  template <typename ObjectBaseDerivative>
50  bool registerCreator()
51  {
52  auto classKeyword = ObjectBaseDerivative::classKeywordStatic();
53 
54  auto entryIt = m_factoryMap.find( classKeyword );
55  if ( entryIt != m_factoryMap.end() )
56  {
57  CAFFA_ASSERT( classKeyword != entryIt->first ); // classKeyword has already been used
58  CAFFA_ASSERT( false ); // To be sure ..
59  return false; // never hit;
60  }
61  auto object = new ObjectCreator<ObjectBaseDerivative>();
62  m_factoryMap[std::string( classKeyword )] = object;
63  return true;
64  }
65 
66 private:
67  std::shared_ptr<ObjectHandle> doCreate( const std::string_view& classKeyword ) override;
68 
70  ~DefaultObjectFactory() override { /* Could clean up, but ... */ }
71 
72  // Internal helper classes
73 
74  class ObjectCreatorBase
75  {
76  public:
77  ObjectCreatorBase() {}
78  virtual ~ObjectCreatorBase() {}
79  virtual std::shared_ptr<ObjectHandle> create() = 0;
80  };
81 
82  template <typename ObjectBaseDerivative>
83  class ObjectCreator : public ObjectCreatorBase
84  {
85  public:
86  std::shared_ptr<ObjectHandle> create() override { return std::make_shared<ObjectBaseDerivative>(); }
87  };
88 
89  // Map to store factory
90  std::map<std::string, ObjectCreatorBase*, std::less<>> m_factoryMap;
91 };
92 
93 } // End of namespace caffa
Definition: cafDefaultObjectFactory.h:40
Definition: cafObjectFactory.h:36
std::list< std::string > classes() const
ObjectFactory implementations.
Definition: cafDefaultObjectFactory.cpp:9
Main Caffa namespace.
Definition: cafApplication.h:30