#ifndef __has_builtin // Optional of course.
#define __has_builtin(x) 0 // Compatibility with non-clang compilers.
#endif
#if __has_builtin(__builtin_dump_struct))
__builtin_dump_struct(your_struct_point, &printf);
#endif
!!!不用再挨个变量加print了!!!
使用如上代码就可以利用编译器函数来实现快速打印结构体内容。
学习CLang编译器内置函数: https://clang.llvm.org/docs/LanguageExtensions.html
如果在安卓native c++,printf打印不出东西,那么就要自己定义printf。
int myPrint(const char *pFormat, ...)
{
char logText[MAXLOGLEN];
va_list args;
va_start(args, pFormat);
vsnprintf(logText, MAXLOGLEN, pFormat, args);
va_end(args);
__android_log_write(ANDROID_LOG_ERROR, "chwit", logText);
return 0;
}
或者用一个string去保存打印的字符串,最后再统一输出:
string chwit_str;
int myPrint(const char *pFormat, ...)
{
char logText[MAXLOGLEN];
va_list args;
va_start(args, pFormat);
vsnprintf(logText, MAXLOGLEN, pFormat, args);
va_end(args);
// __android_log_write(ANDROID_LOG_ERROR, "chwit", logText);
chwit_str += logText;
return 0;
}
本质:A.获取链表头,B.判断链表项是不是链表头,C.指向链表的下一项。
* list_for_each_entry - iterate over list of given type * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list_struct within the struct. */
#define list_for_each_entry(pos, head, member) \A.获取链表头,B.判断链表项是不是链表头,C.指向链表的下一项。
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
作用: 循环遍历每个pos实例中的member成员,在大多数情况下member成员是一个链表节点(list_head node)
其实list_for_each_entry就是遍历链表(list_head)的每个节点,返回每个节点所在的数据结构实例,另外还有一个宏
list_for_each_entry遍历的链表,其每一项都是某个结构体中的成员,单纯遍历链表还不行,还要找到包含这个
链表项的结构体的地址,从而为下一步应用该结构体做好准备。
将for循环分解为以下三点:
1. for循环初始化 pos = list_first_entry(head, typeof(*pos), member);
2. for循环执行条件 &pos->member != (head);
3. 每循环一次执行 list_next_entry(pos, member))
解析:
pos = list_first_entry(head, typeof(*pos), member); ,因为list_first_entry第二个参数是typeof(* pos)。
typeof()是取变量的类型,这里是取指针pos所指向数据的类型
结构体pos中包含链表member,遍历pos中的member成员。