LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-01-2007, 07:23 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
Callback/signal mechanism in library


I have a library written in C++ which is free from Qt, GTK and any other GUI-code. Then on top of this library there is a GUI (which can be written in Qt, GTK, Win32, etc).

I need a way for the library to tell the GUI that it should update itself. This makes me think of the signal mechanism in Qt but as I said I cannot use Qt code inside the libs.

Any good programmer here who has any ideas of a solution?

Thanks in advance.
 
Old 09-02-2007, 12:36 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Using signals would be an extremely *poor* choice, for many different reasons.

Most GUI's have some kind of "invalidate region" API. Most GUI's also have some kind of "send event to GUI event queue" API. You should definitely consider either/both of these alternatives first.

IMHO .. PSM
 
Old 09-03-2007, 09:46 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
I know you don't want to be tied down to GTK+, but GTK+'s signalling mechanism is actually from the GObject library (which depends on Glib), which does not require GTK+, so that is an option. You can also check out libevent: http://monkey.org/~provos/libevent/
 
Old 10-07-2007, 08:20 AM   #4
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Here is what I did:

I created a register function within my library which took two arguments, one is a pointer to a function and one a pointer to void. These pointers are then stored inside the library.

The GUI is now require to call this register function with a pointer to itself and a pointer to a static function (cast to *void).

The lib will now use these two pointer when it needs to talk to the GUI. It calls the first pointer (which is a pointer to a function) where the first argument is the pointer-to-void (which is actually a pointer to the GUI instance running).

The static function (which has just been called by the lib) knows the actual type of the first argument (the class type of the GUI) and can therefore cast the pointer to this type and call the public functions of the GUI class.

So to sum it up, what I now require the GUIs to define if they are to use my library are a static function which looks like this:

Code:
void function(void *ptr)
{
    MyGUIClass *gui = (MyGUIClass*)ptr;
    ptr->refresh_function();
}
And second: the GUI has to register itself and the static function:
Code:
// inside the MyGUIClass class
Library *lib = new Library((void*)this, function);
Note how I decided to offer a constructor which automaticly registered the two pointers.

This works fine for me, but I would like to hear if there are some comments about it. Maybe it is not optimal, maybe there can be further improvements?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Signal frequency out of range / please change signal timing starrynite Linux - Hardware 1 01-06-2007 11:18 AM
Signal Strength Meter shows low signal? falcon56215 Linux - Networking 0 09-15-2006 07:39 AM
missing libsigc C++ callback library 1337 Twinkie Fedora 5 08-08-2004 06:38 PM
Signal 6: Unknown Signal <=> Error with MAYA4.X under Linux SOLVED!!!! Faeroon Linux - Software 9 05-09-2003 01:57 PM
Signal: 6 (Unknown Signal) - Problems running a program Faeroon Linux - Software 86 01-14-2003 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration