Not an expert on preprocessor macro tricks, so if the problem here is just that I'm not familiar with some common macro idiom I'd be happy with just a term to Google. X macros are about as far as I've got before and I'm pretty sure I can't do anything with them.
Right now I do some stuff like this in code:
std::size_t trial = 0;
std::array<std::array<uint64_t, 5>, MAX_TRIALS> results_f;
void f()
{
unsigned int core;
results_f[trial][0] = __rdtscp(&core);
// do stuff
results_f[trial][1] = __rdtscp(&core);
// do some more stuff
results_f[trial][2] = __rdtscp(&core);
// do yet more stuff
results_f[trial][3] = __rdtscp(&core);
// do even more stuff
results_f[trial][4] = __rdtscp(&core);
if(++trial == MAX_TRIALS)
{
process_timestamps_f(results);
trial = 0;
}
}
(Here __rdtscp
is an x86 intrinsic that gets a tick number from the CPU.)
I would instead like to be able to write something like so:
STOPWATCH_BOILERPLATE_PRE(f);
void f()
{
STOPWATCH_BEGIN;
// do stuff
STOPWATCH_LAP;
// do some more stuff
STOPWATCH_LAP;
// do yet more stuff
STOPWATCH_LAP;
// do even more stuff
STOPWATCH_END;
};
STOPWATCH_BOILERPLATE_POST(f);
So basically, I need to be able to count the number of times that STOPWATCH_LAP
appears inside of f and use it to set the size of an array which is visible inside of f.
Bonus: it would be nice if, inside of process_timestamps_f
, I can write something like LINE_ID(f, 3)
and get the results of the __LINE__
preprocessor macro at the third instance of STOPWATCH_LAP
in f
.
(I imagine the actual code probably can't look exactly like what I wrote above. Open to whatever modifications are required to make this work. The only real requirement is that I don't have to constantly count how many of these lap points I've put into a function and update the corresponding code to match.)