I'm currently trying to implement an event manager in C++. In principle it keeps a map of event types (uint64_t) to a list of boost::function<void(const IEventDataPtr&)>s.
User can register new listeners by calling EventManager::add(boost::function<void(const IEventDataPtr&)> delegate, EventType event). However, a user might want to deregister their listener later. This would involve finding this particular function object and removing it from the respective list.
I know that boost::function is generally not comparable. When playing around in the debugger I found that by comparing functor.members.func_ptr I could actually do what I was trying to do. It works as expected for lambdas, boost::bind, static member functions and regular functions. Is this safe to do? Are there any gotchas?
My expectation would be for the same object (i.e. the same lambda, function, etc. pp.) to have the same address, that is not shared with others.
Related
This question already has answers here:
What is the point of function pointers?
(18 answers)
Closed 4 years ago.
I hope its an extremely repetitive question. And my advance excuse to all the viewers who find it annoying.
Although I am bit experienced programmer, but I cannot justify the use of function pointer over direct call. Scenarios where I unable to find the differences are -
1) callbacks - same can be achieved by direct call.
2) Asynchronous or synchronous event handling - anyway event has to be identified, based on which element no. in function pointer array got updated. But the same can be also done via direct call.
3) In some post I had seen people commenting it is to be used when it is not known which function to call. I didn't get any proper justification for this.
I really appreciate if someone can explain me using above scenarios with practical and really simple realistic example.
Some more things function pointers are often used for:
Runtime polymorphism: You can define a structure that encapsulates a function pointer, or a pointer to a function table. This enables you to call the specified function at runtime, even for a type of client object that did not exist when your library was written. You can use this to implement multiple dispatch or something like the visitor design pattern in C. This is also how C++ classes and their virtual member functions were originally implemented under the hood.
Closures: These can be structures containing a function pointer and one or more of its arguments.
State Machines: Instead of a switch with a case for each state label, I’ve often found it convenient to give the handler for each state its own function. The current state is the function you’re in, the state transitions are tail-recursive calls, and the program variables are parameters. The state labels then become function pointers, which you might store in a table or return from a function.
Higher-Order Functions: Two examples from the C standard library are qsort() and btree(), which generalize the type of elements and the comparison function.
Low-Level Support: Shared-library loaders, for example, need this.
1) callbacks - same can be achieved by direct call.
Not true. For a direct call, the caller must know the function name and signature when the code is compiled, and can only ever call that one function. A callback is defined at runtime and can be changed dynamically, while the caller need only know the signature, not the name. Moreover each instance of an object may have a different callback, whereas with a direct call, all instances must call the same function.
2) Asynchronous or synchronous event handling - anyway event has to be
identified, based on which element no. in function pointer array got
updated. But the same can be also done via direct call.
Not sure what you mean, but an event handler is simply a kind of callback. The event may be identified by the caller and different call-back handlers called through pointers. Your point only stands if there is one event handler for all event types and the user is to be responsible for identification.
3) In some post I had seen people commenting it is to be used when it is not known which function to call. I didn't get any proper justification for this.
See (1) and (2) above. Often it is a means to hook platform independent third-party library code into a specific platform without having to deliver source-code or for system events that require user/application-defined handlers.
I would not sweat it however - if all your application requirements can be resolved without using a pointer to a function, then you don't need a pointer to a function. When you need one, you will probably know. You will most likely encounter it when you have to use an API that requires it before you ever implement an interface yourself that does. For example in the standard library the qsort() function requires a pointer to a function in order to define how two objects of arbitrary type are to be ordered - allowing qsort() to support any type of object - it is a way in C of making a function "polymorphic". C++ supports polymorphism directly, so there is often less need for explicit function-pointers in C++ - although internally polymorphism is implemented using function pointers in any case.
There is a concept in programming called DRY -- don't repeat yourself.
Suppose you have 121 buttons in your UI. Each one of them behaves much the same, except when you press the button, a different operation happens.
You can (A) use virtual inheritance to dispatch to the right operation (requiring a class per button), or (B) use a function pointer (or a std::function) stored in the class in order to call the right "on click" handler, or (C) have every single button be a distinct type.
A virtual function is implemented in every compiler I have examined as a complex table that, in the end, is a collection of function pointers.
So your choices are function pointers or generating 121 completely distinct buttons that happen to mostly behave the same.
In any situation where you want to decouple the caller and the called, you must use something akin to a function pointer. There are a ridiculous number of cases, from work queues to thread off tasks, callbacks, etc.
In tiny programs where everything is hard coded, hard coding every call can work. But hard coded stuff like this doesn't scale. When you want to update those 121 buttons each hand-implemented, knowing their points of customization is going to be ridiculously difficult. And they will fall out of sync.
And 121 is a modest number of buttons. What about an app with 10,000? And you want to update every button's behavior to handle touch-based input?
Even more, when you type erase, you can reduce binary size significantly. 121 copies of a class implementing a button is going to take more executable space than 1 class, each of which stores a function pointer or two.
Function pointers are but one type of "type erasure". Type erasure reduces binary size, provides clearer contracts between provider and consumer, and makes it easier to refactor behavior around the type erased data.
Without function pointers, how would you implement a function which calculates the integral of any real-valued function?
typedef double (*Function)(double);
double Integral(Function f, double a, double b);
1) callbacks - same can be achieved by direct call.
Not in all cases, since the caller may not know at compile-time what function must be called. For instance, this is typical in libraries since they cannot know in advance your code.
However, it can also happen in your own code: whenever you want to re-use partially a function, you can either:
Create several versions of that function, each calling a different function. Duplicates code, very bad maintenance. Good performance unless hit by code bloat.
Pass a function pointer (or callable in general in C++). Flexible, less code, performance might suffer in some cases.
Create a set of branches (if/switch chain), if you know in advance the set of possible functions to call. Rigid, but might be faster than a function pointer for small number of branches.
In C++, create a templated version. Same as the first case, but automated; so good maintenance. Code bloat might be an issue.
Factor out the common code so that callers can call whatever they need piece by piece. Sometimes this isn't possible/easy -- specially when parametrizing complex algorithms that you want to keep reusable (e.g. qsort()). In C++, see the STL (Standard Template Library).
2) Asynchronous or synchronous event handling - anyway event has to be identified, based on which element no. in function pointer array got updated. But the same can be also done via direct call.
Some event systems are designed so that you simply configure which function(s) will be triggered when a given event happens. If this is an external library with a C interface, they have no choice but to use function pointers.
Some other systems let you create your own event loop and you fetch the events somehow and do whatever you want with them; so they avoid callbacks.
3) In some post I had seen people commenting it is to be used when it is not known which function to call. I didn't get any proper justification for this.
See the first case.
Thanks all for actively participating in this discussion. Thanks for giving practical examples like -
1) Implement Library function
2) Look qsort
3) Refer Linux Kernel
4) Generic Heap data structure in C
I feel qsort() void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)) s is quite sufficient to clear my 1) & 3) point.
1) callbacks - same can be achieved by direct call.
3) In some post I had seen people commenting it is to be used when it is not known which function to call. I didn't get any proper justification for this.
Mainly by callbacks - it is a provision of calling a function for which the body is not yet defined. And it expected that the definition of the function will be provided later during run-time. So, compilation won't be hindered due to lack of function definition. Practical use if someone consider above qsort() function. In this the user is responsible for providing the function definition for compare() like -
int compare (int* a, int* b)
{
//User defined body based on problem requirement
}
Lets consider a practical scenario where multiple threads have their respective compare function. In case of direct call every thread need to implement their own sorting function or if a common function then implementation would be much more bulky. But by using the callback method all threads can use same function for sorting, since the sorting algo remain same for all threads.
Considering a layered architecture mainly higher layers have an abstract view of lower layer. So, here if say we have qsort() function [User defined qsort] implemented at application layer and lets say underlying application there is a ADC driver layer which capture sample and provide to application for sorting. Then for application it is not necessary to understand the definition of function responsible for collecting and providing the samples. But application will only focus on obtaining the sample. Hence, that main application won't know which function to call. Respective ADC driver will simply make a call to application using the qsort() and provide needful data.
Regarding 2 point still confused -
2) Asynchronous or synchronous event handling - anyway event has to be identified, based on which element no. in function pointer array got updated. But the same can be also done via direct call.
From above discussion I conclude that if event handlers pointed to some library function, then it need to be implemented via pointer to function. And secondly to create an independent and handy code it is necessary to maintain function pointer. Lets say between application and driver we have an interfacing layer. So, if either application or driver changes anytime it won't affect or very least affect each other. And this interface layer is implemented using pointer to function. But consider below scenario -
int (*fptr[10]) (void) =
{
function1; //function for starting LED
function2; //function for relay operation
.
.
function10; //function for motor control
}
lets say we have GPIO0.0 - GPIO0.10 has been mapped to the function pointer array. i.e. GPIO0.0 - 0th element of fptr
.
.
GPIO0.10 - 10th element of fptr
These GPIO pins has been configured for level triggered interrupt and their respective ISR will update the array element no. i=GPIO_Value; further the scheduler have an thread which will call the function pointer array -
fptr[i]();
Does the use of function pointer is justifiable here??
I'm attempting to write a an observer pattern in c++
so I have a map that contains eventname -> vector of callback functions
The callback functions are stored in a vector as
std::function<void(void *)>
so the map looks like
std::unordered_map<std::string, std::vector<std::function<void(void *)>>>
I can add listeners to the vector and receive and respond to event notifications. My problem is with implementing detach.
So std::function's can't be compared, so erase/remove is out, so I wanted to search the vector and compare manually.
I found that this question had success using std::function::target with getting access to underlying pointers, but I can't use this, since I'm using std::bind to initialize the callback:
std::function<void(void *)> fnCallback = std::bind(&wdog::onBark, this, std::placeholders::_1)
I just want to compare the underlying member function ptrs, or even comparison with the underlying object ptr to which the member function is associated. Is there any way?
I'd like to avoid wrapping the member fn ptr in an object that contains a hash, although it looks like I might have to go that way...
When I do this, I return a token to the listening code.
My usual pattern is to have a weak_ptr<function<sig>> in the broadcaster, and the token is a shared_ptr<void> (to the stored weak ptr).
When broadcasting, I filter out dead targets, then broadcast. Targets deregister by simply clearing, destroying or otherwise discarding their token. A vector of tokens in their instance is a reasonable way if they want to never deregister.
If you never broadcast this can lead to old dead resources hanging around needlessly, so in a public framework I might want something with bettter guarantees. But it is lightweight easy and fast otherwise.
I just started learning about design patterns, and I'm having trouble with some should-be-simple concepts. The concepts of some of these patterns make sense, but I'm struggling with how I should implement them in C++.
Let's saying I'm working on a problem that implements an observer problem. Let's assume there is only a single observer. That leaves two objects that need to communicate: the subject and the observer.
Conceptually, what this pattern is attempting to do is very simple to understand. However, I'm getting bogged down by questions like: where do they objects live? Must they both live within some container? How do they actually make requests from one another?
Regarding that last question, is it necessary for each object to a have a data member that references the other object? As in, must the subject contain a pointer to the observer, and must the observer contain a pointer to the subject?
So stepping away from the observer. If I have any two objects that are dependent to each other (uni-directionally or bi-directionally), is it necessary that each object have a pointer to its respective object?
Thank you,
A typical high-level run-time polymorphic implementation of the observer pattern has the observable object add a data member such as std::vector<Observer*> observers_;, and when something of interest happens the observable's member function iterates over that observers_ calling some function through the Observer*s. There's not necessarily any need for the Observers to keep pointers/references to the observable object, but if it's useful they may do so, or the callbacks from the observable object might pass the this pointer or a reference to *this as a parameter. Simpler observables might only support one Observer* instead of a container thereof.
For lower-level / performance-critical code - when it's practical to do so because you know the types involved at compile time - you may prefer to stipulate one or more observers at compile time - perhaps as template arguments. That can allow the dispatch to be inlined and optimised, dead-code elimination to avoid calls to observers that do nothing etc..
where do they objects live?
Anywhere that makes sense for the object's general function in the program. For example, if a Database_Server_Connection was observable, it might let other parts of the program register interest in knowing when the connection's established asynchronously, when the connection's dropped, when async requests complete, when the database connection is closed by program code, when the database observable object's destructor runs. The observers could be anywhere else in the program - they might be local objects in some function's scope, possibly in another thread, or they might be in or managed by smart pointers in a static or dynamically allocated container.
Must they both live within some container?
Nope - as above.
How do they actually make requests from one another?
Firstly, the code adding observers needs access to the observable object, and normally calls something like observable.add_observer(this); to register themselves, taking care to call observable.remove_observer(this); in their destructor so the observable never accidentally attempts a call into an already "destructed" object. The callbacks then happen using the pointers stashed by add_observer. As above, the callbacks may be passed pointers or references to the observable as arguments, the observers might have stashed away a pointer or reference earlier, or they might not even need one if all the information they need is passed to the callback and they don't need to mutate (call a non-const function on) the observable.
So stepping away from the observer. If I have any two objects that are dependent to each other (uni-directionally or bi-directionally), is it necessary that each object have a pointer to its respective object?
It's often easiest, but sometimes some other communications mechanism may be used such as a queue or socket, in which case the communicating parties need some manner of pointer/reference/handle/id for that communications mechanism rather than pointers to each other.
Another method you can use to make objects communicate is through an intermediate Queue object, or a socket, or other type of shared memory, so storing a pointer to the other object is not always necessary. In fact, to improve decoupling and write general code it is often better to use an event Queue or a Signal (see design of QT Libraries).
Don't take that as meaning that storing a pointer is wrong: it is often a good solution and avoid over-engineering which is expensive (in terms of money, time, and other computing resources).
I have a fairly generic work queue. It takes some number of std::function<void(void)> and executes them on N worker threads. Closures + type erasure working well.
Now however, I would like the functions to "return" some state, and I would prefer the queue know nothing about what the types might be. To that end, I am considering using a queue of
std::function <std::function<void(void)>(void)>
Which, if I can keep the syntax straight, is a function taking void and returning a function taking void and returning void. The idea being to add a second work queue to return the result of the task execution.
This doesn't strictly solve the problem though - I can then call the returned value, but this doesn't obviously allow me to retrieve the state.
I could return boost::any, which doesn't appear to tell me what the contained type is, or boost::variant which means providing the task library with a list of all possible return types. Neither seem ideal.
What I would like to do is encode the information required to interpret the result of the function call within the functor, but I do not see a clean way to achieve this. Bundling an execute() and an extract() method into a single void(void) functor is beyond my cunning.
An alternative workaround is a heterogenous work queue, but such a thing is also quite a pain to write in C++. I feel optimistic that there is an idiomatic solution for getting at the unknown type that results from executing type erased code, but guessing the search keywords has not gone well for me. Guidance would be appreciated.
edit: An outline of the intended workflow for the application code, as distinct from the work queue / thread pool layer
Construct one or more tasks to execute asynchronously
Package the tasks as std::function</*consistent type*/>
Push onto queue provided by a library
Do some other things for a while
Retrieve an opaque type of some sort from the queue
Pass this opaque type to a function that works out what it actually is
All is well from here
edit: As suggested in the comments, type erasure goes both ways.
Let the generic functor be:
struct functor
{
typedef std::function<void(void)> functype;
functype async;
functype result;
};
Then use an instance of queue<functor> for both send and receive. Async gets run on a remote thread. When an instance of functor comes back we have no idea what it represents, but the result() member does and can perform whatever next step is considered reasonable. This will probably suffice.
(Copying from my comment):
Instead of having step 6 know the precise concrete type, what about solving your problem by using either dynamic or static polymorphism instead?
The question is strictly about std::function and not boost::function. See the Update section at the bottom of this question for more details, especially the part about it not being possible to compare non-empty std::function objects per the C++11 standard.
The C++11 std::function class template is great for maintaining a collection of callbacks. One can store them in a vector, for example and invoke them when need be. However, maintaining these objects and allowing for unregistration seems to be impossible.
Let me be specific, imagine this class:
class Invoker
{
public:
void Register(std::function<void()> f);
void Unregister(std::function<void()> f);
void InvokeAll();
private:
// Some container that holds the function objects passed to Register()
};
Sample usage scenario:
void foo()
{
}
int main()
{
std::function<void()> f1{foo};
std::function<void()> f2{[] {std::cout << "Hello\n";} };
Invoker inv;
// The easy part
// Register callbacks
inv.Register(f1);
inv.Register(f2);
// Invoke them
inv.InvokeAll();
// The seemingly impossible part. How can Unregister() be implemented to actually
// locate the correct object to unregister (i.e., remove from its container)?
inv.Unregister(f2);
inv.Unregister(f1);
}
It is fairly clear how the Register() function can be implemented. However, how would one go about implementing Unregister(). Let's say that the container that holds the function objects is vector<std::function<void()>> . How would you find a particular function object that is passed to the Unregister() call? std::function does supply an overloaded operator==, but that only tests for an empty function object (i.e., it cannot be used to compare two non-empty function objects to see if they both refer to the same actual invocation).
I would appreciate any ideas.
Update:
Ideas so far mainly consist of the addition of a cookie to be associated with each std::function object that can be used to unregister it. I was hoping for something that is not exogenous to the std::function object itself. Also, there seems to be much confusion between std::function and boost::function. The question is strictly about std::function objects, and not boost::function objects.
Also, you cannot compare two non-empty std::function objects for equality. They will always compare non-equal per the standard. So, links in the comments to solutions that do just that (and use boost::function objects to boot) are patently wrong in the context of this question.
Since you can't test for element identity in the container, it's probably best to use a container (such as std::list) whose iterators do not invalidate when the container is modified, and return iterators back to registering callers that can be used to unregister.
If you really want to use vector (or deque), you could return the integral index into the vector/deque when the callback is added. This strategy would naturally require you to make sure indexes are usable in this fashion to identify the function's position in the sequence. If callbacks and/or unregistration is rare, this could simply mean not reusing spots. Or, you could implement a free list to reuse empty slots. Or, only reclaim empty slots from the ends of the sequence and maintain a base index offset that is increased when slots are reclaimed off the beginning.
If your callback access pattern doesn't require random access traversal, storing the callbacks in a std::list and using raw iterators to unregister seems simplest to me.
I have an idea for this.
Store the callbacks as std::weak_ptr<std::function<void(argtype1, argtype1)>>. Then the caller is responsible for keeping the corresponding std::shared_ptr alive, and all the caller has to do to unregister the callback is destroy all active std::shared_ptrs to the callback function.
When invoking callbacks, the code has to be careful to check for lock failures on the std::weak_ptr<>s it is using. When it runs across these it can remove them from its container of registered callbacks.
Note that this does not give complete thread safety, as the callback invoker can lock the std::weak_ptr and make a temporarily newly active std::shared_ptr of the callback function that can stay alive after the caller's std::shared_ptr goes out of scope.