Astd-BOL

BOL库可以获取函数参数个数或者获取地址或者进行位运算

代码

宏定义:

#ifndef __BOL__
#define __BOL__ 202108L
#define __BOL_VER_ "1.0.0"
#define __BOL_VER_F 1
#define __BOL_VER_S 0
#define __BOL_VER_T 0
#endif

//得到一个field在结构体中的偏移量
#define FPOS(type, field) ((unsigned long)&((type*)0)->field)

//得到一个field所占字节数
#define FSIZE(type, field) sizeof(((type*)0)->field)

//2Bytes->Word(LSB)
#define FLIPW(ray) ((((unsigned short)(ray)[0]) * 256) + (ray)[1])

//Word->2Bytes(LSB)
#define FLOPW(ray, val)\
(ray)[0] = ((val) / 255);\
(ray)[1] = ((val) & 0xFF)

//得到一个变量的地址
#define B_PTR(var) ((unsigned char*)(void*) & (var))
#define W_PTR(var) ((unsigned short*)(void*) & (var))

//得到一个字的高位和低位字节
#define WORD_LOW(var) ((unsigned char)((unsigned short)(var) & 255))
#define WORD_HIG(var) ((unsigned char)((unsigned short)(var) >> 8))

//获取数组长度
#define ARRAY_SIZE(var) (siezof((a))/sizeof((a[0])))

//获取n尾的值
#define MOD_BY_POWER_OF_TWO(val, mod_by) ((unsigned long)(val) & (unsigned long)(mod_by) - 1)

//用函数地址调用函数
#define USEADDR(name, var1, var2, args...) name(var1, var2, ##args)

函数:

/*获取成员函数地址, 两种方法*/
template<typename dst_type,
    typename src_type>
inline dst_type pointer_cast(src_type src);

template<typename dst_type,
    typename src_type>
inline dst_type union_cast(src_type src);
//获取函数参数个数
//https://www.cnblogs.com/pandamohist/p/13705204.html(来源)
template<typename R,
    typename... Args>
inline int getVarNum(R(*)(Args...));
inline int getk(int a, int k); //取n的第k位
inline void setk_zero(int& a, int k); //将第n位置0
inline void setk_one(int& a, int k); //将第n位置1
inline void to_left(int& a, int k); //循环左移k次
inline void to_right(int& a, int k); //循环右移k次

用法举例:

#include <iostream>
#include <bitset>
#include "bol.h"
using namespace std;

struct Test{
    int field;
    double field2;
};

class TestClass{
public:
    void function()
    {
        cout << "使用函数地址调用函数" << endl;
    }
};

void test(int, int, string, double, const char*) {}
using namespace astd;

int main()
{
    typedef void (*func)(void* pThis, int edx);
    cout << pointer_cast<void*>(&TestClass::function) << endl;
    cout << union_cast<void*>(&TestClass::function) << endl;
    func func1 = pointer_cast<func>(&TestClass::function);
    USEADDR(func1, NULL, NULL);
    std::string ray2 = "测试";
    cout<<getVarNum(test)<<endl;
    cout << FPOS(Test, field2) << endl;
    cout << FSIZE(Test, field) << endl;
    cout << FLIPW(ray2) << endl;
    cout << ray2 << endl;
    unsigned short tmp = FLIPW(ray2);
    cout << tmp << endl;
    FLOPW(ray2, tmp);
    cout << ray2 << endl;
    int tmp2 = 1111112545;
    cout << W_PTR(tmp2) << endl;
    cout << WORD_LOW(tmp2) << endl;
    cout << WORD_HIG(tmp2) << endl;
    cout << MOD_BY_POWER_OF_TWO(114514, 114) << endl;
    int num = 5444;
    cout << num << endl;
    cout << bitset<16>(num) << endl;
    cout << getk(num, 0) << endl;
    cout << getk(num, 1) << endl;
    cout << getk(num, 2) << endl;
    cout << getk(num, 3) << endl;
    cout << getk(num, 4) << endl;
    cout << getk(num, 5) << endl;
    cout << getk(num, 6) << endl;
    cout << getk(num, 7) << endl;
    cout << getk(num, 8) << endl;
    cout << getk(num, 9) << endl;
    cout << getk(num, 10) << endl;
    cout << getk(num, 11) << endl;
    cout << getk(num, 12) << endl;
    cout << getk(num, 13) << endl;
    cout << getk(num, 14) << endl;
    cout << getk(num, 15) << endl;
    setk_zero(num, 13);
    cout << num << endl;
    setk_one(num, 13);
    cout << num << endl;
    
    setk_zero(num, 3);
    cout << num << endl;
    setk_one(num, 12);
    cout << num << endl;
    
    to_right(num, 6);
    cout << num << endl;
    to_left(num, 4);
    cout << num << endl;
    to_right(num, 30);
    cout << num << endl;
    return 0;
}

输出:

0x401f10
0x401f10
使用函数地址调用函数
5
8
4
16770485
测试
58805
测试
0x7ffd942b9bd4
a
;
80
5444
0001010101000100
0
0
1
0
0
0
1
0
1
0
1
0
1
0
0
0
1348
5444
5440
7488
117
117
7488