Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Callbacks in C++

Reply
Thread Tools

Callbacks in C++

 
 
makuchaku
Guest
Posts: n/a
 
      01-03-2005
Hi,

How can I implement callback mechanism in C++.

Basically I need it to provide exception handling to a user who will be
using a library developed by me?

If not callbacks, how can i enable an end user/developer to call his
custom functions in case of an error.

Please elaborate a bit... thanks alot,
makuchaku
---
http://makuchaku.blogspot.com
---

 
Reply With Quote
 
 
 
 
Nicolas Pavlidis
Guest
Posts: n/a
 
      01-03-2005
makuchaku wrote:

> Hi,
>
> How can I implement callback mechanism in C++.
>
> Basically I need it to provide exception handling to a user who will be
> using a library developed by me?


Why you don't want to throw an exception to the user? He can do
something with it.

> If not callbacks, how can i enable an end user/developer to call his
> custom functions in case of an error.


There are several ways to do this:
1) The good old functionpointers.
2) Functionobjects (these are classes that define the operator () ()
3) The template way:
template<typename Callable>
void registerCallback(Callable const &call_back);
4) The Functors of the Lokilibrary.

But IMHO it is the best way to throw an exception, and let the user do
what he wants do do.

> Please elaborate a bit... thanks alot,


HTH && Kind regards,
Nicolas

 
Reply With Quote
 
 
 
 
Evan Carew
Guest
Posts: n/a
 
      01-04-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

makuchaku wrote:
> Hi,
>
> How can I implement callback mechanism in C++.
>
> Basically I need it to provide exception handling to a user who will be
> using a library developed by me?
>
> If not callbacks, how can i enable an end user/developer to call his
> custom functions in case of an error.
>

Hmm... Im not sure there's enough of a fact pattern here to diagnose a
problem, however, if you are looking for a OO callback pattern, then the
observer pattern is what you are looking for. For a very good example,
see Bruce Eckel's "Thinking in C++, 2nd edition" online version at:
http://www.camtp.uni-mb.si/books/Thi...tml#Heading323

Enjoy,
Evan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFB2fhxpxCQXwV2bJARAnc+AKDGFExc3ARVn+GLA1sS9v cX73iwtwCfXqpR
sqyS8Cwlsn9G0CbOZY5IEVo=
=USFq
-----END PGP SIGNATURE-----
 
Reply With Quote
 
makuchaku
Guest
Posts: n/a
 
      01-04-2005
Hi,
Thanks.

I'm already using exceptions & letting the user handle them. But i also
want user to override a method (say onError()) from my Error class,
which shall be called in case of exception_object.onError()

But the problem is how do i implement this mechanism using virtual
functions (user overriding the onError()), but still retaining the
arguments which i passed in my -- throw excepti_object("error string",
code, value)

Have a look at this code


//The Parent Error class from the Library
//SockErr.h
using namespace std;
class SockErr
{

private:

string message;


public:

SockErr()
{
}

SockErr(string message)
{
this->message=message;
}

string getMessage()
{
return(message);
}

virtual void onError()
{

}


};





//The class in error.cc which is written by the end-user
#include "SocErr.h"

using namespace std;
class SocketHandler : public SockErr
{
public:

void onError()
{
cout<<"Inside onError() of UserDefined
handler"<<endl;
}
};


int main(void)
{

try
{
Socket(10000, SOCK_STREAM, 0);
}
catch(SockErr *s)
{
s=new SocketHandler();
s->onError();

cout<<s->getMessage()<<endl<<s->getMessage().length()<<endl;
}
}


I need to use SockErr *s as i have to implement virtual functions....

so? any advice?

thanks,
makuchaku
---
http://makuchaku.blogspot.com
Linux is Life... coz ...Life is Freedom
---

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Callbacks 2.0 and ActiveX =?Utf-8?B?UGF1bCBN?= ASP .Net 4 10-25-2005 04:38 PM
Callbacks and FireFox browser =?Utf-8?B?RGlmZmlkZW50?= ASP .Net 0 07-28-2005 10:01 PM
Gridview, using Callbacks when Sorting and Paging ck388 ASP .Net 1 06-06-2005 09:55 PM
Problem using Client Side Callbacks in 2.0 =?Utf-8?B?TmV2ZXJseW4=?= ASP .Net 0 05-08-2005 04:10 AM
Client Side Callbacks for ASP.NET 1.x Lucas Tam ASP .Net 6 02-07-2005 01:41 PM



Advertisments