forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpara_collection.h
More file actions
83 lines (71 loc) · 2.06 KB
/
Copy pathpara_collection.h
File metadata and controls
83 lines (71 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef PARA_COLLECTION_H
#define PARA_COLLECTION_H
#include <memory>
#include <string>
#include <vector>
#include "para_world.h"
namespace Parallel
{
/**
* @brief Container for all parallel communication domains.
*
* A ParaCollection owns a set of ParaWorld objects (base class pointers),
* each describing one communication domain (see ParaTag). Callers look up
* domains by tag via find(); a missing tag yields a static empty (invalid)
* domain as a safe degradation, never an exception.
*
* The collection is passed explicitly to functions that need communicator
* access, replacing reads of loose globals such as GlobalV::POOL_WORLD.
*/
class ParaCollection
{
public:
ParaCollection() = default;
/**
* @brief Append a domain to the collection.
*
* Duplicate tags are rejected (the existing entry is kept).
*
* @param[in] world domain to add (ownership transferred)
*/
void add(std::unique_ptr<ParaWorld> world);
/**
* @brief Look up a domain by tag.
*
* @param[in] tag domain tag string
* @return the matching ParaWorld, or a static empty domain if not found
*/
const ParaWorld& find(const std::string& tag) const;
/**
* @brief Look up a domain by tag and cast to the requested subclass.
*
* @tparam T expected subclass (e.g. ParaKmeshWorld)
* @param[in] tag domain tag string
* @return pointer to the domain if found and type matches, nullptr otherwise
*/
template <typename T>
const T* find_as(const std::string& tag) const;
/**
* @brief Number of domains in the collection.
*/
size_t size() const
{
return worlds_.size();
}
private:
std::vector<std::unique_ptr<ParaWorld>> worlds_; ///< owned domains
};
template <typename T>
const T* ParaCollection::find_as(const std::string& tag) const
{
for (const auto& world : worlds_)
{
if (world->tag() == tag)
{
return dynamic_cast<const T*>(world.get());
}
}
return nullptr;
}
} // namespace Parallel
#endif // PARA_COLLECTION_H