cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
macro.hpp File Reference

通用的编译器、平台、架构检测和实用宏定义 / Common macros for compiler, platform, architecture detection and utility macros More...

Include dependency graph for macro.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define CPP_TOOLBOX_STATIC_ASSERT(Condition, Message)    static_assert(Condition, Message)
 静态断言宏 / Static assertion macro
 
#define CPP_TOOLBOX_ASSERT(Condition, Message)   ((void)0)
 运行时断言宏(仅在调试模式下) / Runtime assertion macro (debug only)
 
#define CPP_TOOLBOX_UNREACHABLE()
 不可达代码标记 / Unreachable code marker
 
#define CPP_TOOLBOX_LIKELY(Condition)   __builtin_expect(!!(Condition), true)
 分支预测优化宏 / Branch prediction optimization macros
 
#define CPP_TOOLBOX_UNLIKELY(Condition)   __builtin_expect(!!(Condition), false)
 
#define CPP_TOOLBOX_FORCE_INLINE   inline
 强制内联宏 / Force inline macro
 
#define CPP_TOOLBOX_ALIGNAS(Alignment)   alignas(Alignment)
 函数名宏 / Function name macro
 
#define CPP_TOOLBOX_UNUSED(Variable)   ((void)(Variable))
 标记变量为未使用 / Mark variable as unused
 
#define CPP_TOOLBOX_NODISCARD   [[nodiscard]]
 不可丢弃属性 / No discard attribute
 
#define CPP_TOOLBOX_FALLTHROUGH   [[fallthrough]]
 贯穿属性 / Fallthrough attribute
 
#define CPP_TOOLBOX_LOG_DEBUG(fmt, ...)   ((void)0)
 调试日志宏 / Debug logging macro
 
#define CPP_TOOLBOX_COUNT_ARGS(...)    CPP_TOOLBOX_COUNT_ARGS_IMPL(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
 计算参数数量 / Count number of arguments
 
#define CPP_TOOLBOX_COUNT_ARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, Count, ...)    Count
 
#define CPP_TOOLBOX_REPEAT_2(x)   x x
 重复宏 / Repeat macros
 
#define CPP_TOOLBOX_REPEAT_3(x)   CPP_TOOLBOX_REPEAT_2(x) x
 
#define CPP_TOOLBOX_REPEAT_4(x)   CPP_TOOLBOX_REPEAT_2(x) CPP_TOOLBOX_REPEAT_2(x)
 
#define CPP_TOOLBOX_REPEAT_5(x)   CPP_TOOLBOX_REPEAT_4(x) x
 
#define CPP_TOOLBOX_REPEAT(n, x)   CPP_TOOLBOX_REPEAT_##n(x)
 
#define CPP_TOOLBOX_STRING_CONCAT(a, b)   a##b
 字符串连接 / String concatenation
 
#define CPP_TOOLBOX_STRINGIZE(x)   CPP_TOOLBOX_STRINGIZE_IMPL(x)
 字符串化宏 / Stringize macro
 
#define CPP_TOOLBOX_STRINGIZE_IMPL(x)   #x
 
#define CPP_TOOLBOX_SAFE_CALL(Func)
 带异常处理的安全函数调用 / Safe function call with exception handling
 
#define __CURRENT_FUNCTION__   __PRETTY_FUNCTION__
 当前函数名宏 / Current function name macro
 

Detailed Description

通用的编译器、平台、架构检测和实用宏定义 / Common macros for compiler, platform, architecture detection and utility macros

Macro Definition Documentation

◆ __CURRENT_FUNCTION__

#define __CURRENT_FUNCTION__   __PRETTY_FUNCTION__

当前函数名宏 / Current function name macro

◆ CPP_TOOLBOX_ALIGNAS

#define CPP_TOOLBOX_ALIGNAS (   Alignment)    alignas(Alignment)

函数名宏 / Function name macro

数据对齐宏 / Data alignment macro

Parameters
Alignment对齐值 / Alignment value
CPP_TOOLBOX_ALIGNAS(16) float matrix[4][4];
#define CPP_TOOLBOX_ALIGNAS(Alignment)
函数名宏 / Function name macro
Definition macro.hpp:114

◆ CPP_TOOLBOX_ASSERT

