• 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 
16 #ifndef COMMON_INTERFACES_BASE_COMMON_H
17 #define COMMON_INTERFACES_BASE_COMMON_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <iostream>
22 
23 // Windows platform will define ERROR, cause compiler error
24 #ifdef ERROR
25 #undef ERROR
26 #endif
27 
28 namespace common {
29 #ifndef PANDA_TARGET_WINDOWS
30 #define PUBLIC_API __attribute__((visibility ("default")))
31 #else
32 #define PUBLIC_API __declspec(dllexport)
33 #endif
34 
35 #define NO_INLINE_CC __attribute__((noinline))
36 
37 #define LIKELY_CC(exp) (__builtin_expect((exp) != 0, true))
38 #define UNLIKELY_CC(exp) (__builtin_expect((exp) != 0, false))
39 
40 #define UNREACHABLE_CC()                                             \
41     do {                                                             \
42         std::cerr << "This line should be unreachable" << std::endl; \
43         std::abort();                                                \
44         __builtin_unreachable();                                     \
45     } while (0)
46 
47 #define CHECK_CC(expr) \
48     do { \
49         if (UNLIKELY_CC(!(expr))) { \
50             std::cerr << "CHECK FAILED: " << #expr; \
51             std::cerr << "          IN: " << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << std::endl; \
52             std::abort(); \
53         } \
54     } while (0)
55 
56 #if defined(NDEBUG)
57 #define ALWAYS_INLINE_CC __attribute__((always_inline))
58 #define DCHECK_CC(x)
59 #else
60 #define ALWAYS_INLINE_CC
61 #define DCHECK_CC(x) CHECK_CC(x)
62 #endif
63 
64 #if defined(__cplusplus)
65 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
66 #define DEFAULT_COPY_CTOR_CC(TypeName)      \
67     TypeName(const TypeName&) = default
68 
69 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
70 #define DEFAULT_COPY_OPERATOR_CC(TypeName)          \
71     /* NOLINTNEXTLINE(misc-macro-parentheses) */ \
72     TypeName& operator=(const TypeName&) = default
73 
74 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
75 #define DEFAULT_COPY_SEMANTIC_CC(TypeName) \
76     DEFAULT_COPY_CTOR_CC(TypeName);        \
77     DEFAULT_COPY_OPERATOR_CC(TypeName)
78 
79 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
80 #define NO_COPY_CTOR_CC(TypeName) \
81     TypeName(const TypeName&) = delete
82 
83 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
84 #define NO_COPY_OPERATOR_CC(TypeName) \
85     void operator=(const TypeName&) = delete
86 
87 // Disabling copy ctor and copy assignment operator.
88 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
89 #define NO_COPY_SEMANTIC_CC(TypeName) \
90     NO_COPY_CTOR_CC(TypeName);        \
91     NO_COPY_OPERATOR_CC(TypeName)
92 
93 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
94 #define NO_MOVE_CTOR_CC(TypeName)                   \
95     /* NOLINTNEXTLINE(misc-macro-parentheses) */ \
96     TypeName(TypeName&&) = delete
97 
98 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
99 #define NO_MOVE_OPERATOR_CC(TypeName)               \
100     /* NOLINTNEXTLINE(misc-macro-parentheses) */ \
101     TypeName& operator=(TypeName&&) = delete
102 
103 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
104 #define NO_MOVE_SEMANTIC_CC(TypeName) \
105     NO_MOVE_CTOR_CC(TypeName);        \
106     NO_MOVE_OPERATOR_CC(TypeName)
107 
108 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
109 #define DEFAULT_NOEXCEPT_MOVE_CTOR_CC(TypeName)     \
110     /* NOLINTNEXTLINE(misc-macro-parentheses) */    \
111     TypeName(TypeName&&) noexcept = default
112 
113 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
114 #define DEFAULT_NOEXCEPT_MOVE_OPERATOR_CC(TypeName) \
115     /* NOLINTNEXTLINE(misc-macro-parentheses) */    \
116     TypeName& operator=(TypeName&&) noexcept = default
117 
118 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
119 #define DEFAULT_NOEXCEPT_MOVE_SEMANTIC_CC(TypeName) \
120     DEFAULT_NOEXCEPT_MOVE_CTOR_CC(TypeName);        \
121     DEFAULT_NOEXCEPT_MOVE_OPERATOR_CC(TypeName)
122 
123 #endif  // defined(__cplusplus)
124 
125 enum class PUBLIC_API LOG_LEVEL : uint8_t {
126     DEBUG = 0,
127     INFO = 1,
128     WARN = 2,
129     ERROR = 3,
130     FATAL = 4,
131     FOLLOW = 100, // if hilog enabled follow hilog, otherwise use INFO level
132 };
133 }  // namespace common
134 
135 #endif  // COMMON_INTERFACES_BASE_COMMON_H
136