Reputation: 71009
I am trying to define a macros that increases along all source files of my project. Unlike the __COUNTER__
macros which is separate for all files.
I tried using a static member variable in a class something like:
class A{
static int GetNextNum() {
number++;
return number - 1;
}
private:
static int number;
};
#define NEXT_NUM A::GetNextNum()
But the result is not I want. For instance I want to have the same number for each iteration in this cycle(as would happen with __COUNTER__
):
for (int i = 0; i < 10; ++i) {
doSomething(NEXT_NUM)
}
While as I have impemented it it increases on each iteration. Any ideas on how can I achieve what I need? (I am using visual studio but would prefer a compiler independent solution).
EDIT: as requested here is a minimalistic example of something similar to what I want to do. I try to define a class that counts how many times I pass through a given section of the code. Here is how I would use such a class:
for (int i = 0; i < 10; ++i) {
ExecuteCounter(location_id);
... do stuff ...
}
There is also tons of other stuff I keep track of and accumulate in the destructor but I believe this is enough for you to get the idea. At first I used a map where entry is an object where I store the information I care of and string is formed by appending __FILE__
with __LINE__
(thus identifies the location uniquely), but this proved to be slow. So instead of using a map I decided to use a table and indices within it. This will removes a logarithm from the overall complexity and improves performance significantly. Hope this explanation makes my question clearer.
Upvotes: 0
Views: 1201
Reputation: 920
Would something as simple as this suffice?
#include <stdio.h>
int g_counter = 0; // make it visible to other files through 'extern'
#define UPDATE_COUNTER() {\
static int _this_counter_ = 0;\
if (_this_counter_ == 0) {\
g_counter++;\
_this_counter_ = 1;\
}\
}\
int main() {
printf("Counter: %d\n", g_counter);
for (int i=0; i<100; i++) {
UPDATE_COUNTER();
}
printf("Counter: %d\n", g_counter);
for (int i=0; i<100; i++) {
UPDATE_COUNTER();
}
printf("Counter: %d\n", g_counter);
}
You can make g_counter visible to other files by declaring an extern for it in a header file and including it in all your source files. The same header is also a good place for the UPDATE_COUNTER macro.
Upvotes: 1
Reputation: 78953
So I understand that you want to have a counter of how many times you visit a location in your code.
The basic idea for such a thing should be just simple, your macro first of all should expand to the declaration of a static variable that is incremented each time you visit the place.
static size_t my_count = 0;
++my_count;
would do the trick if you don't have multithreading. (If you have, look into the new atomic operations).
Now you also want the final result of your counting to be accessible somewhere. This is easiest done with another static
variable of your own collector
type, that receives the address of your counter and some application string for naming conventions as argument to the constructor.
#define DO_ACCOUNTING(NAME) \
do { \
static size_t my_count L = 0; \
static collector my_collector(&my_count, #NAME);\
++my_count;
} while(0)
The do-while
stuff is just to have these variables scoped, such that you don't pollute your name space.
Upvotes: 1