/home/runner/work/amr-wind/amr-wind/amr-wind/core/CollMgr.H Source File

AMR-Wind API: /home/runner/work/amr-wind/amr-wind/amr-wind/core/CollMgr.H Source File
AMR-Wind API v0.1.0
CFD solver for wind plant simulations
Loading...
Searching...
No Matches
CollMgr.H
Go to the documentation of this file.
1#ifndef COLLMGR_H
2#define COLLMGR_H
3
4#include <string>
5#include <memory>
6#include <unordered_map>
7#include "AMReX_Vector.H"
8
9namespace amr_wind {
10
20template <typename Collection, typename Type, bool allow_duplicates = false>
22{
23public:
24 using TypePtr = std::unique_ptr<Type>;
25 using TypeVector = amrex::Vector<TypePtr>;
26
27 CollMgr() = default;
28
29 ~CollMgr() = default;
30
31 CollMgr(const CollMgr&) = delete;
32 CollMgr& operator=(const CollMgr&) = delete;
33
37 template <class... Args>
38 Type& create(const std::string& key, Args&&... args)
39 {
40 if (!allow_duplicates && contains(key)) {
41 return operator()(key);
42 }
43
44 m_obj_vec.emplace_back(Type::create(key, std::forward<Args>(args)...));
45 m_obj_map[key] = m_obj_vec.size() - 1;
46
47 return *m_obj_vec.back();
48 }
49
52 const TypeVector& objects() const { return m_obj_vec; }
53
55 bool contains(const std::string& key) const
56 {
57 auto it = m_obj_map.find(key);
58 return (it != m_obj_map.end());
59 }
60
62 Type& operator()(const std::string& key)
63 {
64 return *m_obj_vec[m_obj_map.at(key)];
65 }
66
67 const Type& operator()(const std::string& key) const
68 {
69 return *m_obj_vec[m_obj_map.at(key)];
70 }
71
73 template <typename T>
74 T& operator()(const std::string& key)
75 {
76 return dynamic_cast<T&>(operator()(key));
77 }
78
79protected:
82
84 std::unordered_map<std::string, int> m_obj_map;
85};
86
87} // namespace amr_wind
88
89#endif /* COLLMGR_H */
Definition CollMgr.H:22
~CollMgr()=default
TypeVector & objects()
Return a vector of the registered objects.
Definition CollMgr.H:51
Type & operator()(const std::string &key)
Return the object corresponding to a lookup key.
Definition CollMgr.H:62
amrex::Vector< TypePtr > TypeVector
Definition CollMgr.H:25
bool contains(const std::string &key) const
Query if an object exists using the lookup key.
Definition CollMgr.H:55
std::unique_ptr< Type > TypePtr
Definition CollMgr.H:24
CollMgr & operator=(const CollMgr &)=delete
T & operator()(const std::string &key)
Return object for a lookup key cast into its exact class definition.
Definition CollMgr.H:74
CollMgr(const CollMgr &)=delete
TypeVector m_obj_vec
Collection of objects registered so far.
Definition CollMgr.H:81
const TypeVector & objects() const
Definition CollMgr.H:52
Type & create(const std::string &key, Args &&... args)
Definition CollMgr.H:38
const Type & operator()(const std::string &key) const
Definition CollMgr.H:67
std::unordered_map< std::string, int > m_obj_map
Key word based lookup.
Definition CollMgr.H:84
Definition BCInterface.cpp:7