 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
chasdev Guest
|
Posted: Mon Oct 27, 2003 9:42 am Post subject: __func__ macro emulation |
|
|
I'm using a preprocessor/compiler (HP aCC) that doesn't support the
__func__ macro (a la gcc: expands to the name of the enclosing
function). I've thought about using an extra preprocessing step, writing
a parser, etc but this seems like way too much work. Has anyone tried
this before? Any suggestions welcome!
--
Posted via http://dbforums.com
|
|
| Back to top |
|
 |
Pavel Vozenilek Guest
|
Posted: Mon Oct 27, 2003 10:20 pm Post subject: Re: __func__ macro emulation |
|
|
chasdev <member45782 (AT) dbforums (DOT) com> wrote
| Quote: | I'm using a preprocessor/compiler (HP aCC) that doesn't support the
__func__ macro (a la gcc: expands to the name of the enclosing
function). I've thought about using an extra preprocessing step, writing
a parser, etc but this seems like way too much work. Has anyone tried
this before? Any suggestions welcome!
aCC should use __FUNCTION__. |
You may take look on file boost/current_function.hpp from Boost
distribution, it contains macro expanded properly under many
compilers.
/Pavel
|
|
| Back to top |
|
 |
Samuele Armondi Guest
|
Posted: Wed Oct 29, 2003 10:23 pm Post subject: Re: __func__ macro emulation |
|
|
chasdev wrote:
| Quote: |
I'm using a preprocessor/compiler (HP aCC) that doesn't support the
__func__ macro (a la gcc: expands to the name of the enclosing
function). I've thought about using an extra preprocessing step, writing
a parser, etc but this seems like way too much work. Has anyone tried
this before? Any suggestions welcome!
--
Posted via http://dbforums.com
|
Maybe you could manually keep track of the current function, i.e:
#include <vector>
#include <string>
#include <iostream>
class FunctionTracer
{
private:
std::vector<std::string> Trace;
int MaxSteps;
public:
FunctionTracer(int);
void EnterFunction(const std::string&);
void LeaveFunction(const std::string&);
void SetMaxSteps(int);
void Reset();
};
FunctionTracer::FunctionTracer(int n) : MaxSteps(n)
{
Trace.clear();
Trace.reserve(MaxSteps);
}
void FunctionTracer::EnterFunction(const std::string& s)
{
if (Trace.size() == MaxSteps)
Trace.clear();
Trace.push-back(s);
}
void FunctionTracer::LeaveFunction(const std::string& s)
{
if (Trace.back() == s) // the function we are leaving will be th
e last one we entered
Trace.pop_back();
else
; // error!
}
void SetMaxSteps(int n)
{std::cout << ft.CurrentFunction();
if (MaxSteps != n)
{
MaxSteps = n;
Trace.reserve(MaxSteps);
}
}
void Reset()
{
Trace.clear();
}
//test it
FunctionTracer ft(10);
void f1()
{
ft.EnterFunction("f1");
std::cout << "in function 1n";
ft.LeaveFunction("f1");
}
void f2()
{
ft.EnterFunction("f2");
std::cout << "in function2n";
ft.LeaveFunction("f2");
}
int main()
{
ft.EnterFunction("main");
f1();
std::cout << ft.CurrentFunction();
f2();
std::cout << ft.CurrentFunction();
}
This is untested code, I just wrote as it came...so don't blame me if its
broken and buggy (as it probaly will be!). I hope it helps anyway.
S. Armondi
|
|
| Back to top |
|
 |
Samuele Armondi Guest
|
Posted: Thu Oct 30, 2003 10:25 am Post subject: Re: __func__ macro emulation |
|
|
"Samuele Armondi" <armondolo (AT) yahoo (DOT) co.uk> wrote
| Quote: | chasdev wrote:
I'm using a preprocessor/compiler (HP aCC) that doesn't support the
__func__ macro (a la gcc: expands to the name of the enclosing
function). I've thought about using an extra preprocessing step, writing
a parser, etc but this seems like way too much work. Has anyone tried
this before? Any suggestions welcome!
--
Posted via http://dbforums.com
Maybe you could manually keep track of the current function, i.e:
#include <vector
#include
#include
class FunctionTracer
{
snip
std::cout << ft.CurrentFunction();
}
This is untested code, I just wrote as it came...so don't blame me if its
broken and buggy (as it probaly will be!). I hope it helps anyway.
S. Armondi
Sorry, I forgot to add the CurrentFunction() code... the code should look |
like this:
#include
#include <string>
#include <iostream>
class FunctionTracer
{
private:
std::vector<std::string> Trace;
int MaxSteps;
public:
FunctionTracer(int);
void EnterFunction(const std::string&);
void LeaveFunction(const std::string&);
void SetMaxSteps(int);
void Reset();
const std::string& CurrentFunction();
};
FunctionTracer::FunctionTracer(int n) : MaxSteps(n)
{
Trace.clear();
Trace.reserve(MaxSteps);
}
void FunctionTracer::EnterFunction(const std::string& s)
{
if (Trace.size() == MaxSteps)
Trace.clear();
Trace.push-back(s);
}
void FunctionTracer::LeaveFunction(const std::string& s)
{
if (Trace.back() == s) // the function we are leaving will
be th
e last one we entered
Trace.pop_back();
else
; // error!
}
void SetMaxSteps(int n)
{std::cout << ft.CurrentFunction();
if (MaxSteps != n)
{
MaxSteps = n;
Trace.reserve(MaxSteps);
}
}
void Reset()
{
Trace.clear();
}
const std::string& CurrentFunction()
{
return Trace.back();
}
//test it
FunctionTracer ft(10);
void f1()
{
ft.EnterFunction("f1");
std::cout << "in function 1n";
ft.LeaveFunction("f1");
}
void f2()
{
ft.EnterFunction("f2");
std::cout << "in function2n";
ft.LeaveFunction("f2");
}
int main()
{
ft.EnterFunction("main");
f1();
std::cout << ft.CurrentFunction();
f2();
std::cout << ft.CurrentFunction();
}
|
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|