WRApplication
External kratos "application" for multiscale time integration.
ContainerProxy.hpp
Go to the documentation of this file.
1 
3 #pragma once
4 
5 // --- Core Includes ---
6 #include "includes/global_variables.h" // DataLocation
7 #include "includes/kratos_export_api.h" // KRATOS_API
8 #include "includes/model_part.h" // ModelPart::NodesContainerType, ModelPart::ElementsContainerType, ModelPart::ConditionsContainerType
9 #include "utilities/model_part_utils.h" // ModelPartUtils::GetContainer
10 
11 // --- WRApp Includes ---
12 #include "wrapp/utils/inc/EntityProxy.hpp" // EntityProxy
13 
14 
15 namespace Kratos::WRApp {
16 
17 
26 template <class TEntityProxy>
27 class KRATOS_API(WR_APPLICATION) ContainerProxy
28 {
29 private:
30  using UnqualifiedContainer = std::conditional_t<
31  std::is_same_v<typename TEntityProxy::UnqualifiedEntity,Node>,
32  ModelPart::NodesContainerType,
33  std::conditional_t<
34  std::is_same_v<typename TEntityProxy::UnqualifiedEntity,Element>,
35  ModelPart::ElementsContainerType,
36  std::conditional_t<
37  std::is_same_v<typename TEntityProxy::UnqualifiedEntity,Condition>,
38  ModelPart::ConditionsContainerType,
39  void // <== invalid fallback type; will throw a compile-time error
40  >
41  >
42  >;
43 
44  constexpr static bool IsMutable = TEntityProxy::IsMutable;
45 
46  using WrappedIterator = std::conditional_t<IsMutable,
47  typename UnqualifiedContainer::iterator,
48  typename UnqualifiedContainer::const_iterator>;
49 
50  template <bool TMutable>
51  class Iterator
52  {
53  private:
54  using Wrapped = std::conditional_t<TMutable,
55  typename UnqualifiedContainer::iterator,
56  typename UnqualifiedContainer::const_iterator>;
57 
58  public:
60 
61  using pointer = std::conditional_t<TMutable,
62  value_type*,
63  const value_type*>;
64 
65  using reference = std::conditional_t<TMutable,
66  value_type&,
67  const value_type&>;
68 
69  using difference_type = std::ptrdiff_t;
70 
71  using iterator_category = std::random_access_iterator_tag;
72 
73  Iterator() noexcept = default;
74 
75  Iterator(Wrapped It) noexcept : mWrapped(It) {}
76 
77  value_type operator*() const noexcept {return value_type(*mWrapped);}
78 
79  Iterator& operator++() noexcept {++mWrapped; return *this;}
80 
81  Iterator operator++(int) noexcept {Iterator copy(mWrapped); ++mWrapped; return copy;}
82 
83  Iterator& operator--() noexcept {--mWrapped; return *this;}
84 
85  Iterator operator--(int) noexcept {Iterator copy(mWrapped); --mWrapped; return copy;}
86 
87  Iterator& operator+=(difference_type Rhs) noexcept {mWrapped += Rhs; return *this;}
88 
89  Iterator& operator-=(difference_type Rhs) noexcept {mWrapped -= Rhs; return *this;}
90 
91  Iterator operator+(difference_type Rhs) const noexcept {Iterator copy(mWrapped); copy += Rhs; return copy;}
92 
93  Iterator operator-(difference_type Rhs) const noexcept {Iterator copy(mWrapped); copy -= Rhs; return copy;}
94 
95  difference_type operator-(Iterator Rhs) const noexcept {return mWrapped - Rhs.mWrapped;}
96 
97  bool operator==(Iterator Rhs) const noexcept {return mWrapped == Rhs.mWrapped;}
98 
99  bool operator!=(Iterator Rhs) const noexcept {return mWrapped != Rhs.mWrapped;}
100 
101  bool operator<(Iterator Rhs) const noexcept {return mWrapped < Rhs.mWrapped;}
102 
103  bool operator>(Iterator Rhs) const noexcept {return mWrapped > Rhs.mWrapped;}
104 
105  bool operator<=(Iterator Rhs) const noexcept {return mWrapped <= Rhs.mWrapped;}
106 
107  bool operator>=(Iterator Rhs) const noexcept {return mWrapped >= Rhs.mWrapped;}
108 
109  private:
110  Wrapped mWrapped;
111  }; // class Iterator
112 public:
113  using iterator = Iterator<IsMutable>;
114 
115  using const_iterator = Iterator<false>;
116 
117  using size_type = std::size_t;
118 
120 
121  ContainerProxy() noexcept = default;
122 
123  ContainerProxy(WrappedIterator Begin, WrappedIterator End) noexcept
124  : mBegin(Begin),
125  mEnd(End)
126  {}
127 
128  typename const_iterator::value_type operator[](size_type Index) const noexcept {return typename const_iterator::value_type(*(mBegin + Index));}
129 
130  typename iterator::value_type operator[](size_type Index) noexcept {return typename iterator::value_type(*(mBegin + Index));}
131 
132  typename const_iterator::value_type at(size_type Index) const noexcept {return typename const_iterator::value_type(*(mBegin + Index));}
133 
134  typename iterator::value_type at(size_type Index) noexcept {return typename iterator::value_type(*(mBegin + Index));}
135 
136  size_type size() const noexcept {return std::distance(mBegin, mEnd);}
137 
138  bool empty() const noexcept {return this->size() == 0;}
139 
140  const_iterator cbegin() const noexcept {return const_iterator(mBegin);}
141 
142  const_iterator begin() const noexcept {return this->cbegin();}
143 
144  iterator begin() noexcept {return iterator(mBegin);}
145 
146  const_iterator cend() const noexcept {return const_iterator(mEnd);}
147 
148  const_iterator end() const noexcept {return this->cend();}
149 
150  iterator end() noexcept {return iterator(mEnd);}
151 
152 private:
153  WrappedIterator mBegin, mEnd;
154 }; // class ContainerProxy
155 
156 
157 
158 #define WRAPP_DEFINE_CONTAINER_PROXY_FACTORY(TLocation) \
159  \
160  template <> \
161  inline auto MakeProxy<TLocation,ModelPart>(const ModelPart& rModelPart) \
162  { \
163  const auto& r_container = ModelPartUtils::GetContainer<TLocation>(rModelPart); \
164  return ContainerProxy<EntityProxy<TLocation,false>>(r_container.begin(), \
165  r_container.end()); \
166  } \
167  \
168  template <> \
169  inline auto MakeProxy<TLocation,ModelPart>(ModelPart& rModelPart) \
170  { \
171  auto& r_container = ModelPartUtils::GetContainer<TLocation>(rModelPart); \
172  return ContainerProxy<EntityProxy<TLocation,true>>(r_container.begin(), \
173  r_container.end()); \
174  }
175 
176 WRAPP_DEFINE_CONTAINER_PROXY_FACTORY(Globals::DataLocation::NodeHistorical)
177 
178 WRAPP_DEFINE_CONTAINER_PROXY_FACTORY(Globals::DataLocation::NodeNonHistorical)
179 
180 WRAPP_DEFINE_CONTAINER_PROXY_FACTORY(Globals::DataLocation::Element)
181 
182 WRAPP_DEFINE_CONTAINER_PROXY_FACTORY(Globals::DataLocation::Condition)
183 
184 #undef WRAPP_DEFINE_CONTAINER_PROXY_FACTORY
185 
186 
187 } // namespace Kratos::WRApp
#define WRAPP_DEFINE_CONTAINER_PROXY_FACTORY(TLocation)
Definition: ContainerProxy.hpp:158
A view with a uniform interface for ModelPart::NodesContainerType, ModelPart::ElementsContainerType,...
Definition: ContainerProxy.hpp:28
size_type size() const noexcept
Definition: ContainerProxy.hpp:136
const_iterator::value_type at(size_type Index) const noexcept
Definition: ContainerProxy.hpp:132
const_iterator begin() const noexcept
Definition: ContainerProxy.hpp:142
bool empty() const noexcept
Definition: ContainerProxy.hpp:138
Iterator< IsMutable > iterator
Definition: ContainerProxy.hpp:113
iterator::value_type operator[](size_type Index) noexcept
Definition: ContainerProxy.hpp:130
const_iterator end() const noexcept
Definition: ContainerProxy.hpp:148
iterator begin() noexcept
Definition: ContainerProxy.hpp:144
const_iterator::value_type operator[](size_type Index) const noexcept
Definition: ContainerProxy.hpp:128
const_iterator cend() const noexcept
Definition: ContainerProxy.hpp:146
Iterator< false > const_iterator
Definition: ContainerProxy.hpp:115
iterator::value_type at(size_type Index) noexcept
Definition: ContainerProxy.hpp:134
typename iterator::value_type value_type
Definition: ContainerProxy.hpp:119
const_iterator cbegin() const noexcept
Definition: ContainerProxy.hpp:140
std::size_t size_type
Definition: ContainerProxy.hpp:117
ContainerProxy() noexcept=default
iterator end() noexcept
Definition: ContainerProxy.hpp:150
Wrapper class providing a uniform interface for historical/non-historical Node, Element,...
Definition: EntityProxy.hpp:48
Definition: MPIUtils.hpp:9