Callbacks in C++

Discussion in 'C++' started by makuchaku, Jan 3, 2005.

  1. makuchaku

    makuchaku Guest

    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
    ---
    makuchaku, Jan 3, 2005
    #1
    1. Advertising

  2. 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
    Nicolas Pavlidis, Jan 3, 2005
    #2
    1. Advertising

  3. makuchaku

    Evan Carew Guest

    -----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/Thinking-in-C /Chapter10.html#Heading323

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

    iD8DBQFB2fhxpxCQXwV2bJARAnc+AKDGFExc3ARVn+GLA1sS9vcX73iwtwCfXqpR
    sqyS8Cwlsn9G0CbOZY5IEVo=
    =USFq
    -----END PGP SIGNATURE-----
    Evan Carew, Jan 4, 2005
    #3
  4. makuchaku

    makuchaku Guest

    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
    ---
    makuchaku, Jan 4, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lucas Tam

    Client Side Callbacks for ASP.NET 1.x

    Lucas Tam, Feb 1, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    1,324
    Jeff @ Dart
    Feb 7, 2005
  2. =?Utf-8?B?TmV2ZXJseW4=?=

    Problem using Client Side Callbacks in 2.0

    =?Utf-8?B?TmV2ZXJseW4=?=, May 8, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    360
    =?Utf-8?B?TmV2ZXJseW4=?=
    May 8, 2005
  3. ck388
    Replies:
    1
    Views:
    10,660
    Brock Allen
    Jun 6, 2005
  4. =?Utf-8?B?RGlmZmlkZW50?=

    Callbacks and FireFox browser

    =?Utf-8?B?RGlmZmlkZW50?=, Jul 28, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    330
    =?Utf-8?B?RGlmZmlkZW50?=
    Jul 28, 2005
  5. =?Utf-8?B?UGF1bCBN?=

    Callbacks 2.0 and ActiveX

    =?Utf-8?B?UGF1bCBN?=, Oct 25, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    529
    sp3d2orbit
    Oct 25, 2005
Loading...

Share This Page