#define CPP_TOOLBOX_ASSERT (   Condition,
  Message 
)    ((void)0)

运行时断言宏(仅在调试模式下) / Runtime assertion macro (debug only)

Parameters
Condition断言条件 / Assertion condition
Message错误消息 / Error message
void process(int* ptr) {
CPP_TOOLBOX_ASSERT(ptr != nullptr, "Null pointer not allowed");
// 处理指针 / Process pointer
}
#define CPP_TOOLBOX_ASSERT(Condition, Message)
运行时断言宏(仅在调试模式下) / Runtime assertion macro (debug only)
Definition macro.hpp:46

◆ CPP_TOOLBOX_COUNT_ARGS

#define CPP_TOOLBOX_COUNT_ARGS (   ...)     CPP_TOOLBOX_COUNT_ARGS_IMPL(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)

计算参数数量 / Count number of arguments

int count = CPP_TOOLBOX_COUNT_ARGS(a, b, c); // count = 3
#define CPP_TOOLBOX_COUNT_ARGS(...)
计算参数数量 / Count number of arguments
Definition macro.hpp:177

◆ CPP_TOOLBOX_COUNT_ARGS_IMPL

#define CPP_TOOLBOX_COUNT_ARGS_IMPL (   _1,
  _2,
  _3,
  _4,
  _5,
  _6,
  _7,
  _8,
  _9,
  _10,
  Count,
  ... 
)     Count

◆ CPP_TOOLBOX_FALLTHROUGH

#define CPP_TOOLBOX_FALLTHROUGH   [[fallthrough]]

贯穿属性 / Fallthrough attribute

switch(value) {
case 1:
doSomething();
case 2:
doMore();
break;
}
#define CPP_TOOLBOX_FALLTHROUGH
贯穿属性 / Fallthrough attribute
Definition macro.hpp:153

◆ CPP_TOOLBOX_FORCE_INLINE

#define CPP_TOOLBOX_FORCE_INLINE   inline

强制内联宏 / Force inline macro

CPP_TOOLBOX_FORCE_INLINE int add(int a, int b) {
return a + b;
}
#define CPP_TOOLBOX_FORCE_INLINE
强制内联宏 / Force inline macro
Definition macro.hpp:94

◆ CPP_TOOLBOX_LIKELY

#define CPP_TOOLBOX_LIKELY (   Condition)    __builtin_expect(!!(Condition), true)

分支预测优化宏 / Branch prediction optimization macros

if (CPP_TOOLBOX_LIKELY(ptr != nullptr)) {
// 常见情况 / Common case
} else {
// 罕见情况 / Rare case
}
#define CPP_TOOLBOX_LIKELY(Condition)
分支预测优化宏 / Branch prediction optimization macros
Definition macro.hpp:77

◆ CPP_TOOLBOX_LOG_DEBUG

#define CPP_TOOLBOX_LOG_DEBUG (   fmt,
  ... 
)    ((void)0)

调试日志宏 / Debug logging macro

CPP_TOOLBOX_LOG_DEBUG("Processing item %d", itemId);
#define CPP_TOOLBOX_LOG_DEBUG(fmt,...)
调试日志宏 / Debug logging macro
Definition macro.hpp:167

◆ CPP_TOOLBOX_NODISCARD

#define CPP_TOOLBOX_NODISCARD   [[nodiscard]]

不可丢弃属性 / No discard attribute

CPP_TOOLBOX_NODISCARD int getValue() {
return 42;
}
#define CPP_TOOLBOX_NODISCARD
不可丢弃属性 / No discard attribute
Definition macro.hpp:137

◆ CPP_TOOLBOX_REPEAT

#define CPP_TOOLBOX_REPEAT (   n,
 
)    CPP_TOOLBOX_REPEAT_##n(x)

◆ CPP_TOOLBOX_REPEAT_2

#define CPP_TOOLBOX_REPEAT_2 (   x)    x x

重复宏 / Repeat macros

CPP_TOOLBOX_REPEAT(3, "Hello ") // 输出: "Hello Hello Hello " / Outputs:
"Hello Hello Hello "
#define CPP_TOOLBOX_REPEAT(n, x)
Definition macro.hpp:195

◆ CPP_TOOLBOX_REPEAT_3

#define CPP_TOOLBOX_REPEAT_3 (   x)    CPP_TOOLBOX_REPEAT_2(x) x

