ArrayComponentId.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <boost/functional/hash.hpp>
7 #include <ckarrayindex.h>
8 #include <cstddef>
9 #include <functional>
10 #include <string>
11 
12 #include "Utilities/PrettyType.hpp"
13 
14 namespace PUP {
15 class er;
16 } // namespace PUP
17 
18 namespace observers {
19 /*!
20  * \ingroup ObserversGroup
21  * \brief An ID type that identifies both the parallel component and the index
22  * in the parallel component.
23  *
24  * A specialization of `std::hash` is provided to allow using `ArrayComponentId`
25  * as a key in associative containers.
26  */
28  public:
29  ArrayComponentId() = default; // Needed for Charm++ serialization
30 
31  template <typename ParallelComponent>
32  ArrayComponentId(const ParallelComponent* /*meta*/,
33  const CkArrayIndex& index) noexcept;
34 
35  // NOLINTNEXTLINE(google-runtime-references)
36  void pup(PUP::er& p) noexcept;
37 
38  size_t component_id() const noexcept { return component_id_; }
39 
40  const CkArrayIndex& array_index() const noexcept { return array_index_; }
41 
42  private:
43  size_t component_id_{0};
44  CkArrayIndex array_index_{};
45 };
46 
47 template <typename ParallelComponent>
48 ArrayComponentId::ArrayComponentId(const ParallelComponent* const /*meta*/,
49  const CkArrayIndex& index) noexcept
50  : component_id_(
51  std::hash<std::string>{}(pretty_type::get_name<ParallelComponent>())),
52  array_index_(index) {}
53 
54 bool operator==(const ArrayComponentId& lhs,
55  const ArrayComponentId& rhs) noexcept;
56 
57 bool operator!=(const ArrayComponentId& lhs,
58  const ArrayComponentId& rhs) noexcept;
59 } // namespace observers
60 
61 namespace std {
62 template <>
63 struct hash<observers::ArrayComponentId> {
64  size_t operator()(const observers::ArrayComponentId& t) const {
65  size_t result = t.component_id();
66  boost::hash_combine(result, t.array_index().hash());
67  return result;
68  }
69 };
70 } // namespace std
Definition: Actions.hpp:20
Definition: Strahlkorper.hpp:14
An ID type that identifies both the parallel component and the index in the parallel component...
Definition: ArrayComponentId.hpp:27
Contains a pretty_type library to write types in a "pretty" format.