11#ifndef SIMCORE_FACTORY_H
22#define SIMCORE_FACTORY_H
33
4- #include < memory> // for the unique_ptr default
5- #include < string> // for the keys in the library map
6- #include < unordered_map> // for the library of prototypes
7- #include < algorithm> // for for_each call in apply
8-
9- #include < boost/core/demangle.hpp> // for demangling
4+ #include < algorithm> // for for_each call in apply
5+ #include < boost/core/demangle.hpp> // for demangling
6+ #include < memory> // for the unique_ptr default
7+ #include < string> // for the keys in the library map
8+ #include < unordered_map> // for the library of prototypes
109
1110#include " Framework/Exception/Exception.h"
1211
@@ -21,7 +20,7 @@ namespace simcore {
2120 * @tparam Prototype the type of object that this factory creates.
2221 * This should be the base class that all types in this factory derive from.
2322 * @tparam PrototypePtr the type of pointer that the factory creates
24- * @tparam PrototypeConstructorArgs parameter pack of arguments to pass
23+ * @tparam PrototypeConstructorArgs parameter pack of arguments to pass
2524 * to the object constructor.
2625 *
2726 * ## Terminology
@@ -42,10 +41,11 @@ namespace simcore {
4241 * to it in the form of a prototype-class pointer.
4342 *
4443 * ### Declaration
45- * Using an
46- * [unnamed namespace](https://en.cppreference.com/w/cpp/language/namespace#Unnamed_namespaces)
47- * defines the variables inside it as having internal linkage and as implicitly
48- * static. Having internal linkage allows us to have repeat variable names
44+ * Using an
45+ * [unnamed
46+ namespace](https://en.cppreference.com/w/cpp/language/namespace#Unnamed_namespaces)
47+ * defines the variables inside it as having internal linkage and as implicitly
48+ * static. Having internal linkage allows us to have repeat variable names
4949 * across different source files. Being static means that the variable is
5050 * guaranteed to be constructed during library load time.
5151 *
@@ -61,7 +61,8 @@ namespace simcore {
6161 * ```
6262 *
6363 * The details of how this is handled is documented in
64- * [Storage Class Specifiers](https://en.cppreference.com/w/cpp/language/storage_duration).
64+ * [Storage Class
65+ Specifiers](https://en.cppreference.com/w/cpp/language/storage_duration).
6566 *
6667 * ## Usage
6768 *
@@ -82,7 +83,7 @@ namespace simcore {
8283 * #define LIBRARYENTRY_HPP
8384 * // we need the factory template
8485 * #include "Factory.h"
85- *
86+ *
8687 * // this class is our prototype
8788 * class LibraryEntry {
8889 * public:
@@ -93,7 +94,7 @@ namespace simcore {
9394 * // the factory type that we will use here
9495 * using Factory = ::fire::factory::Factory<LibraryEntry>;
9596 * }; // LibraryEntry
96- *
97+ *
9798 * // a macro to help with registering our library entries with our factory
9899 * #define DECLARE_LIBRARYENTRY(CLASS) \
99100 * namespace { \
@@ -116,10 +117,10 @@ namespace simcore {
116117 * }
117118 * };
118119 * }
119- *
120+ *
120121 * DECLARE_LIBRARYENTRY(library::Book)
121122 * ```
122- *
123+ *
123124 * ```cpp
124125 * // Podcast.cpp
125126 * #include "LibraryEntry.hpp"
@@ -133,10 +134,10 @@ namespace simcore {
133134 * };
134135 * }
135136 * }
136- *
137+ *
137138 * DECLARE_LIBRARYENTRY(library::audio::Podcast)
138139 * ```
139- *
140+ *
140141 * ```cpp
141142 * // Album.cpp
142143 * #include "LibraryEntry.hpp"
@@ -150,7 +151,7 @@ namespace simcore {
150151 * };
151152 * }
152153 * }
153- *
154+ *
154155 * DECLARE_LIBRARYENTRY(library::audio::Album)
155156 * ```
156157 *
@@ -165,9 +166,9 @@ namespace simcore {
165166 * ```cpp
166167 * // main.cxx
167168 * #include "LibraryEntry.hpp"
168- *
169+ *
169170 * int main(int argc, char* argv[]) {
170- * std::string full_cpp_name{argv[1]};
171+ * std::string full_cpp_name{argv[1]};
171172 * try {
172173 * auto entry_ptr{LibraryEntry::Factory::get().make(full_cpp_name)};
173174 * std::cout << entry_ptr->name() << std::endl;
@@ -176,7 +177,7 @@ namespace simcore {
176177 * }
177178 * }
178179 * ```
179- *
180+ *
180181 * Compiling these files together into the `fave-things` executable would
181182 * then lead to the following behavior.
182183 *
@@ -191,8 +192,7 @@ namespace simcore {
191192 * ERROR: An object named library::DoesNotExist has not been declared.
192193 * ```
193194 */
194- template <typename Prototype,
195- typename PrototypePtr,
195+ template <typename Prototype, typename PrototypePtr,
196196 typename ... PrototypeConstructorArgs>
197197class Factory {
198198 public:
@@ -237,7 +237,7 @@ class Factory {
237237 * at library load time. It relates to variables so that it cannot be
238238 * optimized away.
239239 */
240- template <typename DerivedType>
240+ template <typename DerivedType>
241241 uint64_t declare () {
242242 std::string full_name{boost::core::demangle (typeid (DerivedType).name ())};
243243 library_[full_name] = &maker<DerivedType>;
@@ -256,7 +256,8 @@ class Factory {
256256 * The arguments to the maker are determined at compiletime
257257 * using the template parameters of Factory.
258258 *
259- * @param[in] full_name name of class to create, same name as passed to declare
259+ * @param[in] full_name name of class to create, same name as passed to
260+ * declare
260261 * @param[in] maker_args parameter pack of arguments to pass on to maker
261262 *
262263 * @returns a pointer to the parent class that the objects derive from.
@@ -265,8 +266,8 @@ class Factory {
265266 PrototypeConstructorArgs... maker_args) {
266267 auto lib_it{library_.find (full_name)};
267268 if (lib_it == library_.end ()) {
268- EXCEPTION_RAISE (" SimFactory" ," An object named " + full_name+
269- " has not been declared." );
269+ EXCEPTION_RAISE (" SimFactory" , " An object named " + full_name +
270+ " has not been declared." );
270271 }
271272 warehouse_.emplace_back (lib_it->second (maker_args...));
272273 return warehouse_.back ();
@@ -278,7 +279,7 @@ class Factory {
278279 * UnaryFunction is simply passed dirctly to std::for_each so
279280 * look there for requirements upon it.
280281 */
281- template <class UnaryFunction >
282+ template <class UnaryFunction >
282283 void apply (UnaryFunction f) const {
283284 std::for_each (warehouse_.begin (), warehouse_.end (), f);
284285 }
@@ -293,9 +294,9 @@ class Factory {
293294 /* *
294295 * make a new DerivedType returning a PrototypePtr
295296 *
296- * Basically a copy of what
297- * [`std::make_unique`](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique)
298- * or
297+ * Basically a copy of what
298+ * [`std::make_unique`](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique)
299+ * or
299300 * [`std::make_shared`](https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared)
300301 * do but with the following changes:
301302 * 1. constructor arguments defined by the Factory and not here
@@ -311,7 +312,8 @@ class Factory {
311312 */
312313 template <typename DerivedType>
313314 static PrototypePtr maker (PrototypeConstructorArgs... args) {
314- return PrototypePtr (new DerivedType (std::forward<PrototypeConstructorArgs>(args)...));
315+ return PrototypePtr (
316+ new DerivedType (std::forward<PrototypeConstructorArgs>(args)...));
315317 }
316318
317319 // / private constructor to prevent creation
@@ -325,5 +327,4 @@ class Factory {
325327}; // Factory
326328
327329} // namespace simcore
328-
329330#endif // SIMCORE_FACTORY_H
0 commit comments