常州建设银行网站首页,了解做房产广告的网站,想开个网站怎样开,花都定制型网站建设转自:https://www.cnblogs.com/qicosmos/archive/2013/04/07/3003480.html #xff08;原创#xff09; C 轻量级AOP框架 c11 boost技术交流群#xff1a;296561497#xff0c;欢迎大家来交流技术。 c中开源的AOP框架AspectC需要单独编译才能将切面的代码织入到核心逻辑代码… 转自:https://www.cnblogs.com/qicosmos/archive/2013/04/07/3003480.html 原创 C 轻量级AOP框架 c11 boost技术交流群296561497欢迎大家来交流技术。 c中开源的AOP框架AspectC需要单独编译才能将切面的代码织入到核心逻辑代码中感觉使用起来不方便不能满足快速开发要求。我希望只要实现方法拦截即可能织入before()和after()操作就行不追求动态织入。思路是这样的通过一个包装类里面定义before()和after()方法和-运算符重载方法在重载操作函数中实现before()和after()操作。具体代码如下 1 #include boost/shared_ptr.hpp2 3 template typename WrappedType, typename DerivedAspect4 class BaseAspect5 {6 protected:7 WrappedType* m_wrappedPtr; //被织入的对象8 9 //获取派生的切面对象
10 DerivedAspect* GetDerived()
11 {
12 return static_castDerivedAspect*(this);
13 }
14
15 //被织入对象的删除器用来自动触发切面中的After方法
16 struct AfterWrapper
17 {
18 DerivedAspect* m_derived;
19 AfterWrapper(DerivedAspect* derived): m_derived(derived) {};
20 void operator()(WrappedType* p)
21 {
22 m_derived-After(p);
23 }
24 };
25 public:
26 explicit BaseAspect(WrappedType* p) : m_wrappedPtr(p) {};
27
28
29 void Before(WrappedType* p) {
30 // Default does nothing
31 };
32
33 void After(WrappedType* p) {
34 // Default does nothing
35 }
36
37 //重载指针运算符用来织入切面Before和After
38 boost::shared_ptrWrappedType operator-()
39 {
40 GetDerived()-Before(m_wrappedPtr);
41 return boost::shared_ptrWrappedType(m_wrappedPtr, AfterWrapper(GetDerived()));
42 }
43 };
44
45 //织入切面的工厂函数, 返回包含被织入对象的切面
46 template template typename class Aspect, typename WrappedType
47 AspectWrappedType MakeAspect(WrappedType* p)
48 {
49 return AspectWrappedType(p);
50 } BaseAspect为切面的基类提供了Before()和After()方法供派生的切面实现 下面看看具体的切面实现一个实现对函数运行时间的统计一个实现日志功能。 1 #include iostream2 #include boost/chrono/chrono.hpp3 #include boost/chrono/system_clocks.hpp4 5 templatetypename WrappedType6 class TimeElapsedAspect : public BaseAspect WrappedType, TimeElapsedAspectWrappedType 7 {8 typedef BaseAspectWrappedType, TimeElapsedAspectWrappedType BaseAspect;9 typedef boost::chrono::time_pointboost::chrono::system_clock, boost::chrono::durationdouble time_point;
10
11 time_point m_tmBegin;
12 public:
13 TimeElapsedAspect(WrappedType* p): BaseAspect(p) {}
14
15
16 void Before(WrappedType* p)
17 {
18 m_tmBegin boost::chrono::system_clock::now();
19 }
20
21 void After(WrappedType* p)
22 {
23 time_point end boost::chrono::system_clock::now();
24
25 std::cout Time: (end-m_tmBegin).count() std::endl;
26 }
27 }; TimeElapsedAspect切面实现对函数运行时间统计。 1 template typename WrappedType2 class LoggingAspect : public BaseAspectWrappedType, LoggingAspectWrappedType 3 {4 typedef BaseAspectWrappedType, LoggingAspectWrappedType BaseAspect;5 public:6 LoggingAspect(WrappedType* p): BaseAspect(p) {}7 8 void Before(WrappedType* p) 9 {
10 std::cout entering std::endl;
11 }
12
13 void After(WrappedType* p)
14 {
15 std::cout exiting std::endl;
16 }
17
18 }; LoggingAspect实现日志记录 现在来看看测试代码 class IX
{
public:IX(){}virtual ~IX(){}virtual void g()0;
private:};class X : public IX
{
public:void g() {std::cout it is a test std::endl;}};void TestAop()
{boost::shared_ptrIX p(new X());MakeAspectTimeElapsedAspect(p.get())-g();MakeAspectLoggingAspect(p.get())-g();
} 测试结果 总结 这个简单的AOP实现可以实现对类的方法进行拦截具体切面自由定制不过还有个地方不太完善还不支持切面的组合这个可以用TypeList去实现。 一点梦想尽自己一份力让c的世界变得更美好