🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3567

Re: Generic Object Callbacks

#3567
From: linh (l.) dang
Date: Fri, 01 Mar 1996 17:17
85 lines
2741 bytes
>> "ian" == "ian (i ) willmott" <willmott@bnr.ca> writes:

 ian> The point is that the "driver" shouldn't have to know the type
 ian> of the object the callback is directed at. This is particularly
 ian> important when the "driver" issuing the callback is a library
 ian> API, such as Motif; you do not want the library to have to know
 ian> about application data types and you do not want to constrain
 ian> the application to use a single callback object type that is
 ian> defined by the library. The point of the proposal is that there
 ian> is a simple extension to the language which would accomplish
 ian> this in a type-safe way.

I don't understand why the example in the C++ Report is not sufficient
for you. Any way if you want a simpler (i.e. less sophisticated
version, here it is and please, no more extension !!!!

//
// library side ===========================================
//
class Callback
{
public:
  virtual void call() = 0;
};

void foo(Callback& cb)
{
  cb.call();			// callback
}

template<class ClientInfo>
class GenCallback : public Callback
{
public:
  ClientInfo client;
  
  virtual void call()
  {
    ((client.object)->*(client.method))(client.data);
  }
};

//
// application side ===========================================
//

class MyObject			// your object here
{
public:
  struct arg { int i; double f; };
  virtual void bar(arg&);
};

typedef void (MyObject::*MyObjectMethod)(MyObject::arg&);

struct MyCBInfo
{
  MyObject* object;
  MyObjectMethod method;
  MyObject::arg data;
};

main()
{
  MyObject o;
  GenCallback<MyCBInfo>* p = new GenCallback<MyCBInfo>;
  p->client.object = &o;
  p->client.method = MyObject::bar;
  p->client.data.i = 1;
  p->client.data.f = 0.1;
  foo(*p);
  return 0;
}



--------------
L.D.
---
[ To submit articles: try just posting with your news-reader.
                      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu.
]
Technical Details
Message-ID: <gknwx54om1a.fsf@bmtlh30.nortel.ca>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <4h5j7b$1ur@bcarh8ab.bnr.ca>

Show Full Headers
Complete Article Headers
From: "linh (l.) dang" <linhd@bnr.ca>
Message-ID: <gknwx54om1a.fsf@bmtlh30.nortel.ca>
X-Original-Date: Fri, 1 Mar 1996 10:44:49 -0500
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 01 Mar 96 17:17:32 GMT
Approved: fjh@cs.mu.oz.au
References: <4h5j7b$1ur@bcarh8ab.bnr.ca>
Subject: Re: Generic Object Callbacks
Newsgroups: comp.std.c++
Organization: The entity formerly know as BNR
X-Newsreader: Gnus v5.1
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMTcxT+EDnX0m9pzZAQGJewF+IXrd5wDWjD/ZTlZYY01/Uq3DvA1BxHqs
 X3erjHLfcEPksawXdSsuYPxJ504SneIF
 =Podx