◆ CPP_TOOLBOX_REPEAT_4

#define CPP_TOOLBOX_REPEAT_4 (   x)    CPP_TOOLBOX_REPEAT_2(x) CPP_TOOLBOX_REPEAT_2(x)

◆ CPP_TOOLBOX_REPEAT_5

#define CPP_TOOLBOX_REPEAT_5 (   x)    CPP_TOOLBOX_REPEAT_4(x) x

◆ CPP_TOOLBOX_SAFE_CALL

#define CPP_TOOLBOX_SAFE_CALL (   Func)
Value:
try { \
Func(); \
} catch (const std::exception& e) { \
LOG_ERROR_S << __FILE__ << ":" << __LINE__ << " " \
<< CPP_TOOLBOX_FUNCTION_NAME << " " \
<< "Exception: " << e.what(); \
}
#define LOG_ERROR_S
ERROR级别流式日志的宏 / Macro for ERROR level stream logging.
Definition thread_logger.hpp:1332

带异常处理的安全函数调用 / Safe function call with exception handling

Parameters
Func要调用的函数 / Function to call
void riskyOperation() {
// 可能抛出异常的代码 / Code that might throw
throw std::runtime_error("Error");
});
}
#define CPP_TOOLBOX_SAFE_CALL(Func)
带异常处理的安全函数调用 / Safe function call with exception handling
Definition macro.hpp:232

◆ CPP_TOOLBOX_STATIC_ASSERT

#define CPP_TOOLBOX_STATIC_ASSERT (   Condition,
  Message 
)     static_assert(Condition, Message)

静态断言宏 / Static assertion macro

Parameters
Condition断言条件 / Assertion condition
Message错误消息 / Error message
CPP_TOOLBOX_STATIC_ASSERT(sizeof(int) == 4, "Int must be 32 bits");
#define CPP_TOOLBOX_STATIC_ASSERT(Condition, Message)
静态断言宏 / Static assertion macro
Definition macro.hpp:22

◆ CPP_TOOLBOX_STRING_CONCAT

#define CPP_TOOLBOX_STRING_CONCAT (   a,
 
)    a##b

字符串连接 / String concatenation

#define PREFIX "my_"
#define NAME "variable"
char* full_name = CPP_TOOLBOX_STRING_CONCAT(PREFIX, NAME); // "my_variable"
#define CPP_TOOLBOX_STRING_CONCAT(a, b)
字符串连接 / String concatenation
Definition macro.hpp:206

◆ CPP_TOOLBOX_STRINGIZE

#define CPP_TOOLBOX_STRINGIZE (   x)    CPP_TOOLBOX_STRINGIZE_IMPL(x)

字符串化宏 / Stringize macro

#define VERSION 1.0
const char* version = CPP_TOOLBOX_STRINGIZE(VERSION); // "1.0"
#define CPP_TOOLBOX_STRINGIZE(x)
字符串化宏 / Stringize macro
Definition macro.hpp:216

◆ CPP_TOOLBOX_STRINGIZE_IMPL

#define CPP_TOOLBOX_STRINGIZE_IMPL (   x)    #x

◆ CPP_TOOLBOX_UNLIKELY

#define CPP_TOOLBOX_UNLIKELY (   Condition)    __builtin_expect(!!(Condition), false)

◆ CPP_TOOLBOX_UNREACHABLE

#define CPP_TOOLBOX_UNREACHABLE ( )
Value:
do { \
std::cerr << "Unreachable code reached" << std::endl; \
std::abort(); \
} while (false)

不可达代码标记 / Unreachable code marker

switch(value) {
case 1: return "one";
case 2: return "two";
}
#define CPP_TOOLBOX_UNREACHABLE()
不可达代码标记 / Unreachable code marker
Definition macro.hpp:60

◆ CPP_TOOLBOX_UNUSED

#define CPP_TOOLBOX_UNUSED (   Variable)    ((void)(Variable))

标记变量为未使用 / Mark variable as unused

Parameters
Variable变量名 / Variable name
void callback(int unused_param) {
CPP_TOOLBOX_UNUSED(unused_param);
}
#define CPP_TOOLBOX_UNUSED(Variable)
标记变量为未使用 / Mark variable as unused
Definition macro.hpp:126