CloneUniquePtrs.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <memory>
7 #include <unordered_map>
8 #include <vector>
9 
10 /// \ingroup UtilitiesGroup
11 /// \brief Given a map of `std::unique_ptr` returns a copy of the map by
12 /// invoking `get_clone()` on each element of the input map.
13 template <typename KeyType, typename T>
15  const std::unordered_map<KeyType, std::unique_ptr<T>>& map) noexcept {
17  for (const auto& kv : map) {
18  result[kv.first] = kv.second->get_clone();
19  }
20  return result;
21 }
22 
23 /// \ingroup UtilitiesGroup
24 /// \brief Given a vector of `std::unique_ptr` returns a copy of the vector by
25 /// invoking `get_clone()` on each element of the input vector.
26 template <typename T>
28  const std::vector<std::unique_ptr<T>>& vector) noexcept {
29  std::vector<std::unique_ptr<T>> result{vector.size()};
30  for (size_t i = 0; i < vector.size(); ++i) {
31  result[i] = vector[i]->get_clone();
32  }
33  return result;
34 }
vector
memory
clone_unique_ptrs
std::unordered_map< KeyType, std::unique_ptr< T > > clone_unique_ptrs(const std::unordered_map< KeyType, std::unique_ptr< T >> &map) noexcept
Given a map of std::unique_ptr returns a copy of the map by invoking get_clone() on each element of t...
Definition: CloneUniquePtrs.hpp:14
std::unique_ptr
unordered_map