473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question about function objects (a bit lengthy)

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

Jul 23 '05 #1
3 1296
Keith P. Boruff wrote:

public:

typedef void (*somefunc)(void*);

Depending on your newsreaders, you may not see that I put asterisks in the
right spots for this typedef.

Jul 23 '05 #2
"Keith P. Boruff" <kb*****@optonline.net> wrote...
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_; }
If you really want to preserve the functionality of your program, your
functor has to "forward" all calls to the original funcion _as_if_ it
is doing all the work. To achieve that, you need to store the actual
function pointer there and in the operator() call the function through
the pointer.

};
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.
You could try achieving it through a static member function.
Does anyone have any solutions for this that uses a functor?
There can be no solution that uses a functor in this case.
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.
You can do a bit better with a class template which would pack some
data that you need and have the static member your 'a' object would
call:

template<..blah..> class CallCounter {
..blah.. // static data (instead of global objects)
public:
CallCounter(which_function) { /* change static data */ }
static void* CountCalls() { call_which_function(); ++counter; }
};

....

CallCounter<whatever> cc(arguments);
A a(cc.CountCalls);

I would consider it marginally better than a proxy function with
global variables. Just packaged a bit nicer, anyway.
[..]


V
Jul 23 '05 #3
Victor Bazarov wrote:
"Keith P. Boruff" <kb*****@optonline.net> wrote...
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_; }
If you really want to preserve the functionality of your program, your
functor has to "forward" all calls to the original funcion _as_if_ it
is doing all the work. To achieve that, you need to store the actual
function pointer there and in the operator() call the function through
the pointer.


Actually Victor, my intention of using the functor strategy is strictly for
testing purposes. I wanted to use this as a "fake object" in my test code
not as an adapter to a real function. Sorry for the confusion.

};
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.
You could try achieving it through a static member function.


I think... I tried that with no results. I'll give it another shot.
Does anyone have any solutions for this that uses a functor?


There can be no solution that uses a functor in this case.


Somehow, I already knew that was the answer to this question but I thought
I'd throw it out here in case someone faced something similar.

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.


You can do a bit better with a class template which would pack some
data that you need and have the static member your 'a' object would
call:

template<..blah..> class CallCounter {
..blah.. // static data (instead of global objects)
public:
CallCounter(which_function) { /* change static data */ }
static void* CountCalls() { call_which_function(); ++counter; }
};

...

CallCounter<whatever> cc(arguments);
A a(cc.CountCalls);

I would consider it marginally better than a proxy function with
global variables. Just packaged a bit nicer, anyway.


Definitely! It's ugly the way I have it but the good thing is that it's just
confined to a test harness. I would never write this *proxy hack* in
production code.

Thanks for your help,
Keith




Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: cheeser | last post by:
Hello all, I'd like to find a clear and complete reference on the function objects / adapters found in the C++ Standard Library. This would include all supporting types that the programmer does...
4
by: Brian | last post by:
Hi all, I am implementing an object that is currently using function pointers for callbacks to let me do something on assignment: template <class T> class MyClass{ private: typedef T& (*...
0
by: user | last post by:
Hello, I'd like to know how to correctly declare a member function so that it takes as parameter any function object, of which the operator() takes arguments of specific types. The idea is to...
6
by: glen_stark | last post by:
Hi. I'm just curious if there any warnings or caveats or whatever to be aware of when inlining function object calls? If the answer is no, they inline just like everything else, that's good...
4
by: richardclay09 | last post by:
Hi Can someone please write some "compare and contrast" notes for "Template functions vs. function objects"? When to use one and not the other? For example, the TF square_f does the same thing as...
11
by: Protoman | last post by:
Which is more efficient for stuff like callbacks, selecting a funct from a list, and other stuff, function objects or function pointers? Thanks!!!
1
by: rush.william | last post by:
Hi All! Is there any way to compare 2 boost::function<objects? boost::function has no operator==() for comparing boost::function<>. Is there any hack way? I need to do the following: struct A...
9
by: 7stud | last post by:
Hi, I'm trying to figure out what this passage from GvR's tutorial means: ------- Class definitions, like function definitions (def statements) must be executed before they have any effect.......
3
pbmods
by: pbmods | last post by:
AN INTRODUCTION TO FUNCTION OBJECTS LEVEL: INTERMEDIATE PREREQS: OBJECTS You've seen it before. You're setting up an XMLHttpRequest call, and you need to execute a function when it returns, so...
7
by: Giovanni Gherdovich | last post by:
Hello, As you will see from the following code snippets, I'm trying to declare a function object in an header file and implement it in a (different) source file, but my compiler complains (he...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.