function_hook.h 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/**
 * \file imperative/src/include/megbrain/imperative/function_hook.h
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#pragma once

#include "megbrain/utils/thin/function.h"

namespace mgb {
namespace imperative {
18

19 20 21 22 23 24
template <typename TFunction>
class FunctionHooker;

template <typename TRet, typename... TArgs>
class FunctionHooker<TRet(TArgs...)> {
public:
25
    using FunctionType = thin_function<TRet(TArgs...)>;
26 27
    //Type of hooks. Hook should accept a real function as argument
    //and invoke it on an appropriate time
28
    using HookType = thin_function<TRet(FunctionType, TArgs...)>;
29 30 31
    explicit FunctionHooker(FunctionType* fptr) : m_fptr{fptr} {
        m_backup = {nullptr, [](FunctionType*){}};
    }
32 33 34 35 36

public:
    FunctionHooker& apply_hook(HookType&& hook) {
        if (!m_backup) {
            FunctionType* backup = new FunctionType(*m_fptr);
37
            //Restore hooked function, would be invoked when destructed
38 39 40 41 42 43 44
            std::function<void(FunctionType*)> restorer =
                    [fptr = m_fptr](FunctionType* bkp) -> void {
                *fptr = *bkp;
                delete bkp;
            };
            m_backup = decltype(m_backup)(backup, restorer);
        }
45
        //Replace with hooked version
46
        *m_fptr = [func = *m_fptr, hook](TArgs... args) -> TRet {
47 48
            return hook(func, std::forward<TArgs>(args)...);
        };
49
        //Convinent for chain call
50 51 52 53 54 55 56 57
        return *this;
    }

private:
    FunctionType* m_fptr;
    std::unique_ptr<FunctionType, std::function<void(FunctionType*)>> m_backup;
};

58
//Helps to deduce template args
59 60
template <typename TRet, typename... TArgs>
FunctionHooker(thin_function<TRet(TArgs...)>* f)
61
        -> FunctionHooker<TRet(TArgs...)>;
62

63 64 65 66 67 68
template<typename TSignature>
auto make_shared_hook(thin_function<TSignature>* fptr){
    return std::make_shared<FunctionHooker<TSignature>>(fptr);
}

}  // namespace imperative
69
}  // namespace mgb