C++笔记-一些通用的帮助类/Hack/StopWatch/
Hack(访问基类私有变量/函数的访问帮助宏(快速绑定友元类)):
// #include <iostream> // using namespace std; namespace Hacker { template<typename Tag, typename Tag::MemType M> struct PrivateMemberStealer { // define friend funtion GetPrivate,return class member pointer friend typename Tag::MemType GetPrivate(Tag) { return M; } }; } #define DECL_HACK_PRIVATE_DATA(ClassName,DataType,DataName) namespace Hacker{\ struct ClassName##_##DataName\ {\ typedef DataType ClassName::*MemType;\ friend MemType GetPrivate(ClassName##_##DataName);\ };\ template struct PrivateMemberStealer<ClassName##_##DataName, &ClassName::DataName>;\ } #define GET_VAR_PRIVATE_DATA_MEMBER(ClassInstancePointer,ClassName,DataName) ClassInstancePointer->*GetPrivate(::Hacker::ClassName##_##DataName()) #define GET_REF_PRIVATE_DATA_MEMBER(RefName,ClassInstancePointer,ClassName,DataName) auto& RefName = ClassInstancePointer->*GetPrivate(::Hacker::ClassName##_##DataName()) #define DECL_HACK_PRIVATE_NOCONST_FUNCTION(ClassName,MemberName,ReturnType,...) \ namespace Hacker{\ struct ClassName##_##MemberName \ {\ typedef ReturnType (ClassName::*MemType)(__VA_ARGS__);\ friend MemType GetPrivate(ClassName##_##MemberName);\ };\ template struct PrivateMemberStealer<ClassName##_##MemberName, &ClassName::MemberName>;\ } #define DECL_HACK_PRIVATE_CONST_FUNCTION(ClassName,MemberName,ReturnType,...) \ namespace Hacker{\ struct ClassName##_##MemberName \ {\ typedef ReturnType (ClassName::*MemType)(__VA_ARGS__)const;\ friend MemType GetPrivate(ClassName##_##MemberName);\ };\ template struct PrivateMemberStealer<ClassName##_##MemberName, &ClassName::MemberName>;\ } // using ADL found to ::Hacker::Getprivate #define GET_PRIVATE_MEMBER_FUNCTION(ClassName,MemberName) GetPrivate(::Hacker::ClassName##_##MemberName()) #define CALL_MEMBER_FUNCTION(ClassPointer,MemberFuncPointer,...) (ClassPointer->*MemberFuncPointer)(__VA_ARGS__) #define WRAP_TEMPLATE_ARGS(TEMP_TYPE,...) TEMP_TYPE<__VA_ARGS__> // class A{ // public: // A(int ivalp=0):ival{ivalp}{} // private: // int func(int ival)const // { // printf("A::func(int)\t%d\n",ival); // printf("ival is %d\n",ival); // return 0; // } // int ival; // }; // DECL_HACK_PRIVATE_DATA(A,int,ival) // DECL_HACK_PRIVATE_CONST_FUNCTION(A, func, int, int) // int main() // { // A aobj(789); // // get private data member // GET_REF_PRIVATE_DATA_MEMBER(ref_ival, &aobj, A, ival); // ref_ival=456; // cout<<GET_VAR_PRIVATE_DATA_MEMBER(&aobj, A, ival)<<endl; // // // call private func // auto A_func=GET_PRIVATE_MEMBER_FUNCTION(A, func); // cout<<CALL_MEMBER_FUNCTION(&aobj, A_func, 123); // }
StopWatch(C++计时器):
#include <iostream> #include <time.h> struct StopWatch { public: void Start() { std::cout << "start time : " << std::endl; start = clock_t(); } void Stop() { clock_t total = clock_t() - start; //获所用间du std::cout << "`end time : " << (double)total / CLK_TCK << std::endl; } private: clock_t start; }; #pragma region Case /* StopWatch sw; sw.Start(); sw.Stop(); */ #pragma endregion Case
本文原创,不定时更新
可以随意转载到任何网站
~但是~ 转载也要按“基本法”
请注明原文出处和作者