• 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 "bigint_new.impl.hpp"
16 #include <cstdint>
17 #include "bigint_new.proj.hpp"
18 #include "stdexcept"
19 #include "taihe/runtime.hpp"
20 #include "bigint_new.MyUnion.proj.0.hpp"
21 #include "bigint_new.MyUnion.proj.1.hpp"
22 
23 #include <iostream>
24 
25 using namespace taihe;
26 
27 namespace {
28 // To be implemented.
29 
30 class WantImpl {
31 public:
32     array<uint64_t> a_ = {1, 2, 3};
WantImpl()33     WantImpl() {}
34 
SetBigInt(::taihe::array_view<uint64_t> a)35     void SetBigInt(::taihe::array_view<uint64_t> a)
36     {
37         a_ = a;
38         std::cout << "Received array (size = " << a.size() << "): ";
39         for (size_t i = 0; i < a.size(); ++i) {
40             std::cout << a[i] << " ";
41         }
42         std::cout << std::endl;
43     }
44 
GetBigInt()45     ::taihe::array<uint64_t> GetBigInt()
46     {
47         return a_;
48     }
49 };
50 
ProcessBigInt(array_view<uint64_t> a)51 array<uint64_t> ProcessBigInt(array_view<uint64_t> a)
52 {
53     array<uint64_t> res(a.size() + 1);
54     res[0] = 0;
55     for (int i = 0; i < a.size(); i++) {
56         res[i + 1] = a[i];
57         std::cerr << "arr[" << i << "] = " << a[i] << std::endl;
58     }
59     return res;
60 }
61 
PrintBigInt(::taihe::array_view<uint64_t> a)62 void PrintBigInt(::taihe::array_view<uint64_t> a)
63 {
64     for (int i = 0; i < a.size(); i++) {
65         std::cerr << "arr[" << i << "] = " << a[i] << std::endl;
66     }
67 }
68 
CreateBigInt(::taihe::array_view<uint64_t> a)69 ::taihe::array<uint64_t> CreateBigInt(::taihe::array_view<uint64_t> a)
70 {
71     array<uint64_t> res(a.size());
72     for (int i = 0; i < a.size(); i++) {
73         res[i] = a[i];
74         std::cerr << "arr[" << i << "] = " << a[i] << std::endl;
75     }
76     return res;
77 }
78 
GetWant()79 ::bigint_new::Want GetWant()
80 {
81     return taihe::make_holder<WantImpl, ::bigint_new::Want>();
82 }
83 
SetupStructAndPrint(::bigint_new::BigInt1 const & v)84 void SetupStructAndPrint(::bigint_new::BigInt1 const &v)
85 {
86     for (const auto &value : v.a) {
87         std::cout << "a bigint array<u64>: " << value << " ";
88     }
89     std::cout << std::endl;
90 }
91 
GetBigIntOptional(::taihe::optional_view<::taihe::array<uint64_t>> a)92 ::taihe::array<uint64_t> GetBigIntOptional(::taihe::optional_view<::taihe::array<uint64_t>> a)
93 {
94     if (!a.has_value()) {
95         return {0, 0};
96     }
97     const auto &value = a.value();
98     array<uint64_t> result(value.size());
99     for (int i = 0; i < value.size(); i++) {
100         result[i] = value[i];
101     }
102     return result;
103 }
104 
MapBigInt(::taihe::map_view<::taihe::string,::taihe::array<uint64_t>> a)105 ::taihe::map<::taihe::string, ::taihe::array<uint64_t>> MapBigInt(
106     ::taihe::map_view<::taihe::string, ::taihe::array<uint64_t>> a)
107 {
108     map<string, array<uint64_t>> result;
109     for (const auto &val : a) {
110         result.emplace(val.first, val.second);
111     }
112     return result;
113 }
114 
MakeMyUnion(int32_t n)115 ::bigint_new::MyUnion MakeMyUnion(int32_t n)
116 {
117     int32_t const case1Key = 1;
118     int32_t const case2Key = 2;
119 
120     array<uint64_t> bigIntData = {1, 2, 3};
121     string str = "this is string data";
122 
123     switch (n) {
124         case case1Key:
125             return ::bigint_new::MyUnion::make_bigIntValue(bigIntData);  // 自己做的转换
126         case case2Key:
127             return ::bigint_new::MyUnion::make_stringValue(str);
128         default:
129             return ::bigint_new::MyUnion::make_empty();
130     }
131 }
132 
ShowMyUnion(::bigint_new::MyUnion const & u)133 void ShowMyUnion(::bigint_new::MyUnion const &u)
134 {
135     if (auto ptr = u.get_bigIntValue_ptr()) {
136         std::cout << "bigIntValue: [";
137         for (const auto &val : *ptr) {
138             std::cout << " " << val << " ";
139         }
140         std::cout << "]\n";
141     } else if (auto ptr = u.get_stringValue_ptr()) {
142         std::cout << "stringValue: " << *ptr << "\n";
143     } else {
144         std::cout << "empty\n";
145     }
146 }
147 
148 }  // namespace
149 
150 TH_EXPORT_CPP_API_ProcessBigInt(ProcessBigInt);
151 TH_EXPORT_CPP_API_PrintBigInt(PrintBigInt);
152 TH_EXPORT_CPP_API_CreateBigInt(CreateBigInt);
153 TH_EXPORT_CPP_API_GetWant(GetWant);
154 TH_EXPORT_CPP_API_SetupStructAndPrint(SetupStructAndPrint);
155 TH_EXPORT_CPP_API_GetBigIntOptional(GetBigIntOptional);
156 TH_EXPORT_CPP_API_MapBigInt(MapBigInt);
157 TH_EXPORT_CPP_API_MakeMyUnion(MakeMyUnion);
158 TH_EXPORT_CPP_API_ShowMyUnion(ShowMyUnion);
159 // NOLINTEND