• 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 #ifndef RUNTIME_INCLUDE_TAIHE_COMMON_HPP_
16 #define RUNTIME_INCLUDE_TAIHE_COMMON_HPP_
17 // NOLINTBEGIN
18 
19 #include <taihe/common.h>
20 
21 #include <type_traits>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <utility>
25 
26 #ifdef __cplusplus
27 #ifdef __EXCEPTIONS
28 #define TH_THROW(error_type, message) throw error_type(message)
29 #else
30 #define TH_THROW(error_type, message)                                    \
31     do {                                                                 \
32         fprintf(stderr,                                                  \
33                 "%s: %s, \nfunction: %s, "                               \
34                 "file: %s, line %d.\n",                                  \
35                 #error_type, message, __FUNCTION__, __FILE__, __LINE__); \
36         abort();                                                         \
37     } while (0)
38 #endif
39 #endif
40 
41 namespace taihe {
42 template <typename cpp_t, typename = void>
43 struct as_abi;
44 
45 template <typename cpp_t>
46 using as_abi_t = typename as_abi<cpp_t>::type;
47 
48 template <typename cpp_owner_t, typename = void>
49 struct as_param;
50 
51 template <typename cpp_owner_t>
52 using as_param_t = typename as_param<cpp_owner_t>::type;
53 
54 template <typename cpp_t>
55 struct as_abi<cpp_t, std::enable_if_t<std::is_arithmetic_v<cpp_t>>> {
56     using type = cpp_t;
57 };
58 
59 template <typename cpp_owner_t>
60 struct as_param<cpp_owner_t, std::enable_if_t<std::is_arithmetic_v<cpp_owner_t>>> {
61     using type = cpp_owner_t;
62 };
63 
64 template <>
65 struct as_abi<void> {
66     using type = void;
67 };
68 
69 template <typename cpp_t, std::enable_if_t<!std::is_reference_v<cpp_t>, int> = 0>
into_abi(cpp_t && cpp_val)70 inline as_abi_t<cpp_t> into_abi(cpp_t &&cpp_val)
71 {
72     as_abi_t<cpp_t> abi_val;
73     new (&abi_val) cpp_t(std::move(cpp_val));
74     return abi_val;
75 }
76 
77 template <typename cpp_t, std::enable_if_t<!std::is_reference_v<cpp_t>, int> = 0>
into_abi(cpp_t & cpp_val)78 inline as_abi_t<cpp_t> into_abi(cpp_t &cpp_val)
79 {
80     as_abi_t<cpp_t> abi_val;
81     new (&abi_val) cpp_t(std::move(cpp_val));
82     return abi_val;
83 }
84 
85 template <typename cpp_t, std::enable_if_t<!std::is_reference_v<cpp_t>, int> = 0>
from_abi(as_abi_t<cpp_t> & abi_val)86 inline cpp_t &&from_abi(as_abi_t<cpp_t> &abi_val)
87 {
88     return reinterpret_cast<cpp_t &&>(abi_val);
89 }
90 
91 template <typename cpp_t, std::enable_if_t<!std::is_reference_v<cpp_t>, int> = 0>
from_abi(as_abi_t<cpp_t> && abi_val)92 inline cpp_t &&from_abi(as_abi_t<cpp_t> &&abi_val)
93 {
94     return reinterpret_cast<cpp_t &&>(abi_val);
95 }
96 
97 template <typename cpp_t, std::enable_if_t<std::is_reference_v<cpp_t>, int> = 0>
into_abi(cpp_t cpp_val)98 inline as_abi_t<cpp_t> into_abi(cpp_t cpp_val)
99 {
100     return reinterpret_cast<as_abi_t<cpp_t>>(&cpp_val);
101 }
102 
103 template <typename cpp_t, std::enable_if_t<std::is_reference_v<cpp_t>, int> = 0>
from_abi(as_abi_t<cpp_t> abi_val)104 inline cpp_t from_abi(as_abi_t<cpp_t> abi_val)
105 {
106     return reinterpret_cast<cpp_t>(*abi_val);
107 }
108 
109 // enum tags //
110 
111 template <auto tag>
112 struct static_tag_t {};
113 
114 template <auto tag>
115 constexpr static_tag_t<tag> static_tag;
116 }  // namespace taihe
117 // NOLINTEND
118 #endif  // RUNTIME_INCLUDE_TAIHE_COMMON_HPP_