OpenLexocad  28.0
Algorithms.h
Go to the documentation of this file.
1 #pragma once
2 #include <algorithm>
3 #include <numeric>
4 #include <Base/Type.h>
5 
6 
7 namespace Base
8 {
9 template <class InputIt, class OutputIt, class Pred, class Fct>
10 void transform_if(InputIt first, InputIt last, OutputIt dest, Pred pred, Fct transform)
11 {
12  while (first != last)
13  {
14  if (pred(*first))
15  *dest++ = transform(*first);
16  else
17  *dest++ = *first;
18 
19  ++first;
20  }
21 }
22 
23 template <class Cont, class OutputIt, class Pred, class Fct>
24 void transform_if(Cont container, OutputIt dest, Pred pred, Fct transform)
25 {
26  transform_if(container.cbegin(), container.cend(), dest, pred, transform);
27 }
28 
29 template <typename Container, typename OutputIt, typename BinaryFunction>
30 void transform(Container container, OutputIt out, BinaryFunction function)
31 {
32  std::transform(container.cbegin(), container.cend(), out, function);
33 }
34 
35 template <typename Container, typename T, typename BinaryFunction>
36 T accumulate(Container container, T init, BinaryFunction function)
37 {
38  return std::accumulate(container.cbegin(), container.cend(), init, function);
39 }
40 
41 
42 // usage: when we need const std::vector<App::Element*>&, but we have std::vector<App::Wall*> walls, we can use Base::castToBaseContainer<App::Element>(walls);
43 template <typename To, template <typename...> class Cont, typename FromPtr>
44 const Cont<To*>& castToBaseContainer(const Cont<FromPtr>& v)
45 {
46  static_assert(std::is_pointer<FromPtr>(), "FromPtr is not a pointer");
47  static_assert(std::is_base_of<To, std::remove_pointer_t<FromPtr>>(), "From is not derived from To");
48 
49  return *reinterpret_cast<const Cont<To*>*>(&v);
50 }
51 
52 template <typename To, template <typename...> class Cont, typename FromPtr>
53 Cont<To*>& castToBaseContainer(Cont<FromPtr>& v)
54 {
55  static_assert(std::is_pointer<FromPtr>(), "FromPtr is not a pointer");
56  static_assert(std::is_base_of<To, std::remove_pointer_t<FromPtr>>(), "From is not derived from To");
57 
58  return *reinterpret_cast<Cont<To*>*>(&v);
59 }
60 
61 
62 template <typename Val, typename Cont>
63 bool allSubtype(const Cont& cont)
64 {
65  return all_of(begin(cont), end(cont), [](auto val) { return dynamic_cast<Val>(val) != nullptr; });
66 }
67 
81 template <typename BaseClass, template <typename...> class Cont, typename... Derivates>
82 bool isAnyDerivedFrom(const Cont<BaseClass*>& container)
83 {
84  return std::any_of(begin(container),end(container), [](const auto v) { return ((dynamic_cast<Derivates*>(v) != nullptr) || ...); });
85 }
86 
87 }
Base::accumulate
T accumulate(Container container, T init, BinaryFunction function)
Definition: Algorithms.h:36
Base::transform
void transform(Container container, OutputIt out, BinaryFunction function)
Definition: Algorithms.h:30
Base::castToBaseContainer
const Cont< To * > & castToBaseContainer(const Cont< FromPtr > &v)
Definition: Algorithms.h:44
Type.h
Base::transform_if
void transform_if(InputIt first, InputIt last, OutputIt dest, Pred pred, Fct transform)
Definition: Algorithms.h:10
Base::isAnyDerivedFrom
bool isAnyDerivedFrom(const Cont< BaseClass * > &container)
Tests if at least one element of the container is a subclass (dynamic castable) of at least one of th...
Definition: Algorithms.h:82
Base::allSubtype
bool allSubtype(const Cont &cont)
Definition: Algorithms.h:63
Base
Definition: AbstractXMLReader.h:5