Wildmeshing Toolkit
MeshAttributeHandle.hpp
Go to the documentation of this file.
1 #pragma once
2 #if defined(MTAO_DEBUG_MESH_COMP)
3 #include <spdlog/spdlog.h>
4 #endif
5 //
7 //
8 #include "AttributeType.hpp"
10 
11 #include <tuple>
12 #include <variant>
13 
14 namespace wmtk {
15 class Mesh;
16 } // namespace wmtk
17 
18 namespace wmtk::attribute {
19 
20 /* @brief Handle that can construct an accessor on its own
21  * NOTE: This naming is inconsistent with the existing
22  * AttributeHandle/MeshAttributeHandle nomenclature, but in the future most
23  * applications should store MeshAttributeHandles instead of
24  * MeshAttributeHandle, and after most of those changes are made we will
25  * deprecate that name.
26  */
28 {
29 public:
30  // The variant type for storing all of the tuple types.
31  // each type within the Handle variant must have a ::Type value that indicates a type-based
32  // descriptor of the data held by the type
33 
34  using HandleVariant = std::variant<
39 
40  // Convenience class for identifying attribute types
41  using ValueVariant = std::
42  variant<char, int64_t, double, wmtk::Rational, std::tuple<char, wmtk::Rational, double>>;
43 
45 
46  template <HeldType Type>
47  using held_handle_type = std::variant_alternative_t<size_t(Type), HandleVariant>;
48 
49  template <HeldType Type>
51 
52 
53  // for primitive type attributes returns the held type for primitive types
54  // takes as input basic type (char/int64/double/rational)
55  // Note this doesn't work with the hybrid type, so this only makes esnse to use in basic
56  // attribute storage
57  template <typename T>
58  constexpr static HeldType held_type_from_primitive();
59 
60  template <typename T>
61  constexpr static HeldType held_type_from_handle();
62 
63 
64  template <typename T>
65  constexpr static bool handle_type_is_basic();
66 
67 
68  friend class wmtk::Mesh;
69  friend class wmtk::hash<MeshAttributeHandle>;
70  MeshAttributeHandle() = default;
76 
77  bool operator==(const MeshAttributeHandle& o) const
78  {
79 #if defined(MTAO_DEBUG_MESH_COMP)
80  std::visit(
81  [&](const auto& h, const auto& oh) {
82  spdlog::warn(
83  "{} {} == {} {}",
84  std::string(h),
85  fmt::ptr(m_mesh),
86  std::string(oh),
87  fmt::ptr(m_mesh));
88  },
89  m_handle,
90  o.m_handle);
91 #endif
92  return m_handle == o.m_handle && m_mesh == o.m_mesh;
93  }
94 
95  // reutrns if the target mesh is the same as the one represented in the handle
96  bool is_same_mesh(const Mesh&) const;
97 
98 
99  // returns if this handle was initialized
100  bool is_valid() const;
101 
103  template <typename T>
105  // AttributeHandle base_handle() const ;
106 
107 
108  template <typename T>
109  auto as() const -> const held_handle_type<held_type_from_primitive<T>()>&;
110 
111  template <HeldType Type>
112  auto as_from_held_type() const -> const held_handle_type<Type>&;
113  // returns if the held attribute uses the primitive T
114 
115 
116  // returns if the held attribute uses the primitive T
117  template <typename T>
118  bool holds() const;
119 
120  // holds basic type
121  bool holds_basic_type() const;
122 
123  // returns if the held attribute uses the held type primitive Type
124  template <HeldType Type>
125  bool holds_from_held_type() const;
126 
127  HeldType held_type() const;
128 
129  Mesh& mesh();
130  const Mesh& mesh() const;
131 
133  const HandleVariant& handle() const { return m_handle; }
139  // MutableAccessor<T> create_accessor();
140 
146  // ConstAccessor<T> create_const_accessor() const;
147  // ConstAccessor<T> create_accessor() const;
148 
149  // return the dimension of the attribute (i.e the number of values stored per simplex)
150  int64_t dimension() const;
151 
152  std::string name() const;
153 
154 
155 private:
156  Mesh* m_mesh = nullptr;
158 };
159 
160 template <typename T>
161 inline auto MeshAttributeHandle::as() const
162  -> const held_handle_type<held_type_from_primitive<T>()>&
163 {
164  return as_from_held_type<held_type_from_primitive<T>()>();
165 }
166 
167 
168 template <typename T>
169 inline bool MeshAttributeHandle::holds() const
170 {
171  return holds_from_held_type<held_type_from_primitive<T>()>();
172 }
173 
174 namespace internal {
175 template <typename T>
177 {
178  constexpr static bool value = false;
179 };
180 template <typename T>
182 {
183  constexpr static bool value = true;
184 };
185 } // namespace internal
186 
187 template <typename T>
189 {
191 }
192 
194 {
195  return std::visit(
196  [](const auto& h) noexcept -> bool {
197  return handle_type_is_basic<std::decay_t<decltype(h)>>();
198  },
199  m_handle);
200 }
201 template <MeshAttributeHandle::HeldType Type>
202 inline auto MeshAttributeHandle::as_from_held_type() const -> const held_handle_type<Type>&
203 {
204  return std::get<held_handle_type<Type>>(m_handle);
205 }
206 
207 template <MeshAttributeHandle::HeldType Type>
209 {
210  return std::holds_alternative<held_handle_type<Type>>(m_handle);
211 }
212 
213 template <typename T>
215 {
216  return held_type_from_handle<TypedAttributeHandle<T>>();
217 }
218 template <typename T>
220 {
221  return attribute_type_enum_from_type<typename T::Type>();
222 }
223 
225 {
226  return std::visit([](const auto& h) { return h.primitive_type(); }, m_handle);
227 }
228 template <typename T>
230 {
231  return std::get<T>(m_handle).primitive_type();
232 }
233 } // namespace wmtk::attribute
typename held_handle_type< Type >::Type held_primitive_type
const HandleVariant & handle() const
std::variant< TypedAttributeHandle< char >, TypedAttributeHandle< int64_t >, TypedAttributeHandle< double >, TypedAttributeHandle< wmtk::Rational > > HandleVariant
constexpr static HeldType held_type_from_handle()
MeshAttributeHandle(MeshAttributeHandle &&o)=default
std::variant_alternative_t< size_t(Type), HandleVariant > held_handle_type
constexpr static bool handle_type_is_basic()
auto as() const -> const held_handle_type< held_type_from_primitive< T >()> &
constexpr static HeldType held_type_from_primitive()
std::variant< char, int64_t, double, wmtk::Rational, std::tuple< char, wmtk::Rational, double > > ValueVariant
auto as_from_held_type() const -> const held_handle_type< Type > &
MeshAttributeHandle(const MeshAttributeHandle &o)=default
MeshAttributeHandle & operator=(const MeshAttributeHandle &o)=default
bool operator==(const MeshAttributeHandle &o) const
MeshAttributeHandle & operator=(MeshAttributeHandle &&o)=default
Definition: Accessor.hpp:6