これは、 C++ マクロを使用して思いついた最高のものです。要件を満たしているかどうかはわかりません。
カウントを記録するだけの非常に基本的なバージョン。マクロは、関数への既存のすべての呼び出しをマクロの内容に置き換えます。これにより、統計が記録され、関数が呼び出されます。詳細を記録するために簡単に拡張できます。その名前の関数が 1 つしかないか、すべての関数に対して 1 つのカウントが必要であると仮定します。関数ごとにマクロが必要です。
// here's our function
void func()
{ /* some stuff */ }
// this was added
int funcCount = 0;
#define func(...) do { funcCount++; func(__VA_ARGS__); } while(0)
int main()
{
// call the function
func();
// print stats
cout << funcCount << endl;
return 0;
}
印刷し1
ます。
より一般的なバージョン。関数の呼び出し方法を変更する必要があります。
// here are our functions
void someFunc()
{ /* some stuff */ }
void someOtherFunc()
{ /* some stuff */ }
// this was added
map<string, int> funcCounts;
#define call(func, ...) do { funcCounts[ #func ]++; func(##__VA_ARGS__); } while(0)
int main()
{
// call the functions
// needed to change these from 'someFunc();' format
call(someFunc);
call(someOtherFunc);
call(someFunc);
// print stats
for (map<string, int>::iterator i = funcCounts.begin(); i != funcCounts.end(); i++)
cout << i->first << " - " << i->second << endl;
return 0;
}
版画:
someFunc - 2
someOtherFunc - 1