这篇文章介绍了我在工程应用中经常会用到的c++工具代码。
编译期的todo list
下面这些宏能产生编译期的TODO/WARN/FIXME等信息,有助于编写代码。1
2
3
4
5
6
7
8
9
10
11//don't use these macros
//use these macros
例子:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25//test
void not_implement_function()
{
}
float fast_inv_sqrt(float x)
{
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i >> 1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;
}
float custom_add(float a, float b)
{
return a - b;
}
int main(int argc, char* argv[])
{
return 0;
}
vs2013输出结果:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
161> ====================================
1> d:\workspace\local\neontest\neontest.cpp(19)
1> TODO : this function is not implemented!
1> ====================================
1>
1>
1> ====================================
1> d:\workspace\local\neontest\neontest.cpp(24)
1> WARN : this function is implemented by a hack way!
1> ====================================
1>
1>
1> ====================================
1> d:\workspace\local\neontest\neontest.cpp(34)
1> FIXME : buggy ! !
1> ====================================
ScopeExit Tool
下面这个代码片段利用了c++11中的lambda表达式以及C++的RAII(Resource Acquisition Is Initialization)特性,使得代码及其优雅,在不用写wrapper class的情况下充分利用RAII的好处。能够降低编码的心智负担,在哪里申请资源就在哪里释放,保证必须成对出现的操作一定会成对执行。总之,好顶赞!
1 | namespace{ |
例子:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15void dump_data(const char* filename, const char* data, const int data_len)
{
//close file after dump data
FILE* fp = fopen(filename, "wb");
SCOPE_EXIT(if (fp){ fclose(fp); });
//delete data after process and dump
int* processed_data = new int[data_len];
SCOPE_EXIT(delete[] processed_data;);
for (int i = 0; i < data_len;i++)
{
processed_data[i] = data[i] + 100;
}
fwrite(processed_data, sizeof(processed_data[0]), data_len, fp);
}