Skip to main content
gamedev.net
🔒 Locked

Macro to return current function?

Started by Neosmyle Aug 10, 2002 at 5:00 PM 4 replies 750+ views
Original Post
Neosmyle
Neosmyle
I''m trying to write a little profiler class for use in my programs, as I only have VC6 standard - no built-in profiler. Anyway, when I want to use the profiling, I have to create a new instance of the class at the top of each of the functions that will be profiled. When I delete the class at the end of the function, it will save the data it has gathered (cycles since creation of the instance) and write it to a file. Anyway, the usage looks like this:
void MyFunction()
{
   #ifdef _DEBUG
   CProfile* pProfile = new CProfile(__FILE__, __LINE__, "MyFunction"); // note the "MyFunction"
   #endif

   // rest of function

   #ifdef _DEBUG
   delete pProfile;
   #endif
} 
I''ve been searching for a macro that will give the string name of the current function, so I don''t have to manually write out the string for each different usage. Also, I''ve always been confused about variable allocation to the stack/heap/etc. If I declare a pointer to a class, as I did, will it automatically be deleted at the end of the func? If not, is there a way I can declare the var so that it will be destroyed when it goes out of scope? Also, is this bad style, and I should delete it explicitly no matter what? Thanx in advance.
Null and Void
Null and Void
1) Although it''s non-standard, some compilers (notably GCC) support a __func__ or __FUNC__ macro that is equal to the current function''s name. This isn''t a fully qualified name (like ''MyNamespace::MyClass::MyFunction'') just the name of the function (''MyFunction'') with GCC''s __func__. Like I said, this is not standard, so it may work differently or not at all in a specific compiler.

2) No, pointers are not automatically freed (and it''s a good thing too ). Use auto_ptr or Boost''s smart_ptr or write something that deletes a pointer in the destructor on its own (it''s relatively easy, depending upon the functionality that you''re seeking).

Zipster
Zipster
If you have to create an instance of the profile class at the beginning of each function anyway, you can get away with just typing out the name manually for now. It''s a lot easier than any other method, unless of course your compiler supports that nifty _FUNC_ macro.
Qoy
Qoy
Also, you'd be better off declaring the variable on the stack, that way you wouldn't have to explicitly allocate it at all. Also, you can write a macro that will automatically do the __FILE__ and __LINE__ stuff for you, like this:

#define Profile(func) CProfile profile(__FILE__,__LINE__,func);

That way, you'll only have to type

Profile("MyFunction")

and the profile object will be created and destroyed automatically when it goes out of scope at the end of the function. You can also create a new profile object with the macro at the beginning of any other code block, like a loop or an if, or just any random block you create with curly braces, as each block has its own scope.

[edited by - Qoy on August 11, 2002 3:38:38 AM]
Shannon Barber
Shannon Barber
They plan on adding __FUNC__ and a couple of others to C++0x... might not want to hold your breathe waiting for that to happen though It''s non-standard today, but will be within the next 7 years...
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.