STIR 6.4.0
FactoryRegistry.inl
Go to the documentation of this file.
1//
2//
12/*
13 Copyright (C) 2001- 2009, Hammersmith Imanet Ltd
14 This file is part of STIR.
15
16 SPDX-License-Identifier: Apache-2.0
17
18 See STIR/LICENSE.txt for details
19*/
20
21#include <utility>
22#include <sstream>
23#include "stir/warning.h"
24#include "stir/error.h"
25
26START_NAMESPACE_STIR
27
28template <class Key, class Factory, class Compare>
32
33template <class Key, class Factory, class Compare>
34FactoryRegistry<Key, Factory, Compare>::FactoryRegistry(const Key& default_key, const Factory& default_factory)
35 : has_defaults(true),
36 default_key(default_key),
37 default_factory(default_factory)
38{
39 add_to_registry(default_key, default_factory);
40}
41
42template <class Key, class Factory, class Compare>
43FactoryRegistry<Key, Factory, Compare>::~FactoryRegistry()
44{
45 // TODO not so sure how to get rid of them
46 // we can only use delete if the factories are allocated with new
47 // and that's currently not the case
48 // for (FactoryMap::iterator i = m.begin(); i != m.end(); ++i)
49 // delete i->second;
50}
51
52template <class Key, class Factory, class Compare>
53void
54FactoryRegistry<Key, Factory, Compare>::add_to_registry(const Key& key, Factory const& factory)
55{
56 // cerr << "Adding "<< key << "to registry\n";
57#ifndef NDEBUG
58 typename FactoryMap::iterator iter = m.find(key);
59 if (iter != m.end())
60 {
61 std::ostringstream s;
62 s << "FactoryRegistry:: overwriting previous value of key in registry.\n key: " << key;
63 warning(s.str());
64 }
66#endif
67 m.insert(std::pair<Key, Factory>(key, factory));
68}
69
70template <class Key, class Factory, class Compare>
71void
73{
74 // cerr << "Removing "<< key << "to registry\n";
75 typename FactoryMap::iterator iter = m.find(key);
76 if (iter == m.end())
77 {
78#ifndef _NDEBUG
79 // TODO don't output to cerr, but use only warning()
80 std::ostringstream s;
81 s << "FactoryRegistry:: Attempt to remove key from registry, but it's not in there...\n key: " << key;
82 warning(s.str());
83#endif
84 }
85 else
86 m.erase(iter);
87}
88
89template <class Key, class Factory, class Compare>
90void
92{
93 for (typename FactoryMap::const_iterator i = m.begin(); i != m.end(); ++i)
94 s << i->first << '\n';
95}
96
97template <class Key, class Factory, class Compare>
98Factory const&
99FactoryRegistry<Key, Factory, Compare>::find_factory(const Key& key) const /*throw(unknown_typename)*/
100{
101 typename FactoryMap::const_iterator i = m.find(key);
102 if (i != m.end())
103 return i->second;
104
105 // key not found
106
107 // throw(unknown_typename(key));
108 // TODO don't output to cerr, but use only error()
109 std::cerr << "FactoryRegistry: key " << key << " not found in current registry\n" << m.size() << " possible values are:\n";
110 list_keys(std::cerr);
111 std::cerr << '\n';
112 if (has_defaults)
113 {
114 std::cerr << "Using value corresponding to key \"" << default_key << "\"" << std::endl;
115 return default_factory;
116 }
117 else
118 {
119 error("FactoryRegistry: aborting");
120 // stupid line to prevent warning messages of good compilers
121 return default_factory;
122 }
123}
124
125END_NAMESPACE_STIR
FactoryRegistry()
Default constructor without defaults (see find_factory())
Definition FactoryRegistry.inl:29
Factory const & find_factory(const Key &key) const
Find a factory corresponding to a key.
Definition FactoryRegistry.inl:99
void remove_from_registry(const Key &key)
Remove a pair from the registry.
Definition FactoryRegistry.inl:72
void list_keys(std::ostream &s) const
List all keys to an ostream, separated by newlines.
Definition FactoryRegistry.inl:91
void add_to_registry(const Key &key, Factory const &factory)
Add a pair to the registry.
Definition FactoryRegistry.inl:54
Declaration of stir::error()
void error(const char *const s,...)
Print error with format string a la printf and throw exception.
Definition error.cxx:42
void warning(const char *const s,...)
Print warning with format string a la printf.
Definition warning.cxx:41
Declaration of stir::warning()