宏在c++项目中的妙用

1.宏的定义

#define命令是C语言中的一个宏定义命令,它用来将一个标识符定义为一个字符串,该标识符被称为宏名,被定义的字符串称为替换文本

2.宏示例展示

== COMPONENT_EVENT(name, func) 该宏用于类中定义两个成员函数,像SetFunction,GetFunction。==
在这里插入图片描述

#define COMPONENT_EVENT(name, func)                                                             \
    private:                                                                                    \
        std::unique_ptr<std::function<func>> event##name##_;                                    \
    public:                                                                                     \
        const std::unique_ptr<std::function<func>>& Get##name() const                           \
        {                                                                                       \
            return event##name##_;                                                              \
        }                                                                                       \
        void Set##name(std::function<func>&& event##name)                                       \
        {                                                                                       \
            if (event##name) {                                                                  \
                event##name##_ = std::make_unique<std::function<func>>(std::move(event##name)); \
            } else {                                                                            \
                event##name##_.reset();                                                         \
            }                                                                                   \
        }

== DISALLOW_COPY_AND_MOVE(className)该宏用于类不产生拷贝构造,赋值构造,移动语义构造。==
在这里插入图片描述

#define DISALLOW_COPY_AND_MOVE(className) \
    DISALLOW_COPY(className);             \
    DISALLOW_MOVE(className)

#define DISALLOW_COPY(className)      \
    className(const className&) = delete; \
    className& operator=(const className&) = delete

#define DISALLOW_MOVE(className) \
    className(className&&) = delete; \
    className& operator=(className&&) = delete

代码示例

该示例中也用宏DECLARE_SINGLETON来声明该类为单例类。

#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>  //文件操作头文件
#include <thread>   //多线程头文件
#include <mutex>
#include <shared_mutex>
#include <functional>
#include <vector>
#include <memory>
using namespace std;

/*
demo展示宏在c++的应用,感受宏带来的便利
1.set,get函数以宏来代表
2.单例实现用宏取代
*/

#define COMPONENT_EVENT(name, func)                                                             \
    private:                                                                                    \
        std::unique_ptr<std::function<func>> event##name##_;                                    \
    public:                                                                                     \
        const std::unique_ptr<std::function<func>>& Get##name() const                           \
        {                                                                                       \
            return event##name##_;                                                              \
        }                                                                                       \
        void Set##name(std::function<func>&& event##name)                                       \
        {                                                                                       \
            if (event##name) {                                                                  \
                event##name##_ = std::make_unique<std::function<func>>(std::move(event##name)); \
            } else {                                                                            \
                event##name##_.reset();                                                         \
            }                                                                                   \
        }

#define DISALLOW_COPY_AND_MOVE(className) \
    DISALLOW_COPY(className);             \
    DISALLOW_MOVE(className)

#define DISALLOW_COPY(className)      \
    className(const className&) = delete; \
    className& operator=(const className&) = delete

#define DISALLOW_MOVE(className) \
    className(className&&) = delete; \
    className& operator=(className&&) = delete

#define DECLARE_SINGLETON(MyClass)               \
private:                                         \
    friend Singleton<MyClass>;                   \
    MyClass();                                   \
    ~MyClass();

class NonCopyable {
protected:
    NonCopyable() = default;
    virtual ~NonCopyable() = default;

private:
    DISALLOW_COPY_AND_MOVE(NonCopyable);
};

template<typename T>
class Singleton : public NonCopyable {
public:
    static T& GetInstance()
    {
        return instance_;
    }

private:
    static T instance_;
};
template <typename T>
T Singleton<T>::instance_;

class Component : public Singleton<Component> {
    DECLARE_SINGLETON(Component);
    public:
    //将上述的SetOnChange,GetOnChange用宏COMPONENT_EVENT进行替换,让代码更加简洁优美
    COMPONENT_EVENT(OnChange, void(int32_t));
    private:
};
Component::Component() = default;
Component::~Component() = default;

//定义函数对象
class EventCallback{
    public:
    void operator()(int32_t value){
        cout<<"EventCallback start,value is "<<value<<endl;
    }
};
//将组件的函数与定义的函数对象进行绑定
template<class C, class F>
bool BindEventFunction(
    void (C::*setMethod)(std::function<F>&&), int32_t& value)
{
    Component& component = Component::GetInstance();
    ((component).*setMethod)(EventCallback());
    return true;
}

int main(){
    int32_t info = 1;
    BindEventFunction(&Component::SetOnChange,info);
    std::function<void(int32_t)> func_ = *Component::GetInstance().GetOnChange();
    //触发绑定的回调函数对象
    func_(info);
    return 0;
}

3.运行结果

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex1_Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值