I'm trying to avoid using global variables in an openCV project (I'll have my supervisor no doubt teach me as to why they are bad rah rah rah :) - but at the moment they seem to be the only way I can get information out of mouse and trackbar callback functions.
Using the mouse example - at the moment I have as globals:
vector<Point2d> vectorOfPoints;
int clickCount = 0;
At the moment I have this line in main:
setMouseCallback("test",onMouse, NULL);
Then above main:
void onMouse(int event, int x, int y, int f, void* ){
if(event == CV_EVENT_LBUTTONDOWN){
vectorOfPoints.push_back(Point(x,y));
clickCount++;
}
}
It is working, but what is the syntax to get read/write access to both vectorOfPoints and clickCount inside the callback function without using globals?
I have found this question online a few times but the answers are unclear to me or wont work. There are hints within comments as how to do it, but I am so far unable to interpret the jargon correctly.
I was hoping for something as simple as the syntax I use to pass variables as references to methods ...
void referenceExampleMethod(vector<Point2d>& referenceExample){
//do something with referenceExample...
}
...less convoluted the better
I'm scared to ask (jargon overload!) but maybe it's 100% relevant - what is void* ??
Any help appreciated
I agree with the first part of the #jschultz410's answer about the pointer to some place in memory. However, I disagree with using raw pointers in the wild.
You should define your own data type, holding all your data, it could be struct or class, or std::pair, or std::tuple, whatever, the choice is yours.
Then you create an object of that type and use its address in the last argument for setMouseCallback.
The main thing you must ensure - the life time of that object must cover the life time of the window. That is, the object must be created before the first call to onMouse and destroyed after the last one. You could do this by declaring the variable in the beginning of your main. Then the object will be created early after the program start and destroyed near its finish automatically by the compiler. Here is the example.
typedef std::pair<vector<Point2d>, int> data_holder_type; // note the absence of references, this pair holds std::vector and int
void onMouse(int event, int x, int y, int f, void* ptr){
if(event == CV_EVENT_LBUTTONDOWN){
data_holder_type *dholder = static_cast<data_holder_type *>(ptr);
dholder->first.push_back(Point(x,y));
dholder->second.clickCount++;
}
}
....
int main(void) {
data_holder_type dholder;
// add code to initialize your dholder;
...
setMouseCallback("test", onMouse, &dholder);
...
cv::waitKey(); // wait until the window is closed
// read values from dholder and process them
} //dhloder is deleted somewhere here
Another important thing is the concurrent access to this object. onMouse is called from the separate parallel thread, and if your dholder is read or modified both in main, and onMouse simultaneously while the window is open, race conditions will occur. In general, they usually cause unpredictable and very hard to catch bugs.
Everything is fine while your main doesn't access dholder until the window is closed.
As for your question about void *. Note the lines inside if in onMouse. ptr points to the object of type void. That object has no any members, first, or second, or any others. You'll get the compiler error if you'll try to access them using ptr (e.g. ptr->first). Therefore you have to cast this pointer to pointer to another type, that contains some info about the object it points to, data_holder_type * in this case.
Any pointer type can be cast to void *, and void * can be cast to any other pointer type. This allows you to have several different callbacks for different windows.
Beware of wrong casts! No checks is done by compiler.
This example shows how to set 3 different mouse callbacks for windows with different titles.
typedef blah-blah-blah1 data_holder_type1;
typedef blah-blah-blah2 data_holder_type2;
typedef blah-blah-blah3 data_holder_type3;
void onMouse1(int event, int x, int y, int f, void* ptr){
if(event == CV_EVENT_LBUTTONDOWN){
data_holder_type1 *dholder = static_cast<data_holder_type1 *>(ptr);
dholder->first.push_back(Point(x,y));
dholder->second.clickCount++;
}
}
void onMouse2(int event, int x, int y, int f, void* ptr){
if(event == CV_EVENT_LBUTTONDOWN){
data_holder_type2 *dholder = static_cast<data_holder_type2 *>(ptr);
// processing, related to another data type
}
}
void onMouse3(int event, int x, int y, int f, void* ptr){
if(event == CV_EVENT_LBUTTONDOWN){
data_holder_type3 *dholder = static_cast<data_holder_type3 *>(ptr);
// process
}
}
int main(void) {
data_holder_type1 dholder1;
data_holder_type2 dholder2;
data_holder_type3 dholder3;
// add code to initialize your dholders;
...
setMouseCallback("test1", onMouse1, &dholder1);
setMouseCallback("test2", onMouse2, &dholder2);
setMouseCallback("test3", onMouse3, &dholder3);
...
}
The last parameter to setMouseCallback is passed back to you when onMouse is called. That is what the void * is in onMouse: the pointer that you passed to setMouseCallback. A void * is a pointer to an unspecified type. You can think of it as a generic pointer to some place in memory.
In your case, you would likely pass the address of a struct that either contains or points to the variables that you want to have access to inside onMouse.
#include <utility>
...
typedef std::pair<std::vector<Point2d>&, int&> my_pair;
...
my_pair *p = new my_pair(vectorOfPoints, clickCount); // TODO: needs to be deallocated eventually
setMouseCallback("test", onMouse, p);
The above assumes that vectorOfPoints and clickCount are already allocated somewhere else (e.g. - dynamically, statically, on the main thread's stack, etc.) and will remain so for the duration of your callbacks. Then,
void onMouse(int event, int x, int y, int f, void *pptr)
{
my_pair *p = (my_pair*) pptr;
if(event == CV_EVENT_LBUTTONDOWN){
p->first.push_back(Point(x,y));
p->second++;
}
}
Related
I'm using a C library where the user is supposed to register a int (*functionPtr)(int,int); callback.
Then, some file is processed, and during the file processing the user receives callback with the previously given function, up to thousands for a single file processing.
My question is, how to manage state for this kind of worflow?
For instance I'd like to count how many times the callback was called. The only way I'm thinking of doing this is :
int c = 0; //global variable
int callback(int i, int j) {
++c;
}
Which is basically not very pretty and forces me to manage global variables.
Is there a way to do this effeciently in C++ ? In my case I do not have access to C++11
The standard way is to support a context pointer:
void myAPI(void (*f)(void *, int, int), void *context) {
...
// Call the callback passing the provided context
f(context, x, y);
...
}
The users can then recover custom data from the context if needed... for example
void inc_counter(void *context, int x, int y) {
*((int *)context) += 1;
}
void foo() {
int count = 0;
myAPI(inc_counter, &count);
}
In other words the idea is to add an opaque void * parameter to the callback interface and also a void * value to pass in that parameter when invoking the function.
This doesn't add any coupling but works around the problem that C has no closures.
I'm making a game with SDL that used libconfig to read some settings from a file. The problem is that I made a class called ClipList that contains a std::vector<SDL_Rect> to store the settings but when trying to add SDL_Rect objects to the vector, for some reason push_back does nothing and I end up with an empty vector.
This is the class:
class ClipList
{
public:
ClipList();
ClipList(int);
virtual ~ClipList();
void addClip(int,int,int,int);
void getClip(int,SDL_Rect*);
int getLength();
protected:
private:
std::vector<SDL_Rect> clips;
};
ClipList::ClipList(int l)
{
clips.reserve(l);
}
void ClipList::addClip(int x,int y,int w,int h){
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
clips.push_back(rect);
}
void ClipList::getClip(int i,SDL_Rect* rect){
rect = &(clips.at(i));
}
int ClipList::getLength(){
return clips.size();
}
And this is the function where I initialize the ClipList object. This function gets called from main.
void set_clips(Config* placlips,ClipList* clips, ClipList* flipclips){
const Setting& root = placlips->getRoot();
int x,y,w,h;
try{
Setting& clipsett = root["clips"];
int cliplen = clipsett.getLength();
clips = new ClipList(cliplen);
flipclips = new ClipList(cliplen);
for(int i=0;i<cliplen;i++){
const Setting& c = clipsett[i];
if(!(c.lookupValue("x",x)&&c.lookupValue("y",y)&&c.lookupValue("w",w)&&c.lookupValue("h",h))){
continue;
}
clips->addClip(x,y,w,h);
}
}catch(const SettingNotFoundException &nfex){
cerr << "Setting not found at" << nfex.getPath() << endl;
}
}
Regardless of whether the ClipList objects get initialized in main or set_clips, clips.push_back(rect) doesn't work. The capacity of the vector changes but no object gets stored so I end up with a segfault if I try to do anything else with the vector, even checking if the vector is empty or not.
I am going to guess, the signature of the function
void set_clips(Config* placlips,ClipList* clips, ClipList* flipclips);
is the culprit. You are allocating memory for clips and flipclips in this function but since the pointers are passed by value, the calling function does not see the allocated memory.
If you change the function signature to:
void set_clips(Config* placlips, ClipList*& clips, ClipList*& flipclips);
your problems should go away.
clips.push_back(rect) is working fine. Your set_clips function allocates new ClipList instances but does not pass those pointers back to the caller. The caller is probably attempting to use a garbage pointer as an initialise instance and that is why you are getting a segfault.
You need to pass the created objects back. You should use something like std::shared_ptr<> to do that instead of bare pointers.
Update on how to do this without using std::shared_ptr<>:
You need to keep track of ownership and deal with exceptions. In terms of the actual passing, the rule I use (originally from Lakos in "Large Scale C++ Software Design") is that parameters that are return values (as you are attempting to use them) are pointers, and read-only parameters are by value or const-reference. Return values come first.
So, your set_clips function should look like this:
void set_clips(ClipList** clips, ClipList** flip_clips, Config const& placlips)
When you call set_clips you pass a pointer to each pointer that will receive the allocated value, and pass a const-reference to the placlips object that is not modified by the function.
You would all it something like this:
ClipList* clips = 0;
ClipList* flip_clips = 0;
set_clips(&clips, &flip_flips, placlips);
// ... then do whatever comes next.
But combining those rules with std::shared_ptr<> or boost::shared_ptr<> is better and the "modern C++" style.
To overcome the impossibility of giving C library a callback to C++ member function, wanted to implement something like this:
SomeObject* findSomeObjectByHandlePointer(datahandle *dh) { }..
by using a map, which contains addresses of *datahandle as an index, and addresses of *SomeObject's as value.
When SomeObject is created, it produces a group of datahandle's, which are unique for the object. Then, it passes a pointer to *datahandle and static callback function to C library, then C library calls back and returns a pointer to datahandle, that in turn can be associated back with a SomeObject.
Which types can you recommend for storing pointer values in a map besides safe but slow <string, SomeObject*>?
This answer tells me to avoid using auto_ptr too.
Normally, C-like callbacks take a void* user_data parameter, which allows you to pass in anything you want:
void c_func(void (*fptr)(void*), void* user_data){
// do some stuff
fptr(user_data);
}
Now, simply make the following static member function:
class A{
public:
static void c_callback(void* my_data){
A* my_this = static_cast<A*>(my_data);
// do stuff with my_this
}
};
Edit: According to #Martin's comment, you may get unlucky with a static member function. Better use an extern "C" function:
extern "C" void c_callback(void* my_data){
// same as static method
}
And pass that + your A instance to that c_func:
int main(){
A a;
c_func(&A::c_callback,&a);
}
Or if that A instance needs to outlive the current scope, you need to somehow save the heap-allocated pointer somewhere and delete it manually later on. A shared_ptr or the likes won't work here, sadly. :(
On your problem of storing pointer in a map, that's not a problem at all, see this little example on Ideone.
I think this will suffice. It is what we use:
class datahandle;
class SomeObject;
typedef std::map<datahandle*, SomeObject*> pointer_map;
pointer_map my_map;
SomeObject* findSomeObjectByHandlePointer( datahandle *dh) {
pointer_map::const_iterator ff = my_map.find(dh);
if (ff != my_map.end()) {
return ii->second;
}
return NULL;
}
Often callback functions have an extra parameter of type void* which you can use to pass in any additional data you might need. So if you want to use a member function as your callback, you pass in a pointer to the object casted to void* and then cast it back and call the member function in your callback function.
If you have many reads and less writes, you could use vector as a set. It is very common, because lower_bound is more effective than map and use less space from memory:
typedef std::pair<std::string,Your_pointer> your_type;
bool your_less_function( const your_type &a, const your_type &b )
{
// your less function
return ( a < b );
}
...
std::vector<your_type> ordered-vector;
When you add values:
...
ordered-vector.push_back(value)
...
// Finally. The vector must be sorted before read.
std::sort( ordered-vector.begin(), ordered-vector.end(), your_less_function );
When ask for data:
std::vector<your_type>::iterator iter = std::lower_bound( ordered-vector.begin(), ordered-vector.end(), value, your_less_function );
if ( ( iter == ordered-vector.end() ) || your_less_function( *iter, value ) )
// you did not find the value
else
// iter contains the value
I would like to do something like:
for(int i=0;i<10;i++)
addresses[i] = & function(){ callSomeFunction(i) };
Basically, having an array of addresses of functions with behaviours related to a list of numbers.
If it's possible with external classes like Boost.Lambda is ok.
Edit: after some discussion I've come to conclusion that I wasn't explicit enough. Please read Creating function pointers to functions created at runtime
What I really really want to do in the end is:
class X
{
void action();
}
X* objects;
for(int i=0;i<0xFFFF;i++)
addresses[i] = & function(){ objects[i]->action() };
void someFunctionUnknownAtCompileTime()
{
}
void anotherFunctionUnknowAtCompileTime()
{
}
patch someFunctionUnknownAtCompileTime() with assembly to jump to function at addresses[0]
patch anotherFunctionUnknownAtCompileTime() with assembly to jump to function at addresses[1]
sth, I don't think your method will work because of them not being real functions but my bad in not explaining exactly what I want to do.
If I understand you correctly, you're trying to fill a buffer with machine code generated at runtime and get a function pointer to that code so that you can call it.
It is possible, but challenging. You can use reinterpret_cast<> to turn a data pointer into a function pointer, but you'll need to make sure that the memory you allocated for your buffer is flagged as executable by the operating system. That will involve a system call (LocalAlloc() on Windows iirc, can't remember on Unix) rather than a "plain vanilla" malloc/new call.
Assuming you've got an executable block of memory, you'll have to make sure that your machine code respects the calling convention indicated by the function pointer you create. That means pushing/popping the appropriate registers at the beginning of the function, etc.
But, once you've done that, you should be able to use your function pointer just like any other function.
It might be worth looking at an open source JVM (or Mono) to see how they do it. This is the essence of JIT compilation.
Here is an example I just hacked together:
int func1( int op )
{
printf( "func1 %d\n", op );
return 0;
}
int func2( int op )
{
printf( "func2 %d\n", op );
return 0;
}
typedef int (*fp)(int);
int main( int argc, char* argv[] )
{
fp funcs[2] = { func1, func2 };
int i;
for ( i = 0; i < 2; i++ )
{
(*funcs[i])(i);
}
}
The easiest way should be to create a bunch of boost::function objects:
#include <boost/bind.hpp>
#include <boost/function.hpp>
// ...
std::vector< boost::function<void ()> > functors;
for (int i=0; i<10; i++)
functors.push_back(boost::bind(callSomeFunction, i));
// call one of them:
functors[3]();
Note that the elements of the vector are not "real functions" but objects with an overloaded operator(). Usually this shouldn't be a disadvantage and actually be easier to handle than real function pointers.
You can do that simply by defining those functions by some arbitrary names in the global scope beforehand.
This is basically what is said above but modifying your code would look something like this:
std::vector<int (*) (int)> addresses;
for(int i=0;i<10;i++) {
addresses[i] = &myFunction;
}
I'm not horribly clear by what you mean when you say functions created at run time... I don't think you can create a function at run time, but you can assign what function pointers are put into your array/vector at run time. Keep in mind using this method all of your functions need to have the same signature (same return type and parameters).
You can't invoke a member function by itself without the this pointer. All instances of a class have the function stored in one location in memory. When you call p->Function() the value of p is stored somewhere (can't remember if its a register or stack) and that value is used as base offset to calculate locations of the member variables.
So this means you have to store the function pointer and the pointer to the object if you want to invoke a function on it. The general form for this would be something like this:
class MyClass {
void DoStuf();
};
//on the left hand side is a declaration of a member function in the class MyClass taking no parameters and returning void.
//on the right hand side we initialize the function pointer to DoStuff
void (MyClass::*pVoid)() = &MyClass::DoStuff;
MyClass* pMyClass = new MyClass();
//Here we have a pointer to MyClass and we call a function pointed to by pVoid.
pMyClass->pVoid();
As i understand the question, you are trying to create functions at runtime (just as we can do in Ruby). If that is the intention, i'm afraid that it is not possible in compiled languages like C++.
Note: If my understanding of question is not correct, please do not downvote :)
Think of your basic GLUT programs. They simply run from a main method and contain callbacks like `glutMouseFunc(MouseButton) where MouseButton is the name of a method.
What I have done is I have encapsulated the main file into a class, so that MouseButton is no longer a static function but has an instance. But doing this gives me a compilation error :
Error 2 error C3867: 'StartHand::MouseButton': function call missing argument list; use '&StartHand::MouseButton' to create a pointer to member c:\users\angeleyes\documents\visual studio 2008\projects\capstone ver 4\starthand.cpp 388 IK Engine
It is not possible to provide a code sample as the class is quite huge.
I have tried using this->MouseButton but that gives the same error. Can't a pointer to an instance function be given for callback?
As the error message says, you must use &StartHand::MouseButton syntax to get a pointer to a member function (ptmf); this is simply mandated as part of the language.
When using a ptmf, the function you are calling, glutMouseFunc in this case, must also expect to get a ptmf as a callback, otherwise using your non-static MouseButton won't work. Instead, a common technique is for callbacks to work with a user-supplied void* context, which can be the instance pointer—but the library doing the callbacks must explicitly allow this parameter. It's also important to make sure you match the ABI expected by the external library (the handle_mouse function below).
Since glut doesn't allow user-supplied context, you have to use another mechanism: associate your objects with glut's current window. It does provide a way to get the "current window", however, and I've used this to associate a void* with the window. Then you simply need to create a trampoline to do the type conversion and call the method.
Machinery:
#include <map>
int glutGetWindow() { return 0; } // make this example compile and run ##E##
typedef std::pair<void*, void (*)(void*,int,int,int,int)> MouseCallback;
typedef std::map<int, MouseCallback> MouseCallbacks;
MouseCallbacks mouse_callbacks;
extern "C" void handle_mouse(int button, int state, int x, int y) {
MouseCallbacks::iterator i = mouse_callbacks.find(glutGetWindow());
if (i != mouse_callbacks.end()) { // should always be true, but possibly not
// if deregistering and events arrive
i->second.second(i->second.first, button, state, x, y);
}
}
void set_mousefunc(
MouseCallback::first_type obj,
MouseCallback::second_type f
) {
assert(obj); // preconditions
assert(f);
mouse_callbacks[glutGetWindow()] = MouseCallback(obj, f);
//glutMouseFunc(handle_mouse); // uncomment in non-example ##E##
handle_mouse(0, 0, 0, 0); // pretend it's triggered immediately ##E##
}
void unset_mousefunc() {
MouseCallbacks::iterator i = mouse_callbacks.find(glutGetWindow());
if (i != mouse_callbacks.end()) {
mouse_callbacks.erase(i);
//glutMouseFunc(0); // uncomment in non-example ##E##
}
}
Example:
#include <iostream>
struct Example {
void MouseButton(int button, int state, int x, int y) {
std::cout << "callback\n";
}
static void MouseButtonCallback(
void* self, int button, int state, int x, int y
) {
static_cast<Example*>(self)->MouseButton(button, state, x, y);
}
};
int main() {
Example obj;
set_mousefunc(&obj, &Example::MouseButtonCallback);
return 0;
}
Notice that you don't call glutMouseFunc directly anymore; it is managed as part of [un]set_mousefunc.
Just in case it isn't clear: I've rewritten this answer so it should work for you and so that it avoids the C/C++ linkage issue being debated. It will compile and run as-is (without glut), and it should work with glut with only minor modification: comment or uncomment the 4 lines marked ##E##.
No, a pointer to an instance function can not be given to a callback function expecting a function pointer of a certain signature. Their signatures are different. It won't compile.
Generally such APIs allow you to pass in a void* as a "context" parameter. You pass in your object there, and write a wrapper function which takes the context as the callback. The wrapper casts it back to whatever class you were using, and calls the appropriate member function.
You can't replace a static callback with an instance one. When the caller calls your callback, on what instance whoul it call? In other words, how does the caller pass in the formal 'this' argument?
The solution is to have a static callback stub and pass the instance as argument, which implies the callee must accept an arbitrary pvoid that will pass back when invoking the callback. In the stub, you can then call the non-static method:
class C {
void f() {...}
static void F(void* p) {
C* pC = (C*)p;
pC->f();
}
}
C* pC = ...;
someComponent.setCallback(&C::F, pC);
Contrary to what everyone seems to be saying, you most definitely CAN use a non-static member function as a callback method. It requires special syntax designed specifically for getting pointers to non-static members, and special syntax to call that function on a specific instance of a class. See here for a discussion of the needed syntax.
Here is sample code that illustrates how this works:
#include <cstdlib>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
class Operational
{
public:
Operational(int value) : value_(value) {};
string FormatValue() const ;
private:
int value_;
};
string Operational::FormatValue() const
{
stringstream ss;
ss << "My value is " << value_;
return ss.str();
}
typedef string(Operational::*FormatFn)() const; // note the funky syntax
Operational make_oper(int val)
{
return Operational(val);
}
int main()
{
// build the list of objects with the instance callbacks we want to call
Operational ops[] = {1, 2, 3, 5, 8, 13};
size_t numOps = sizeof(ops)/sizeof(ops[0]);
// now call the instance callbacks
for( size_t i = 0; i < numOps; ++i )
{
// get the function pointer
FormatFn fn = &Operational::FormatValue;
// get a pointer to the instance
Operational* op = &ops[i];
// call the callback on the instance
string retval = (op->*fn)();
// display the output
cout << "The object # " << hex << (void*)op << " said: '" << retval << "'" << endl;
}
return 0;
}
The output of this program when I ran it on my machine was:
The object # 0017F938 said: 'My value is 1'
The object # 0017F93C said: 'My value is 2'
The object # 0017F940 said: 'My value is 3'
The object # 0017F944 said: 'My value is 5'
The object # 0017F948 said: 'My value is 8'
The object # 0017F94C said: 'My value is 13'
You cannot use a non-static member function in this case.
Basically the type of the argument expected by glutMouseFunc is
void (*)(int, int, int, int)
while the type of your non-static member function is
void (StartHand::*)(int, int, int, int)
First problem is that types don't really match.
Second, in order to be able to call that method, the callback would have to know which object ( i.e. "this" pointer ) your method belongs to ( that's pretty much why the types are different in the first place ).
And third, I think you're using the wrong syntax to retrieve the method's pointer. The right syntax should be: &StartHand::MouseButton.
So, you have to either make that method static or use some other static method that would know which StartHand pointer to use to call MouseButton.
The following works in c++ to define a c callback function, useful for example when using glut (glutDisplayFunc, glutKeyboardFunc, glutMouseFunc ...) when you only need a single instance of this class :
MyClass * ptr_global_instance = NULL;
extern "C" void mouse_buttons_callback(int button, int state, int x, int y) {
// c function call which calls your c++ class method
ptr_global_instance->mouse_buttons_cb(button, state, x, y);
}
void MyClass::mouse_buttons_cb(int button, int state, int x, int y) {
// this is actual body of callback - ie. if (button == GLUT_LEFT_BUTTON) ...
// implemented as a c++ method
}
void MyClass::setup_glut(int argc, char** argv) { // largely boilerplate glut setup
glutInit(&argc, argv);
// ... the usual suspects go here like glutInitWindowSize(900, 800); ...
setupMouseButtonCallback(); // <-- custom linkage of c++ to cb
// ... other glut setup calls here
}
void MyClass::setupMouseButtonCallback() {
// c++ method which registers c function callback
::ptr_global_instance = this;
::glutMouseFunc(::mouse_buttons_callback);
}
In your MyClass header we add :
void mouse_buttons_cb(int button, int state, int x, int y);
void setupMouseButtonCallback();
This also works using identical logic flows to setup your glut
call to glutDisplayFunc(display)