From 558c9d4cf7345b766ffccf0fe62c3bd32ff026e4 Mon Sep 17 00:00:00 2001 From: "Tod.Tian" Date: 2021年8月30日 16:59:38 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E4=B8=80=E4=B8=AAAOP,=20?= =?UTF-8?q?=E6=B2=A1=E5=BF=85=E9=A1=BB=E8=A6=81=E6=B1=82before=E5=92=8Caft?= =?UTF-8?q?er=E9=83=BD=E6=9C=89=EF=BC=8C=E5=8F=AF=E4=BB=A5=E5=8F=AA?= =?UTF-8?q?=E6=9C=89=E5=85=B6=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aop/Aop.hpp | 15 ++++++++++++++- Aop/main.cpp | 12 +++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Aop/Aop.hpp b/Aop/Aop.hpp index 2d081c0..d3817d7 100644 --- a/Aop/Aop.hpp +++ b/Aop/Aop.hpp @@ -62,13 +62,26 @@ class Aspect } template - void invoke(Args&&... args, Head&& headAsp, Tail&&... tailAsp) + typename std::enable_if ::value && HasMemberafter::value>::type invoke(Args&&... args, Head&& headAsp, Tail&&... tailAsp) { headAsp.before(std::forward(args)...); invoke(std::forward(args)..., std::forward(tailAsp)...); headAsp.after(std::forward(args)...); } + template + typename std::enable_if::value && HasMemberafter::value>::type invoke(Args&&... args, Head&& headAsp, Tail&&... tailAsp) + { + invoke(std::forward(args)..., std::forward(tailAsp)...); + headAsp.after(std::forward(args)...); + } + + template + typename std::enable_if::value && !HasMemberafter::value>::type invoke(Args&&... args, Head&& headAsp, Tail&&... tailAsp) + { + headAsp.before(std::forward(args)...); + invoke(std::forward(args)..., std::forward(tailAsp)...); + } private: Function m_func = nullptr; // 被织入的函数 }; diff --git a/Aop/main.cpp b/Aop/main.cpp index ff2ca96..18380f1 100644 --- a/Aop/main.cpp +++ b/Aop/main.cpp @@ -34,6 +34,16 @@ class B } }; +class C +{ +public: + void before(int n) + { + std::cout << "before from C: " << n << std::endl; + } + +}; + void coreFunc(int n) { std::cout << "invoke coreFunc: " << n << std::endl; @@ -46,7 +56,7 @@ void coreFunc2() int main() { - aopInvoke(&coreFunc, 100); + aopInvoke(&coreFunc, 100); aopInvoke(&coreFunc2); return 0; } From 59e74d07962706a1980fcc7ad64bf7a91e1073ac Mon Sep 17 00:00:00 2001 From: "Tod.Tian" Date: 2021年9月13日 16:57:31 +0800 Subject: [PATCH 2/3] fix runtime error for ref to tmp variable --- Factory/Factory.hpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Factory/Factory.hpp b/Factory/Factory.hpp index ba47e3a..6995fb0 100644 --- a/Factory/Factory.hpp +++ b/Factory/Factory.hpp @@ -20,10 +20,10 @@ class Factory m_messageMap.emplace(key, []{ return new T(); }); } - template - static void registerMessage(int key, Args... args) + template + static void registerMessage(int key, const Args&... args) { - m_messageMap.emplace(key, [&]{ return new T(args...); }); + m_messageMap.emplace(key, [=] { return new T(args...); }); } static Message* get(int key) @@ -46,10 +46,13 @@ class Factory return std::shared_ptr(get(key)); } + using MessageID = int; + using MessageCreationFunc = std::function; + private: - static std::unordered_map> m_messageMap; + static std::unordered_map m_messageMap; }; - -std::unordered_map> Factory::m_messageMap; +// warning : this imply header only include once +std::unordered_map Factory::m_messageMap; #endif From 773b6eab2736803f4654af57ec20c06c8acefeac Mon Sep 17 00:00:00 2001 From: "Tod.Tian" Date: 2021年9月17日 15:30:34 +0800 Subject: [PATCH 3/3] fix bad type info in gcc --- FunctionTraits/main.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/FunctionTraits/main.cpp b/FunctionTraits/main.cpp index b63fd1e..c91c49b 100644 --- a/FunctionTraits/main.cpp +++ b/FunctionTraits/main.cpp @@ -1,11 +1,19 @@ +#include "FunctionTraits.hpp" #include +#if defined(__GNUC__) +#include +#endif #include -#include "FunctionTraits.hpp" +#include template void printType() { +#if defined(__GNUC__) + std::cout << abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, nullptr) << std::endl; +#else std::cout << typeid(T).name() << std::endl; +#endif } float (*func)(std::string, int);

AltStyle によって変換されたページ (->オリジナル) /