struct TimeElapsedAspect
{
void Before(int i)
{
m_lastTime = m_t.elapsed();
}
void After(int i)
{
std::cout << "time elapsed: " << m_t.elapsed() - m_lastTime << "ms" <<std::endl;
}
private:
double m_lastTime;
Timer m_t;
};
struct LoggingAspect
{
void Before(int i)
{
std::cout << "entering" << std::endl;
}
void After(int i)
{
std::cout << "leaving" << std::endl;
}
};
void foo(int a)
{
std::cout << "real HT function: " << a << std::endl;
for (int i = 0; i < 10000; i+=a )
{
if (i % 1000 == 0)
std::cout << i << std::endl;
}
}
Invoke<LoggingAspect, TimeElapsedAspect>(&foo, 1); //织入方法
std::cout << "-----------------------" << std::endl;
Invoke<TimeElapsedAspect, LoggingAspect>(&foo, 1);
Invoke的实现:http://blog.youkuaiyun.com/ozuoqi/article/details/50261353
TImer的实现:http://blog.youkuaiyun.com/ozuoqi/article/details/50261151