K
Keith P. Boruff
Hello all,
Currently, in my work, I'm dealing with some old and sloppy C++ code
(written before '93).
I'm trying to put test harnesses around the stuff I'm working on (using
Boost testsuite lib FYI) to minimize breakage.
I came across a class something like this:
class A
{
public:
typedef void (*somefunc)(void*);
A( somefunc aFunc) : pFunc_(aFunc) {}
private:
somefunc pFunc_;
};
For my tests, I want to get some feedback on how an object of the class uses
the pointer to the function. For example, how many times is this function
called within the lifetime of a class? In theory, a functor would work
nicely:
class MyFunctor
{
int callCount_;
public:
MyFunctor() : callCount_(0): {}
int timesCalled() const { return callCount_; }
void operator () (void* ) { ++callCount_; }
};
However, I can't do this:
#include <iostream>
int main()
{
MyFunctor myFunc;
A a(&myFunc);
// Do stuff with A
std::cout << "number of times function called: "
<< myFunc.timesCalled() << std::endl;
return 0;
}
Of course, the code in main() won't work but this is something I'd like to
achieve.
Does anyone have any solutions for this that uses a functor? I know I can
just create a *someFunc* function, pack it with some global variables and
read them (which is what I've done for the moment) but I was hoping a
solution similar to the above would be possible.
Please don't say "change the interface of the tested class". At this time,
that's not possible (though I'd like to).
If I haven't been clear on anything here, just ask.
Thanks,
Keith
Currently, in my work, I'm dealing with some old and sloppy C++ code
(written before '93).
I'm trying to put test harnesses around the stuff I'm working on (using
Boost testsuite lib FYI) to minimize breakage.
I came across a class something like this:
class A
{
public:
typedef void (*somefunc)(void*);
A( somefunc aFunc) : pFunc_(aFunc) {}
private:
somefunc pFunc_;
};
For my tests, I want to get some feedback on how an object of the class uses
the pointer to the function. For example, how many times is this function
called within the lifetime of a class? In theory, a functor would work
nicely:
class MyFunctor
{
int callCount_;
public:
MyFunctor() : callCount_(0): {}
int timesCalled() const { return callCount_; }
void operator () (void* ) { ++callCount_; }
};
However, I can't do this:
#include <iostream>
int main()
{
MyFunctor myFunc;
A a(&myFunc);
// Do stuff with A
std::cout << "number of times function called: "
<< myFunc.timesCalled() << std::endl;
return 0;
}
Of course, the code in main() won't work but this is something I'd like to
achieve.
Does anyone have any solutions for this that uses a functor? I know I can
just create a *someFunc* function, pack it with some global variables and
read them (which is what I've done for the moment) but I was hoping a
solution similar to the above would be possible.
Please don't say "change the interface of the tested class". At this time,
that's not possible (though I'd like to).
If I haven't been clear on anything here, just ask.
Thanks,
Keith