1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "taihe/callback.hpp"
16 #include <iostream>
17 #include "callbackTest.impl.hpp"
18 #include "taihe/string.hpp"
19
20 using namespace taihe;
21
22 namespace {
23
24 class MyInterfaceImpl {
25 public:
26 static const int ten = 10;
27 static const int hundred = 100;
28 static const long long tenBillion = 1000000000LL;
MyInterfaceImpl()29 MyInterfaceImpl() {}
30
TestCbIntString(::taihe::callback_view<void (int8_t,int32_t)> f)31 ::taihe::string TestCbIntString(::taihe::callback_view<void(int8_t, int32_t)> f)
32 {
33 f(ten, hundred);
34 return "testCbIntString";
35 }
36
TestCbIntBool(::taihe::callback_view<void (int16_t,int64_t)> f)37 bool TestCbIntBool(::taihe::callback_view<void(int16_t, int64_t)> f)
38 {
39 f(hundred, tenBillion);
40 return true;
41 }
42
TestCbEnum(::taihe::callback_view<void (int32_t)> f)43 ::callbackTest::EnumData TestCbEnum(::taihe::callback_view<void(int32_t)> f)
44 {
45 f(ten);
46 return ::callbackTest::EnumData(::callbackTest::EnumData::key_t::F32_A);
47 }
48 };
49
TestCbV(callback_view<void ()> f)50 void TestCbV(callback_view<void()> f)
51 {
52 f();
53 }
54
TestCbI(callback_view<void (int32_t)> f)55 void TestCbI(callback_view<void(int32_t)> f)
56 {
57 static const int one = 1;
58 f(one);
59 }
60
TestCbS(callback_view<void (string_view,bool)> f)61 void TestCbS(callback_view<void(string_view, bool)> f)
62 {
63 f("hello", true);
64 }
65
TestCbRs(callback_view<string (string_view)> f)66 string TestCbRs(callback_view<string(string_view)> f)
67 {
68 taihe::string out = f("hello");
69 return out;
70 }
71
TestCbStruct(callback_view<::callbackTest::Data (::callbackTest::Data const &)> f)72 void TestCbStruct(callback_view<::callbackTest::Data(::callbackTest::Data const &)> f)
73 {
74 ::callbackTest::Data result = f(::callbackTest::Data {"a", "b", 1});
75 return;
76 }
77
GetInterface()78 ::callbackTest::MyInterface GetInterface()
79 {
80 return taihe::make_holder<MyInterfaceImpl, ::callbackTest::MyInterface>();
81 }
82 } // namespace
83
84 TH_EXPORT_CPP_API_TestCbV(TestCbV);
85 TH_EXPORT_CPP_API_TestCbI(TestCbI);
86 TH_EXPORT_CPP_API_TestCbS(TestCbS);
87 TH_EXPORT_CPP_API_TestCbRs(TestCbRs);
88 TH_EXPORT_CPP_API_TestCbStruct(TestCbStruct);
89 TH_EXPORT_CPP_API_GetInterface(GetInterface);
90 // NOLINTEND