3

I'm interested in being able to do something like this:

 void ISR()
 {
    MEASURE_TIME(counters)
    do_something();
    MEASURE_TIME(counters)
    do_something_else();
    MEASURE_TIME(counters)
    do_another_thing();
    MEASURE_TIME(counters)
    do_one_last_thing();
    MEASURE_TIME(counters)
 }

which would somehow translate at compile time to this:

 void ISR()
 {
    counters[0] = measure_time();
    do_something();
    counters[1] = measure_time();
    do_something_else();
    counters[2] = measure_time();
    do_another_thing();
    counters[3] = measure_time();
    do_one_last_thing();
    counters[4] = measure_time();
 }

Is there a way to do maintain and increment integer state with the preprocessor (seems unlikely) or templates?

I know I can do this:

 void ISR()
 {
    int i = 0;
    counters[i++] = measure_time();
    do_something();
    counters[i++] = measure_time();
    do_something_else();
    counters[i++] = measure_time();
    do_another_thing();
    counters[i++] = measure_time();
    do_one_last_thing();
    counters[i++] = measure_time();
 }

but there's some additional value to having a compile-time index (which is difficult to explain here w/o getting into some proprietary details)


edit: this is on an embedded system, and __COUNTER__ isn't available (I just tried it: __COUNTER__ is not defined by my compiler or the preprocessor), and I'm not sure I could get Boost to work, at least not in its entirety.

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Jason S
  • 178,603
  • 161
  • 580
  • 939

3 Answers3

4

__COUNTER__ macro is your friend.

Nim
  • 32,645
  • 2
  • 59
  • 99
2

How about counters.push_back(measure_time()) ?

  • No. I'm looking for compile-time solutions. (not to mention that STL is not available on my lowly embedded system; I'm just lucky I have C++ and templates) – Jason S Mar 01 '12 at 17:11
1

Get boost source code. See how BOOST_PP_COUNTER works (expect to get headache). Then replicate functionality in your code. Or simply use boost. Please note that BOOST_PP_COUNTER does not use __COUNTER__.

SigTerm
  • 25,518
  • 5
  • 63
  • 112