Lagrange
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
PyAttribute.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 <lagrange/Attribute.h>
15#include <lagrange/AttributeTypes.h>
16#include <lagrange/internal/weak_ptr.h>
17#include <lagrange/utils/Error.h>
18
19#include <memory>
20
21namespace lagrange::python {
22
24{
25public:
27 : m_attr(ptr)
28 {}
29
30public:
31 internal::shared_ptr<AttributeBase> operator->() { return ptr(); }
32
34 {
35 auto attr = m_attr.lock();
36 if (attr == nullptr) throw Error("Attribute is no longer valid!");
37 return attr;
38 }
39
40 template <typename ValueType>
42 {
43 auto attr = m_attr.lock();
44 if (attr == nullptr) throw Error("Attribute is no longer valid!");
46 attr,
47 dynamic_cast<Attribute<ValueType>*>(attr.get()));
48 }
49
50 template <typename CallBack>
51 auto process(CallBack&& cb)
52 {
53#define LA_X_process(_, ValueType) \
54 { \
55 auto attr = ptr<ValueType>(); \
56 if (attr != nullptr) { \
57 return cb(*attr); \
58 } \
59 }
60 LA_ATTRIBUTE_X(process, 0)
61#undef LA_X_process
62 throw Error("Cannot process attribute with unsupported type!");
63 }
64
65private:
67};
68
69} // namespace lagrange::python
NOT implemented: custom allocator support.
Definition: shared_ptr.h:110
Definition: weak_ptr.h:26
Definition: PyAttribute.h:24
#define LA_ATTRIBUTE_X(mode, data)
X Macro arguments for the Attribute<> class.
Definition: AttributeTypes.h:48
Base exception for errors thrown by Lagrange functions.
Definition: Error.h:27