votca 2024-dev
Loading...
Searching...
No Matches
objectfactory.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18#ifndef VOTCA_TOOLS_OBJECTFACTORY_H
19#define VOTCA_TOOLS_OBJECTFACTORY_H
20
21// Standard includes
22#include <map>
23#include <memory>
24#include <stdexcept>
25#include <utility>
26#include <vector>
27
28// Third party includes
29#include <boost/lexical_cast.hpp>
30
31namespace votca {
32namespace tools {
33
43template <typename key_t, typename T, typename... args_t>
45 public:
46 using creator_t = std::unique_ptr<T> (*)(args_t &&...);
47 ObjectFactory() = default;
48 virtual ~ObjectFactory() = default;
49
59 void Register(const key_t &key, creator_t creator);
60
61 template <typename obj_t>
62 void Register(const key_t &key);
63
67 virtual std::unique_ptr<T> Create(const key_t &key, args_t &&...arguments);
68 bool IsRegistered(const key_t &id_) const;
69
70 std::vector<key_t> getKeys() const {
71 std::vector<key_t> key;
72 key.reserve(objects_.size());
73 for (const auto &pair : objects_) {
74 key.push_back(pair.first);
75 }
76 return key;
77 }
78
79 private:
80 std::map<key_t, creator_t> objects_;
81};
82
83template <class parent, class T, typename... args_t>
84std::unique_ptr<parent> create_policy_new(args_t &&...args) {
85 return std::make_unique<T>(std::forward<args_t>(args)...);
86}
87
88template <typename key_t, typename T, typename... args_t>
90 creator_t creator) {
91 objects_.insert(
92 typename std::map<key_t, creator_t>::value_type(key, creator));
93}
94
95template <typename key_t, typename T, typename... args_t>
96template <typename obj_t>
97inline void ObjectFactory<key_t, T, args_t...>::Register(const key_t &key) {
98 Register(key, create_policy_new<T, obj_t>);
99}
100
101template <typename key_t, typename T, typename... args_t>
103 const key_t &key, args_t &&...arguments) {
104 typename std::map<key_t, creator_t>::const_iterator it = objects_.find(key);
105 if (it != objects_.end()) {
106 return (it->second)(std::forward<args_t>(arguments)...);
107 } else {
108 throw std::runtime_error(
109 "factory key " + boost::lexical_cast<std::string>(key) + " not found.");
110 }
111}
112
113template <typename key_t, typename T, typename... args_t>
115 const key_t &id_) const {
116 return (objects_.find(id_) != objects_.end());
117}
118
119} // namespace tools
120} // namespace votca
121
122#endif // VOTCA_TOOLS_OBJECTFACTORY_H
template class for object factory
std::map< key_t, creator_t > objects_
std::unique_ptr< T >(*)(args_t &&...) creator_t
bool IsRegistered(const key_t &id_) const
void Register(const key_t &key, creator_t creator)
register an object
virtual ~ObjectFactory()=default
virtual std::unique_ptr< T > Create(const key_t &key, args_t &&...arguments)
std::vector< key_t > getKeys() const
void Register(const key_t &key)
std::unique_ptr< parent > create_policy_new(args_t &&...args)
base class for all analysis tools
Definition basebead.h:33