prefix.h

c/c++ win32/linux 通用的宏定义头文件,在不影响代码执行效率的前提下,用来美化代码的,整理了一下,个人感觉挺好用的。

源码如下:

prefix.h
 1 //
 2 //  prefix.h
 3 //  otest
 4 //
 5 //  Created by leo_zhengyu on 2017/8/24.
 6 //  Copyright © 2017年 cy0104. All rights reserved.
 7 //
 8 #ifndef prefix_h
 9 #define prefix_h
10 #define cstr const char*
11 #define num double
12 
13 //brief log
14 #ifdef DEBUG
15 #define PRINT(s) printf("%s\n",s)
16 #define LOG(s) printf("%s",s)
17 #define LOGI(s) printf("%d",s)
18 #else
19 #define PRINT(s)
20 #define LOG(s)
21 #endif
22 
23 //start a varied array defination
24 #define ARR_START {
25 //end of array defination
26 #define ARR_END 0}
27 
28 //a multi-lined string
29 #define TEXT(...) #__VA_ARGS__
30 
31 //let <vn> from <start> to <end>
32 #define FOR(vn,start,end) {for(int vn=start;vn<=end;vn++){
33 //foreach <c> in string <s>
34 #define FOR_C_IN_S(s,c) {for(char* p##c = (char*)s;*p##c;p##c++){ char c = *pc;
35 //foreach <c> in string <s> including /0
36 #define FOR_C_IN_S_0(s,c) {for(char* p##c = (char*)s;p##c==s||*(p##c-1);p##c++){ char c = *pc;
37 //for <idx> in <arr>
38 #define FOR_I_IN_ARR(arr,idx) {for(int idx=0;idx<sizeof(arr)/sizeof(arr[0]);idx++){
39 //for <idx>:<ele> in <arr>
40 #define FOR_ELE_IN_ARR(arr,ele,idx) {for(int idx=0;idx<sizeof(arr)/sizeof(arr[0]);idx++){ typeof(arr[0]) ele = arr[idx];
41 //end block
42 #define END }}
43 
44 //switch <fac>
45 #define SELECT(fac) {{typeof(fac) factor = fac;
46 //save as <tar>,use <typename _> if you dont need target varient
47 #define TO(tar) tar = (0)?0
48 //WARNING: invalid in stdc
49 #define ROW(k,v) :__IS_EQUAL(factor,v)
50 //a number case
51 #define ROW_I(k,v) :factor==k?(v)
52 //a string case
53 #define ROW_CS(k,v) :strcmp(factor,k)==0?(v)
54 //a default case,necessary
55 #define ROW_DEFAULT(v) :(v);
56 
57 //macro alias
58 #define text TEXT
59 #define for_i FOR
60 #define for_c_in_s FOR_C_IN_S
61 #define for_c_in_s_0 FOR_C_IN_S_0
62 #define for_i_in_arr FOR_I_IN_ARR
63 #define for_ele_in_arr FOR_ELE_IN_ARR
64 #define end END
65 #define select SELECT
66 #define tab_k SELECT
67 #define to TO
68 #define row_cs ROW_CS
69 #define kv ROW_CS
70 #define row_i ROW_I
71 #define iv ROW_I
72 #define row_def ROW_DEFAULT
73 #define other(v) :(v);}}
74 
75 //run a code-line.To run multi-lined codes,use || to combine.
76 #define eval(codes) (codes)*0
77 
78 /*in c++ environment:
79 1.you are suggested to
80 use std::string instead of c-style string
81 and use kv instead of iv.
82 2.foreach ele in arr has a defer,which ele is a referance now.
83 */
84 #if defined(__cplusplus) || defined(c_plusplus)
85 #define kv iv
86 #undef iv
87 #define FOR_ELE_IN_ARR(arr,ele,idx) {for(int idx=0;idx<sizeof(arr)/sizeof(arr[0]);idx++){ auto ele = arr[idx];
88 #endif
89 
90 #endif /* prefix_h */

 主要的功能:
    0.复杂的字符串常量书写简化了,输入特殊字符不需要转义,默认忽略换行。

比如:
cstr s = text(name="leo");


    1.可以自己决定代码块缩进,排版更美观。

不缩进写法:
for_i(i,1,10)
...
end

 

缩进写法:
for_i(i,1,10)
{
    ...
}
end

 

或者:
{
    for_i(i,1,10)
    ...
    end
}


    2.for循环简化了,代码块结束配合关键字end。 
  for以闭包方式执行,也就是说循环变量不会影响之后的代码,比以前更合理了。
  for可以用最少的代码循环单个循环变量、遍历定长数组、遍历可变长数组。

例子:
for_i(i,1,10)//变量i只作用于代码段内
...
end
int j = i;//报错,i未定义


    3. switch/if else简化,代码更精简,映射关系一目了然,新的名字叫做tab_k。

例子:
tab_k(kname) to(target)
iv("1","one")
...
other("zero")


    在标准C下,可以使用iv和kv,kv用来方便的匹配char*;
    在cpp下,只有kv,k可以是任何类型,建议使用std::string代替char* 。
    另外, 如果想在v中执行代码,则需要配合eval关键字。
    匹配成功时,eval会执行括号内的代码。
    多条eval须使用||连接。
    4.变长数组初始化写法标准化,总是以0结尾,需要配合ARR_START和ARR_END。

下面这个是栗子,喜欢的话,快烤烤吃了吧。

 1     /*
 2      0.test of tab_k
 3      */
 4     int a=2;
 5     cstr b;
 6     
 7     tab_k(a) to(b)
 8     iv(0,"zero")
 9     iv(1,"one")
10     iv(2,"two")
11     iv(3,"three")
12     other("number");
13     
14     cstr star;
15     cstr sfac = "leo";
16     
17     tab_k(sfac) to(star)
18     kv("leo",    "lzy")
19     kv("wang","wh")
20     kv("lee",    "lb")
21     other("unknown");
22     
23     LOG(b);      //two
24     LOG(star);  //lzy
25     
26     /*
27      1.test of tab_k with eval
28      */
29     int c;
30     
31     tab_k(a) to(num _)//we dont care target varient
32     iv(0,c=1)
33     iv(1,eval(c=10)
34        ||  eval(foo(c))
35        )
36     iv(2,eval(printf("first,"))
37        ||  eval(c=100)
38        ||  eval(printf("second,"))
39        )
40     iv(3,c=1000)
41     other(0);
42     //first,second,
43     
44     LOGI(c);    //100
45     
46     /*
47      2.test of muti-lined string defination
48      */
49     char s[32] = text(
50           {
51               name:"leo",
52               age:18,
53           }
54       );
55     LOG(s);     //{ name:"leo", age:18, }
56     
57     /*
58      3.test of brief for block
59      */
60     for_i(i,1,5)
61     LOGI(i);
62     end
63     //12345
64     
65 #pragma pack(4)
66     typedef struct Player{
67         cstr name;
68         int  age;
69         //align & bit-check
70         int  suffix;
71     }Player,*lpPlayer;
72     
73     Player players[4] =
74     ARR_START
75     {"l" "zy",  18, 1},
76     {"w" "h",  56, 1},
77     ARR_END;//end with 0
78     
79     for_ele_in_arr(players,p,i)
80     LOG(p.name);
81     end
82     //lzy wh
83     
84     for_c_in_s(text(name="leo"),c)
85     printf("%c",c);
86     end
87     //name="leo"

 

posted on 2017-08-25 00:15  飞天里欧斯  阅读(274)  评论(0编辑  收藏  举报

导航