WRApplication
External kratos "application" for multiscale time integration.
EntityProxy.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/node.h" // Node
9 #include "includes/element.h" // Element
10 #include "includes/condition.h" // Condition
11 #include "utilities/variable_utils.h" // VariableUtils::HasValue, VariableUtils::GetValue, VariableUtils::SetValue
12 
13 // --- WRApp Includes ---
14 #include "wrapp/utils/inc/OptionalRef.hpp" // OptionalRef
15 
16 // --- STL Includes ---
17 #include <type_traits> // remove_reference_t, is_const_v, is_same_v, decay_t
18 
19 
20 namespace Kratos::WRApp {
21 
22 
23 template <class TEntityProxy>
24 class ContainerProxy;
25 
26 
31 
32 
46 template <Globals::DataLocation TLocation, bool TMutable>
47 class KRATOS_API(WR_APPLICATION) EntityProxy
48 {
49 private:
50  constexpr static Globals::DataLocation Location = TLocation;
51 
52  constexpr static bool IsMutable = TMutable;
53 
55  using UnqualifiedEntity = std::conditional_t<
56  TLocation == Globals::DataLocation::NodeHistorical || TLocation == Globals::DataLocation::NodeNonHistorical,
57  Node,
58  std::conditional_t<
59  TLocation == Globals::DataLocation::Element,
60  Element,
61  std::conditional_t<
62  TLocation == Globals::DataLocation::Condition,
63  Condition,
64  std::conditional_t<
65  TLocation == Globals::DataLocation::ProcessInfo,
66  ProcessInfo,
67  std::conditional_t<
68  TLocation == Globals::DataLocation::ModelPart,
69  ModelPart,
70  void // <== invalid fallback type; will throw a compile-time error
71  >
72  >
73  >
74  >
75  >;
76 
78  using QualifiedEntity = std::conditional_t<TMutable,
79  UnqualifiedEntity,
80  const UnqualifiedEntity>;
81 
83  friend class ContainerProxy<EntityProxy>;
84 
85 public:
89  EntityProxy() noexcept = default;
90 
95  EntityProxy(QualifiedEntity& rEntity) noexcept : mrEntity(rEntity) {}
96 
98  template <class TValue>
99  bool HasValue(const Variable<TValue>& rVariable) const noexcept
100  {
101  return VariableUtils::HasValue<TLocation>(mrEntity.value(), rVariable);
102  }
103 
105  template <class TValue>
106  std::conditional_t<std::is_integral_v<TValue> || std::is_floating_point_v<TValue>,
107  TValue, // <== return by value if scalar type
108  const TValue&> // <== return by reference in non-scalar type
109  GetValue(const Variable<TValue>& rVariable) const
110  {
111  return VariableUtils::GetValue<TLocation>(mrEntity.value(), rVariable);
112  }
113 
115  template <class TValue, std::enable_if_t</*this is required for SFINAE*/!std::is_same_v<TValue,void> && TMutable,bool> = true>
116  TValue& GetValue(const Variable<TValue>& rVariable)
117  {
118  return VariableUtils::GetValue<TLocation>(mrEntity.value(), rVariable);
119  }
120 
122  template <class TValue, std::enable_if_t</*this is required for SFINAE*/!std::is_same_v<TValue,void> && TMutable,bool> = true>
123  void SetValue(const Variable<TValue>& rVariable,
124  std::conditional_t<std::is_integral_v<TValue> || std::is_floating_point_v<TValue>,
125  TValue, /*pass scalar types by value*/
126  const TValue&> /*pass non-scalar types by reference*/ Value)
127  {
128  VariableUtils::SetValue<TLocation>(mrEntity.value(), rVariable, Value);
129  }
130 
132  const UnqualifiedEntity& GetEntity() const
133  {
134  return mrEntity.value();
135  }
136 
138  QualifiedEntity& GetEntity()
139  {
140  return mrEntity.value();
141  }
142 
143 private:
145 }; // class EntityProxy
146 
147 
148 
150 template <Globals::DataLocation TLocation, class TEntity>
151 inline auto MakeProxy(const TEntity& rEntity)
152 {
153  static_assert(std::is_same_v<TEntity,void>, "Invalid DataLocation-Entity combination");
154 }
155 
156 
158 template <Globals::DataLocation TLocation, class TEntity>
159 inline auto MakeProxy(TEntity& rEntity)
160 {
161  static_assert(std::is_same_v<TEntity,void>, "Invalid DataLocation-Entity combination");
162 }
163 
164 
165 #define WRAPP_DEFINE_ENTITY_PROXY_FACTORY(TLocation, TEntity) \
166  \
167  template <> \
168  inline auto MakeProxy<TLocation,TEntity>(const TEntity& rEntity) \
169  {return EntityProxy<TLocation,false>(rEntity);} \
170  \
171  template <> \
172  inline auto MakeProxy<TLocation,TEntity>(TEntity& rEntity) \
173  {return EntityProxy<TLocation,true>(rEntity);}
174 
175 WRAPP_DEFINE_ENTITY_PROXY_FACTORY(Globals::DataLocation::NodeHistorical, Node)
176 
177 WRAPP_DEFINE_ENTITY_PROXY_FACTORY(Globals::DataLocation::NodeNonHistorical, Node)
178 
179 WRAPP_DEFINE_ENTITY_PROXY_FACTORY(Globals::DataLocation::Element, Element)
180 
181 WRAPP_DEFINE_ENTITY_PROXY_FACTORY(Globals::DataLocation::Condition, Condition)
182 
183 WRAPP_DEFINE_ENTITY_PROXY_FACTORY(Globals::DataLocation::ProcessInfo, ProcessInfo)
184 
185 WRAPP_DEFINE_ENTITY_PROXY_FACTORY(Globals::DataLocation::ModelPart, ModelPart)
186 
187 #undef WRAPP_DEFINE_ENTITY_PROXY_FACTORY
188 
189 
191 template <>
192 inline auto MakeProxy<Globals::DataLocation::ProcessInfo,ModelPart>(const ModelPart& rModelPart)
193 {
194  return EntityProxy<Globals::DataLocation::ProcessInfo,false>(rModelPart.GetProcessInfo());
195 }
196 
197 
199 template <>
200 inline auto MakeProxy<Globals::DataLocation::ProcessInfo,ModelPart>(ModelPart& rModelPart)
201 {
202  return EntityProxy<Globals::DataLocation::ProcessInfo,true>(rModelPart.GetProcessInfo());
203 }
204 
205 
208 
209 
210 } // namespace Kratos::WRApp
A view with a uniform interface for ModelPart::NodesContainerType, ModelPart::ElementsContainerType,...
Definition: ContainerProxy.hpp:28
Wrapper class providing a uniform interface for historical/non-historical Node, Element,...
Definition: EntityProxy.hpp:48
bool HasValue(const Variable< TValue > &rVariable) const noexcept
Check whether the entity has a value for the provided variable.
Definition: EntityProxy.hpp:99
std::conditional_t< std::is_integral_v< TValue >||std::is_floating_point_v< TValue >, TValue, const TValue & > GetValue(const Variable< TValue > &rVariable) const
Fetch the value corresponding to the input variable in the wrapped entity.
Definition: EntityProxy.hpp:109
QualifiedEntity & GetEntity()
Mutable or immutable access to the wrapped entity, depending on TMutable.
Definition: EntityProxy.hpp:138
void SetValue(const Variable< TValue > &rVariable, std::conditional_t< std::is_integral_v< TValue >||std::is_floating_point_v< TValue >, TValue, const TValue & > Value)
Overwrite the value corresponding to the input variable in the wrapped entity.
Definition: EntityProxy.hpp:123
EntityProxy() noexcept=default
Default constructor that leaves the instance in an invalid state.
const UnqualifiedEntity & GetEntity() const
Immutable access to the wrapped entity.
Definition: EntityProxy.hpp:132
TValue & GetValue(const Variable< TValue > &rVariable)
Fetch the value corresponding to the input variable in the wrapped entity.
Definition: EntityProxy.hpp:116
auto MakeProxy(const TEntity &rEntity)
Invalid template base to be specialized for valid template parameters.
Definition: EntityProxy.hpp:151
#define WRAPP_DEFINE_ENTITY_PROXY_FACTORY(TLocation, TEntity)
Definition: EntityProxy.hpp:165
Definition: MPIUtils.hpp:9