Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

轻量aop 一个疑问 #44

Open
shelsing opened this issue Feb 20, 2022 · 4 comments
Open

轻量aop 一个疑问 #44

shelsing opened this issue Feb 20, 2022 · 4 comments

Comments

@shelsing
Copy link

aop切面的递归展开只有最里层才允许没有Before或者After其中一个,外层全部都必须要Before和After
和博客里的设计初衷有偏差 https://www.cnblogs.com/qicosmos/p/4772389.html

@shelsing
Copy link
Author

把外层的也加上类似enable_if倒是能实现需求,但是感觉有点繁琐。加起来六个了。
c++ 17的 if constexpr倒是能很方便的解决这个问题,只是目前我用的主要 c++11

template <typename T>
    typename std::enable_if<has_member_Before<T, Args...>::value && has_member_After<T, Args...>::value>::type Invoke(Args&&... args, T&& aspect)
    {
        aspect.Before(std::forward<Args>(args)...); //核心逻辑之前的切面逻辑
        m_func(std::forward<Args>(args)...); //核心逻辑
        aspect.After(std::forward<Args>(args)...); //核心逻辑之后的切面逻辑
    }

    template <typename T>
    typename std::enable_if<has_member_Before<T, Args...>::value && !has_member_After<T, Args...>::value>::type Invoke(Args&&... args, T&& aspect)
    {
        aspect.Before(std::forward<Args>(args)...); //核心逻辑之前的切面逻辑
        m_func(std::forward<Args>(args)...); //核心逻辑
    }

    template <typename T>
    typename std::enable_if<!has_member_Before<T, Args...>::value && has_member_After<T, Args...>::value>::type Invoke(Args&&... args, T&& aspect)
    {
        m_func(std::forward<Args>(args)...); //核心逻辑
        aspect.After(std::forward<Args>(args)...); //核心逻辑之后的切面逻辑
    }

    template <typename Head, typename... Tail>
    typename std::enable_if<has_member_Before<Head, Args...>::value && has_member_After<Head, Args...>::value>::type Invoke(Args&&... args, Head&& headAspect, Tail&&... tailAspect)
    {

        headAspect.Before(std::forward<Args>(args)...);
        Invoke(std::forward<Args>(args)..., std::forward<Tail>(tailAspect)...);
        headAspect.After(std::forward<Args>(args)...);
    }

    template <typename Head, typename... Tail>
    typename std::enable_if<!has_member_Before<Head, Args...>::value && has_member_After<Head, Args...>::value>::type Invoke(Args&&... args, Head&& headAspect, Tail&&... tailAspect)
    {

        Invoke(std::forward<Args>(args)..., std::forward<Tail>(tailAspect)...);
        headAspect.After(std::forward<Args>(args)...);
    }

    template <typename Head, typename... Tail>
    typename std::enable_if<has_member_Before<Head, Args...>::value && !has_member_After<Head, Args...>::value>::type Invoke(Args&&... args, Head&& headAspect, Tail&&... tailAspect)
    {
        headAspect.Before(std::forward<Args>(args)...);
        Invoke(std::forward<Args>(args)..., std::forward<Tail>(tailAspect)...);
    }

@qicosmos
Copy link
Owner

我想说的是尽快升级到c++17以上,c++11已经是十多年前的标准了,有一些过时了。

@wuguinianjing
Copy link

up大大你好,请问C++17的跨平台性怎么样了,各个平台行为一致了吗?

@qicosmos
Copy link
Owner

C++17 是iso 标准,各个平台都会遵循标准去实现,标准C++跨平台没有问题,另外现在都开始用C++20 了,如果能一步升级到C++20 更好。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants