|
| 1 | +/* ---------------------------------------------------------------------- |
| 2 | + * Project: CMSIS Stream Library |
| 3 | + * Title: IdentifiedNode.hpp |
| 4 | + * Description: Helper template that exposes identified C++ nodes through C |
| 5 | + * |
| 6 | + * |
| 7 | + * Target Processor: Cortex-M and Cortex-A cores |
| 8 | + * -------------------------------------------------------------------- |
| 9 | + * |
| 10 | + * Copyright (C) 2026 ARM Limited or its affiliates. All rights reserved. |
| 11 | + * |
| 12 | + * SPDX-License-Identifier: Apache-2.0 |
| 13 | + * |
| 14 | + * Licensed under the Apache License, Version 2.0 (the License); you may |
| 15 | + * not use this file except in compliance with the License. |
| 16 | + * You may obtain a copy of the License at |
| 17 | + * |
| 18 | + * www.apache.org/licenses/LICENSE-2.0 |
| 19 | + * |
| 20 | + * Unless required by applicable law or agreed to in writing, software |
| 21 | + * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 22 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 | + * See the License for the specific language governing permissions and |
| 24 | + * limitations under the License. |
| 25 | + */ |
| 26 | +#pragma once |
| 27 | + |
| 28 | +extern "C" { |
| 29 | +#include "cstream_node.h" |
| 30 | +} |
| 31 | + |
| 32 | +#include "StreamNode.hpp" |
| 33 | + |
| 34 | +#include <type_traits> |
| 35 | + |
| 36 | +/** |
| 37 | + * @brief Create a CStreamNode handle for an identified C++ node. |
| 38 | + * |
| 39 | + * The helper detects supported C++ base classes at compile time and installs |
| 40 | + * the matching C interface tables in the returned CStreamNode. Interface |
| 41 | + * pointers remain NULL when the node type does not implement the corresponding |
| 42 | + * API. |
| 43 | + * |
| 44 | + * Extend this function with additional interface tables when the application |
| 45 | + * adds more C-callable APIs to CStreamNode. |
| 46 | + */ |
| 47 | +template <typename T> |
| 48 | +CStreamNode createStreamNode(T &obj) |
| 49 | +{ |
| 50 | + const StreamNodeInterface *stream_intf_current = nullptr; |
| 51 | + const ContextSwitchInterface *context_switch_intf_current = nullptr; |
| 52 | + |
| 53 | + if constexpr (std::is_base_of<arm_cmsis_stream::StreamNode, T>::value) |
| 54 | + { |
| 55 | + static const StreamNodeInterface stream_intf = { |
| 56 | + [](const void *self) -> int { |
| 57 | + return static_cast<const T *>(self)->nodeID(); |
| 58 | + }, |
| 59 | + [](const void *self) -> int { |
| 60 | + return static_cast<const T *>(self)->needsAsynchronousInit(); |
| 61 | + }, |
| 62 | + [](void *self, int outputPort, CStreamNode *dst, int dstPort) { |
| 63 | + static_cast<T *>(self)->subscribe( |
| 64 | + outputPort, |
| 65 | + *static_cast<arm_cmsis_stream::StreamNode *>(dst->obj), |
| 66 | + dstPort); |
| 67 | + }, |
| 68 | + [](void *self) -> cg_status { |
| 69 | + return static_cast<T *>(self)->init(); |
| 70 | + }, |
| 71 | + }; |
| 72 | + stream_intf_current = &stream_intf; |
| 73 | + } |
| 74 | + |
| 75 | + if constexpr (std::is_base_of<ContextSwitch, T>::value) |
| 76 | + { |
| 77 | + static const ContextSwitchInterface context_switch_intf = { |
| 78 | + [](void *self) -> int { |
| 79 | + return static_cast<T *>(self)->pause(); |
| 80 | + }, |
| 81 | + [](void *self) -> int { |
| 82 | + return static_cast<T *>(self)->resume(); |
| 83 | + }, |
| 84 | + }; |
| 85 | + context_switch_intf_current = &context_switch_intf; |
| 86 | + } |
| 87 | + |
| 88 | + return CStreamNode{&obj, stream_intf_current, context_switch_intf_current}; |
| 89 | +} |
0 commit comments