Lagrange
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
PyIndexedAttribute.h
1/*
2 * Copyright 2022 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include "PyAttribute.h"
15
16#include <lagrange/Attribute.h>
17#include <lagrange/IndexedAttribute.h>
18#include <lagrange/utils/Error.h>
19#include <lagrange/internal/weak_ptr.h>
20
21#include <memory>
22
23namespace lagrange::python {
24
26{
27public:
29 : m_attr(ptr)
30 {}
31
32public:
33 internal::shared_ptr<AttributeBase> operator->() { return ptr(); }
34
36 {
37 auto attr = m_attr.lock();
38 if (attr == nullptr) throw Error("Indexed attribute is no longer valid!");
39 return attr;
40 }
41
42 template <typename CallBack>
43 auto process(CallBack&& cb)
44 {
45 auto attr = m_attr.lock();
46 if (attr == nullptr) throw Error("Indexed attribute is no longer valid!");
47 if (attr->get_element_type() != Indexed)
48 throw Error("Attribute is not an indexed attribute");
49
50#define LA_X_process(Index, ValueType) \
51 { \
52 auto indexed_attr = dynamic_cast<IndexedAttribute<ValueType, Index>*>(attr.get()); \
53 if (indexed_attr != nullptr) { \
54 return cb(*indexed_attr); \
55 } \
56 }
57#define LA_X_process_index(_, Index) LA_ATTRIBUTE_X(process, Index)
58 LA_SURFACE_MESH_INDEX_X(process_index, 0)
59#undef LA_X_process_index
60#undef LA_X_process
61 throw Error("Cannot process indexed attribute with unsupported types!");
62 }
63
64 PyAttribute values()
65 {
66 return process([&](auto& indexed_attr) {
67 auto attr = m_attr.lock();
68 la_debug_assert(attr != nullptr);
69 auto& value_attr = indexed_attr.values();
70 internal::shared_ptr<AttributeBase> alias_ptr(attr, &value_attr);
71 return PyAttribute(alias_ptr);
72 });
73 }
74
75 PyAttribute indices()
76 {
77 return process([&](auto& indexed_attr) {
78 auto attr = m_attr.lock();
79 la_debug_assert(attr != nullptr);
80 auto& index_attr = indexed_attr.indices();
81 internal::shared_ptr<AttributeBase> alias_ptr(attr, &index_attr);
82 return PyAttribute(alias_ptr);
83 });
84 }
85
86private:
88};
89
90} // namespace lagrange::python
NOT implemented: custom allocator support.
Definition: shared_ptr.h:110
Definition: weak_ptr.h:26
Definition: PyAttribute.h:24
Definition: PyIndexedAttribute.h:26
@ Indexed
Indexed mesh attributes.
Definition: AttributeFwd.h:45
#define LA_SURFACE_MESH_INDEX_X(mode, data)
X Macro arguments to iterate over index types available for the SurfaceMesh<> class.
Definition: SurfaceMeshTypes.h:71
#define la_debug_assert(...)
Debug assertion check.
Definition: assert.h:189
Base exception for errors thrown by Lagrange functions.
Definition: Error.h:27