c++中function的实现原理

直接点击下面的链接去B站查看即可

视频链接

代码

#ifndef __JIE_FUNCTOR_H__
#define __JIE_FUNCTOR_H__

namespace Jie {

union AnyData {
    constexpr static int Max = 128;

    template <typename Type>
    constexpr Type* getPointer() noexcept {
        return Max >= sizeof(Type) ? reinterpret_cast<Type*>(buf) : reinterpret_cast<Type*>(pointer);
    }

    template <typename Type>
    void allocMemory() {
        pointer = std::malloc(sizeof(Type));      
    }

    void free() {
        std::free(pointer);              
    }

    void* pointer;
    char buf[Max];
};


template <typename FunctorType, bool IsMemory = (AnyData::Max < sizeof(FunctorType))>
struct AnyDataManage {
    constexpr static void Init(AnyData* dest, const FunctorType& source) {      
        dest->allocMemory<FunctorType>();                             
        new(dest->getPointer<FunctorType>()) FunctorType(source);        
    }

    constexpr static void Destruct(AnyData* source) {
        source->getPointer<FunctorType>()->~FunctorType();
        source->free();
    }
};

template <typename FunctorType>
struct AnyDataManage<FunctorType, false> {
    constexpr static void Init(AnyData* dest, const FunctorType& source) {                                 
        new(dest->getPointer<FunctorType>()) FunctorType(source);        
    }

    constexpr static void Destruct(AnyData* source) {
        source->getPointer<FunctorType>()->~FunctorType();        
    }
};

template <typename ResultType, typename FunctorType, typename ...ArgsType>
struct InvokeManage {
    constexpr static ResultType Invoke(AnyData* source, ArgsType&& ...args) {
        return (*source->getPointer<FunctorType>())(std::forward<ArgsType>(args) ...);
    }

    constexpr static void Destruct(AnyData* source) {
        AnyDataManage<FunctorType>::Destruct(source);
    }
};

template <typename FunctorType, typename ...ArgsType>
struct InvokeManage<void, FunctorType, ArgsType ...> {
    constexpr static void Invoke(AnyData* source, ArgsType&& ...args) {
        (*source->getPointer<FunctorType>())(std::forward<ArgsType>(args) ...);
    }

    constexpr static void Destruct(AnyData* source) {
        AnyDataManage<FunctorType>::Destruct(source);
    }
};

template <typename ...ArgsType>
struct Functor;

template <typename ResultType, typename ...ArgsType>
struct Functor<ResultType(ArgsType ...)> {
    using InvokeType = ResultType(*)(AnyData*, ArgsType&&...);
    using DestructType = void(*)(AnyData*);

    Functor() = default;

    template <typename FunctorType>
    Functor(FunctorType functor) {        
        AnyDataManage<FunctorType>::Init(&_anyData, functor);     
        _invoke = InvokeManage<ResultType, FunctorType, ArgsType...>::Invoke;
        _destruct = InvokeManage<ResultType, FunctorType, ArgsType...>::Destruct;
    }

    template <typename FunctorType>
    void setInvoke(FunctorType functor) {        
        AnyDataManage<FunctorType>::Init(&_anyData, functor);     
        _invoke = InvokeManage<ResultType, FunctorType, ArgsType...>::Invoke;
        _destruct = InvokeManage<ResultType, FunctorType, ArgsType...>::Destruct;
    }

    ResultType operator()(ArgsType&&... args) {    
        return _invoke(&_anyData, std::forward<ArgsType>(args) ...);
    }

    void free() {
        if (_destruct) {
            _destruct(&_anyData);
            _destruct = nullptr;
        }
    }

    ~Functor() {
        if (_destruct) 
            _destruct(&_anyData);
    }

private:
    AnyData _anyData;
    InvokeType _invoke;
    DestructType _destruct = nullptr;
};


template <typename ...ArgsType>
struct Functor<void(ArgsType ...)> {
    using InvokeType = void(*)(AnyData*, ArgsType&&...);
    using DestructType = void(*)(AnyData*);

    Functor() = default;

    template <typename FunctorType>
    Functor(FunctorType functor) {        
        AnyDataManage<FunctorType>::Init(&_anyData, functor);     
        _invoke = InvokeManage<void, FunctorType, ArgsType...>::Invoke;
        _destruct = InvokeManage<void, FunctorType, ArgsType...>::Destruct;
    }

    template <typename FunctorType>
    void setInvoke(FunctorType functor) {        
        AnyDataManage<FunctorType>::Init(&_anyData, functor);     
        _invoke = InvokeManage<void, FunctorType, ArgsType...>::Invoke;
        _destruct = InvokeManage<void, FunctorType, ArgsType...>::Destruct;
    }

    void operator()(ArgsType&&... args) {    
        _invoke(&_anyData, std::forward<ArgsType>(args) ...);
    }

    void free() {
        if (_destruct) {
            _destruct(&_anyData);
            _destruct = nullptr;
        }
    }

    ~Functor() {
        if (_destruct) 
            _destruct(&_anyData);
    }

private:
    AnyData _anyData;
    InvokeType _invoke;
    DestructType _destruct = nullptr;
};

}


#endif
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值