等有时间了我再bb吧,先直接上码
msvc 2015 编译执行效果
用法
style1
int CCar :: oilfeed ( int degress)
DECORATED ( logging< int > , ARGS ( __FUNCTION__) ,
DECORATED ( checkRotateSpeed< int > , ARGS ( this , __FUNCTION__) ,
{
m_n_rotat_speed += degress;
return m_n_rotat_speed;
} ) )
style2
void CCar :: brake ( int degress)
DECORATED ( CheckRotateSpeed < void > ( this , __FUNCTION__) , ,
{
m_n_rotat_speed -= degress;
return m_n_rotat_speed;
} )
完整测试代码 main.cpp
# include <QCoreApplication>
# include <functional>
# include <iostream>
# include <chrono>
class CCar {
public :
int oilfeed ( int degress) ;
void brake ( int degress) ;
int rotat_speed ( ) const { return m_n_rotat_speed; }
private :
int m_n_rotat_speed = 2000 ;
} ;
template < typename T >
T logging ( std:: function< T ( ) > func, std:: string const & funcanme)
{
auto start = std:: chrono:: high_resolution_clock:: now ( ) ;
std:: cout << "log before " << funcanme << std:: endl;
auto const & ret = func ( ) ;
auto end = std:: chrono:: high_resolution_clock:: now ( ) ;
std:: chrono:: duration< double > time_span = std:: chrono:: duration_cast < std:: chrono:: duration< double >> ( end - start) ;
std:: cout << "log after " << funcanme << ": elapsed time(s): " << time_span. count ( ) << std:: endl;
return ret;
}
template < typename T >
T checkRotateSpeed ( std:: function< T ( ) > func, CCar const * obj, std:: string const & funcanme)
{
std:: cout << "checkRotateSpeed before " << funcanme << ", rotat_speed:" << obj-> rotat_speed ( ) << std:: endl;
auto const & ret = func ( ) ;
std:: cout << "checkRotateSpeed after " << funcanme << ", rotat_speed:" << obj-> rotat_speed ( ) << std:: endl;
return ret;
}
template < bool T>
struct bool_trait { } ;
template < typename T >
class CheckRotateSpeed {
public :
CheckRotateSpeed ( CCar const * obj, std:: string const & funcanme)
: m_p_obj ( obj)
, m_str_funcanme ( funcanme)
{
}
T operator ( ) ( std:: function< T ( ) > func) {
return ( * this ) ( func, bool_trait < std:: is_same_v< T, void >> ( ) ) ;
}
protected :
inline void before ( )
{
std:: cout << "checkRotateSpeed before " << m_str_funcanme << ", rotat_speed:" << m_p_obj-> rotat_speed ( ) << std:: endl;
}
inline void after ( )
{
std:: cout << "checkRotateSpeed after " << m_str_funcanme << ", rotat_speed:" << m_p_obj-> rotat_speed ( ) << std:: endl;
}
T operator ( ) ( std:: function< T ( ) > func, bool_trait< false > ) {
before ( ) ;
auto const & ret = func ( ) ;
after ( ) ;
return ret;
}
T operator ( ) ( std:: function< T ( ) > func, bool_trait< true > ) {
before ( ) ;
func ( ) ;
after ( ) ;
}
private :
CCar const * m_p_obj;
std:: string m_str_funcanme;
} ;
# define USE_DECORATOR
# ifdef USE_DECORATOR
# define ARGS ( . . . ) \
, __VA_ARGS__
# define DECORATED ( decorator, args, codeblock) \
{ \
return decorator ( [ & ] ( ) codeblock args) ; \
}
# endif
int CCar :: oilfeed ( int degress)
DECORATED ( logging< int > , ARGS ( __FUNCTION__) ,
DECORATED ( checkRotateSpeed< int > , ARGS ( this , __FUNCTION__) ,
{
m_n_rotat_speed += degress;
return m_n_rotat_speed;
} ) )
void CCar :: brake ( int degress)
DECORATED ( CheckRotateSpeed < void > ( this , __FUNCTION__) , ,
{
m_n_rotat_speed -= degress;
return m_n_rotat_speed;
} )
# undef USE_DECORATOR
int main ( int , char * [ ] )
{
CCar car;
car. oilfeed ( 45 ) ;
car. brake ( 1000 ) ;
return 0 ;
}
各位如果觉得有用,请把 “有用” 打到评论区🤪