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  ObjectHandle::Ptr doCreate( const std::string_view& classKeyword ) override;
68 
70  ~DefaultObjectFactory() override
71  { /* Could clean up, but ... */
72  }
73 
74  // Internal helper classes
75 
76  class ObjectCreatorBase
77  {
78  public:
79  ObjectCreatorBase() {}
80  virtual ~ObjectCreatorBase() {}
81  virtual ObjectHandle::Ptr create() = 0;
82  };
83 
84  template <typename ObjectBaseDerivative>
85  class ObjectCreator : public ObjectCreatorBase
86  {
87  public:
88  ObjectHandle::Ptr create() override { return std::make_shared<ObjectBaseDerivative>(); }
89  };
90 
91  // Map to store factory
92  std::map<std::string, ObjectCreatorBase*, std::less<>> m_factoryMap;
93 };
94 
95 } // End of namespace caffa
Definition: cafDefaultObjectFactory.h:41
std::list< std::string > classes() const
ObjectFactory implementations.
Definition: cafDefaultObjectFactory.cpp:9
Definition: cafObjectFactory.h:39
Main Caffa namespace.
Definition: __init__.py:1