• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "opt.impl.hpp"
16 #include <iostream>
17 #include "opt.ReturnResult.proj.1.hpp"
18 #include "taihe/array.hpp"
19 #include "taihe/optional.hpp"
20 #include "taihe/string.hpp"
21 
22 using namespace taihe;
23 
24 namespace {
25 
26 class TestImpl {
27 public:
28     string str = "this is str";
29     optional<string> a_;
TestImpl()30     TestImpl() {}
31 
Setstring(::taihe::optional_view<::taihe::string> a)32     void Setstring(::taihe::optional_view<::taihe::string> a)
33     {
34         this->a_ = a;
35     }
36 
Getstring()37     ::taihe::optional<::taihe::string> Getstring()
38     {
39         return a_;
40     }
41 
SetIntData(::taihe::string_view a)42     void SetIntData(::taihe::string_view a)
43     {
44         this->str = a;
45     }
46 
ShowOptionalString(::taihe::optional_view<::taihe::string> a)47     ::taihe::optional<::taihe::string> ShowOptionalString(::taihe::optional_view<::taihe::string> a)
48     {
49         if (a) {
50             return a;
51         } else {
52             return optional<string>(nullptr);
53         }
54     }
55 
ShowOptionalInt32(::taihe::optional_view<int32_t> a)56     ::taihe::optional<int32_t> ShowOptionalInt32(::taihe::optional_view<int32_t> a)
57     {
58         if (a) {
59             return a;
60         } else {
61             return optional<int32_t>(nullptr);
62         }
63     }
64 
ShowOptionalBool(::taihe::optional_view<bool> a)65     ::taihe::optional<bool> ShowOptionalBool(::taihe::optional_view<bool> a)
66     {
67         if (a) {
68             return a;
69         } else {
70             return optional<bool>(nullptr);
71         }
72     }
73 
ShowOptionalRecord(::taihe::optional_view<::taihe::map<::taihe::string,bool>> a)74     ::taihe::optional<::taihe::map<::taihe::string, bool>> ShowOptionalRecord(
75         ::taihe::optional_view<::taihe::map<::taihe::string, bool>> a)
76     {
77         if (a) {
78             return a;
79         } else {
80             return optional<map<string, bool>>(nullptr);
81         }
82     }
83 
ShowOptionalStruct(::taihe::optional_view<::opt::MyStruct> a)84     ::taihe::optional<::opt::MyStruct> ShowOptionalStruct(::taihe::optional_view<::opt::MyStruct> a)
85     {
86         if (a) {
87             return a;
88         } else {
89             return optional<::opt::MyStruct>(nullptr);
90         }
91     }
92 };
ShowOptionalInt(optional_view<int32_t> x)93 void ShowOptionalInt(optional_view<int32_t> x)
94 {
95     if (x) {
96         std::cout << *x << std::endl;
97     } else {
98         std::cout << "Null" << std::endl;
99     }
100 }
101 
MakeOptionalInt(bool b)102 optional<int32_t> MakeOptionalInt(bool b)
103 {
104     if (b) {
105         int const optionalMakeValue = 10;
106         return optional<int32_t>::make(optionalMakeValue);
107     } else {
108         return optional<int32_t>(nullptr);
109     }
110 }
111 
MakeOptionalArray(bool b,int32_t val,int32_t num)112 optional<array<int32_t>> MakeOptionalArray(bool b, int32_t val, int32_t num)
113 {
114     if (b) {
115         return optional<array<int32_t>>::make(array<int32_t>::make(num, val));
116     } else {
117         return optional<array<int32_t>>(nullptr);
118     }
119 }
120 
SendReturnResult(::opt::ReturnResult const & result)121 optional<string> SendReturnResult(::opt::ReturnResult const &result)
122 {
123     if (result.results) {
124         string ret = "";
125         for (auto str : *result.results) {
126             ret = ret + str;
127         }
128         return optional<string>::make(ret);
129     } else {
130         return optional<string>(nullptr);
131     }
132 }
133 
GetTest()134 ::opt::Test GetTest()
135 {
136     return taihe::make_holder<TestImpl, ::opt::Test>();
137 }
138 
139 }  // namespace
140 
141 TH_EXPORT_CPP_API_ShowOptionalInt(ShowOptionalInt);
142 TH_EXPORT_CPP_API_MakeOptionalInt(MakeOptionalInt);
143 TH_EXPORT_CPP_API_MakeOptionalArray(MakeOptionalArray);
144 TH_EXPORT_CPP_API_SendReturnResult(SendReturnResult);
145 TH_EXPORT_CPP_API_GetTest(GetTest);
146 // NOLINTEND