Coverage Report

Created: 2025-06-13 06:07

/src/poco/Foundation/include/Poco/DynamicFactory.h
Line
Count
Source (jump to first uncovered line)
1
//
2
// DynamicFactory.h
3
//
4
// Library: Foundation
5
// Package: Core
6
// Module:  DynamicFactory
7
//
8
// Definition of the DynamicFactory class.
9
//
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11
// and Contributors.
12
//
13
// SPDX-License-Identifier: BSL-1.0
14
//
15
16
17
#ifndef Foundation_DynamicFactory_INCLUDED
18
#define Foundation_DynamicFactory_INCLUDED
19
20
21
#include "Poco/Foundation.h"
22
#include "Poco/Instantiator.h"
23
#include "Poco/Exception.h"
24
#include "Poco/Mutex.h"
25
#include <map>
26
#include <memory>
27
28
29
namespace Poco {
30
31
32
template <class Base>
33
class DynamicFactory
34
  /// A factory that creates objects by class name.
35
{
36
public:
37
  using AbstractFactory = AbstractInstantiator<Base>;
38
39
  DynamicFactory()
40
    /// Creates the DynamicFactory.
41
47.5k
    = default;
42
43
  ~DynamicFactory()
44
    /// Destroys the DynamicFactory and deletes the instantiators for
45
    /// all registered classes.
46
47.5k
  {
47
47.5k
    for (auto& p: _map)
48
428k
    {
49
428k
      delete p.second;
50
428k
    }
51
47.5k
  }
52
53
  DynamicFactory(const DynamicFactory&) = delete;
54
  DynamicFactory& operator=(const DynamicFactory&) = delete;
55
56
  Base* createInstance(const std::string& className) const
57
    /// Creates a new instance of the class with the given name.
58
    /// The class must have been registered with registerClass.
59
    /// If the class name is unknown, a NotFoundException is thrown.
60
47.5k
  {
61
47.5k
    FastMutex::ScopedLock lock(_mutex);
62
63
47.5k
    typename FactoryMap::const_iterator it = _map.find(className);
64
47.5k
    if (it != _map.end())
65
47.5k
      return it->second->createInstance();
66
0
    else
67
0
      throw NotFoundException(className);
68
47.5k
  }
69
70
  template <class C>
71
  void registerClass(const std::string& className)
72
    /// Registers the instantiator for the given class with the DynamicFactory.
73
    /// The DynamicFactory takes ownership of the instantiator and deletes
74
    /// it when it's no longer used.
75
    /// If the class has already been registered, an ExistsException is thrown
76
    /// and the instantiator is deleted.
77
428k
  {
78
428k
    registerClass(className, new Instantiator<C, Base>);
79
428k
  }
void Poco::DynamicFactory<Poco::JWT::Algorithm>::registerClass<Poco::JWT::HMACAlgorithm<Poco::JWT::SHA256Engine> >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
77
47.5k
  {
78
47.5k
    registerClass(className, new Instantiator<C, Base>);
79
47.5k
  }
void Poco::DynamicFactory<Poco::JWT::Algorithm>::registerClass<Poco::JWT::HMACAlgorithm<Poco::JWT::SHA384Engine> >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
77
47.5k
  {
78
47.5k
    registerClass(className, new Instantiator<C, Base>);
79
47.5k
  }
void Poco::DynamicFactory<Poco::JWT::Algorithm>::registerClass<Poco::JWT::HMACAlgorithm<Poco::JWT::SHA512Engine> >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
77
47.5k
  {
78
47.5k
    registerClass(className, new Instantiator<C, Base>);
79
47.5k
  }
void Poco::DynamicFactory<Poco::JWT::Algorithm>::registerClass<Poco::JWT::RS256>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
77
47.5k
  {
78
47.5k
    registerClass(className, new Instantiator<C, Base>);
79
47.5k
  }
void Poco::DynamicFactory<Poco::JWT::Algorithm>::registerClass<Poco::JWT::RS384>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
77
47.5k
  {
78
47.5k
    registerClass(className, new Instantiator<C, Base>);
79
47.5k
  }
void Poco::DynamicFactory<Poco::JWT::Algorithm>::registerClass<Poco::JWT::RS512>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
77
47.5k
  {
78
47.5k
    registerClass(className, new Instantiator<C, Base>);
79
47.5k
  }
void Poco::DynamicFactory<Poco::JWT::Algorithm>::registerClass<Poco::JWT::ES256>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
77
47.5k
  {
78
47.5k
    registerClass(className, new Instantiator<C, Base>);
79
47.5k
  }
void Poco::DynamicFactory<Poco::JWT::Algorithm>::registerClass<Poco::JWT::ES384>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
77
47.5k
  {
78
47.5k
    registerClass(className, new Instantiator<C, Base>);
79
47.5k
  }
void Poco::DynamicFactory<Poco::JWT::Algorithm>::registerClass<Poco::JWT::ES512>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
77
47.5k
  {
78
47.5k
    registerClass(className, new Instantiator<C, Base>);
79
47.5k
  }
80
81
  void registerClass(const std::string& className, AbstractFactory* pAbstractFactory)
82
    /// Registers the instantiator for the given class with the DynamicFactory.
83
    /// The DynamicFactory takes ownership of the instantiator and deletes
84
    /// it when it's no longer used.
85
    /// If the class has already been registered, an ExistsException is thrown
86
    /// and the instantiator is deleted.
87
428k
  {
88
428k
    poco_check_ptr (pAbstractFactory);
89
90
428k
    FastMutex::ScopedLock lock(_mutex);
91
92
428k
    std::unique_ptr<AbstractFactory> ptr(pAbstractFactory);
93
94
428k
    typename FactoryMap::iterator it = _map.find(className);
95
428k
    if (it == _map.end())
96
428k
      _map[className] = ptr.release();
97
0
    else
98
0
      throw ExistsException(className);
99
428k
  }
100
101
  void unregisterClass(const std::string& className)
102
    /// Unregisters the given class and deletes the instantiator
103
    /// for the class.
104
    /// Throws a NotFoundException if the class has not been registered.
105
  {
106
    FastMutex::ScopedLock lock(_mutex);
107
108
    typename FactoryMap::iterator it = _map.find(className);
109
    if (it != _map.end())
110
    {
111
      delete it->second;
112
      _map.erase(it);
113
    }
114
    else throw NotFoundException(className);
115
  }
116
117
  bool isClass(const std::string& className) const
118
    /// Returns true iff the given class has been registered.
119
47.5k
  {
120
47.5k
    FastMutex::ScopedLock lock(_mutex);
121
122
47.5k
    return _map.find(className) != _map.end();
123
47.5k
  }
124
125
private:
126
  using FactoryMap = std::map<std::string, AbstractFactory *>;
127
128
  FactoryMap _map;
129
  mutable FastMutex _mutex;
130
};
131
132
133
} // namespace Poco
134
135
136
#endif // Foundation_DynamicFactory_INCLUDED