IdPair.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <ostream>
7 #include <pup.h>
8 #include <type_traits>
9 #include <utility>
10 
11 /*!
12  * \ingroup DataStructuresGroup
13  * \brief A data structure that contains an ID and data associated with that ID.
14  */
15 template <typename IdType, typename DataType>
16 struct IdPair {
17  IdType id{};
18  DataType data{};
19 };
20 
21 template <typename IdType, typename DataType>
23  IdType&& id, DataType&& data) noexcept {
24  return {std::forward<IdType>(id), std::forward<DataType>(data)};
25 }
26 
27 /// \cond
28 // We write the pup function as a free function to keep IdPair a POD
29 // clang-tidy: no non-const references
30 template <typename IdType, typename DataType>
31 void pup(PUP::er& p, IdPair<IdType, DataType>& t) noexcept { // NOLINT
32  p | t.id;
33  p | t.data;
34 }
35 
36 // clang-tidy: no non-const references
37 template <typename IdType, typename DataType>
38 void operator|(PUP::er& p, IdPair<IdType, DataType>& t) noexcept { // NOLINT
39  pup(p, t);
40 }
41 
42 template <typename IdType, typename DataType>
43 bool operator==(const IdPair<IdType, DataType>& lhs,
44  const IdPair<IdType, DataType>& rhs) noexcept {
45  return lhs.id == rhs.id and lhs.data == rhs.data;
46 }
47 
48 template <typename IdType, typename DataType>
49 bool operator!=(const IdPair<IdType, DataType>& lhs,
50  const IdPair<IdType, DataType>& rhs) noexcept {
51  return not(lhs == rhs);
52 }
53 
54 template <typename IdType, typename DataType>
55 std::ostream& operator<<(std::ostream& os,
56  const IdPair<IdType, DataType>& t) noexcept {
57  return os << '(' << t.id << ',' << t.data << ')';
58 }
59 /// \endcond
A data structure that contains an ID and data associated with that ID.
Definition: IdPair.hpp:16