aspect c++ tracing function control flow and input output parameters

人走茶凉 提交于 2019-12-04 17:37:31
#ifndef __trace_ah__
 #define __trace_ah__

#include <cstdio>
#include <iostream>
using namespace std;

template <int I> struct ArgPrinter 
{
  template <class JP> static inline void work (JP &tjp) {
    ArgPrinter<I - 1>::work (tjp);
    cout << "Arg " << I << ": " << *tjp.template arg<I - 1> () << endl;
  }
};    

template <> struct ArgPrinter<0> 
{
  template <class JP> static inline void work (JP &tjp) {}
};

// Control flow tracing example

    aspect trace {


            pointcut virtual methods() = "% ...::%(...)";

    template <class JP> void print_args (JP &tjp) 
        {
             ArgPrinter<JP::ARGS>::work (tjp);
        }

        advice execution (methods()) : before () 
    {
                cout << "entering: " << JoinPoint::signature() << endl;
            tjp->arg(0);
        print_args (*tjp);


        }

    advice execution("% ...::%(...)" && !"void ...::%(...)") : after() 
    {   
            JoinPoint::Result res = *tjp->result();
            cout << "leaving " << tjp->signature()<< " << returning value--" << res<<endl;
    }


};
  #endif 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!