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;
}