Wildmeshing Toolkit
filter_pointers_to_derived.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <algorithm>
3 #include <iterator>
4 #include <memory>
5 #include <vector>
6 
7 namespace wmtk::utils {
8 // returns the subset of edges that are drived form a particular type in the same order passed in
9 template <typename Base, typename Derived>
10 auto filter_pointers_to_derived(const std::vector<std::shared_ptr<Base>>& edges)
11  -> std::vector<std::shared_ptr<Derived>>
12 {
13  // dynamic_ptr_cast will convert any invalid conversion to a nullptr so this code
14  // * forces every edge to be the derived type, and invalid casts will be set to
15  // shared_ptr<Derived>{nullptr}
16  // * uses the erase-remove idiom to remove all nullptrs (i.e all elements that oculd not be
17  // cast)
18 
19 
20  std::vector<std::shared_ptr<Derived>> ret;
21 
22  std::transform(
23  edges.begin(),
24  edges.end(),
25  std::back_inserter(ret),
26  [](const std::shared_ptr<Base>& ptr) -> std::shared_ptr<Derived> {
27  return std::dynamic_pointer_cast<Derived>(ptr);
28  });
29 
30  ret.erase(std::remove(ret.begin(), ret.end(), std::shared_ptr<Derived>{}), ret.end());
31  return ret;
32 }
33 } // namespace wmtk::utils
std::vector< Tuple > edges(const Mesh &m, const Simplex &simplex)
auto filter_pointers_to_derived(const std::vector< std::shared_ptr< Base >> &edges) -> std::vector< std::shared_ptr< Derived >>