473,698 Members | 2,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling upwards from within an aggregation

If I have two classes:

class bar
{
public:
void RunTheBar(void) ;
};

class foo
{
private:
bar bar_;
public:
void Callback(void);
void RunTheBar(void) {bar_.RunTheBar ();}
...
};

What's the best way to design "bar" such that it can call
foo::Callback()
from within its RunTheBar() method?

This is pretty straightforward using the Borland C++Builder extension
"__closure" :

typedef void (__closure *MyCallback)(vo id);

class bar
{
private:
MyCallback callback_;
public:
bar(void):callb ack_(0){}
void RunTheBar(void) ;
void SetCallback(MyC allback Acallback){call back_ = Acallback;}
};

void bar::RunTheBar( void)
{
// ...
if (callback_)
{
callback_();
}
// ...
}

class foo
{
private:
bar bar_;
public:
foo(void)
{
bar_.SetCallbac k(Callback);
}
void Callback(void);
void RunTheBar(void) {bar_.RunTheBar ();}
};

Is there a way I can do this without using the non standard __closure,
but still without "bar" needing a definition of "foo"? It would be nice
if "bar" could be contained by objects other than "foo" without needing
to change it each time.

--
Simon Elliott
http://www.ctsn.co.uk/


Jul 19 '05 #1
10 2570
Simon Elliott wrote:
Is there a way I can do this without using the non standard __closure,
but still without "bar" needing a definition of "foo"? It would be
nice if "bar" could be contained by objects other than "foo" without
needing to change it each time.


Did you look at boost::function ?

--
Attila aka WW
Jul 19 '05 #2
On Fri, 26 Sep 2003 11:39:47 +0100, Simon Elliott
<si***@nospam.d emon.co.uk> wrote:
If I have two classes:

class bar
{
public:
void RunTheBar(void) ;
};

class foo
{
private:
bar bar_;
public:
void Callback(void);
void RunTheBar(void) {bar_.RunTheBar ();}
...
};

What's the best way to design "bar" such that it can call
foo::Callback( )
from within its RunTheBar() method?

This is pretty straightforward using the Borland C++Builder extension
"__closure" :

typedef void (__closure *MyCallback)(vo id);
Hmm, I think GCC has something like this too.

class bar
{
private:
MyCallback callback_;
public:
bar(void):callb ack_(0){}
void RunTheBar(void) ;
void SetCallback(MyC allback Acallback){call back_ = Acallback;}
};

void bar::RunTheBar( void)
{
// ...
if (callback_)
{
callback_();
}
// ...
}

class foo
{
private:
bar bar_;
public:
foo(void)
{
bar_.SetCallbac k(Callback);
}
void Callback(void);
void RunTheBar(void) {bar_.RunTheBar ();}
};

Is there a way I can do this without using the non standard __closure,
but still without "bar" needing a definition of "foo"? It would be nice
if "bar" could be contained by objects other than "foo" without needing
to change it each time.


If you can use 3rd party libraries, then boost.function is what you
want:

#include <boost/function.hpp>
#include <boost/bind.hpp>

class bar
{
public:
typedef boost::function <void()> MyCallback;
bar(void){}
void RunTheBar(void) ;
void SetCallback(MyC allback Acallback){call back_ = Acallback;}
private:
MyCallback callback_;
};

void bar::RunTheBar( void)
{
// ...
if (!callback_.emp ty())
{
callback_();
}
// ...
}

class foo
{
private:
bar bar_;
public:
foo(void)
{
bar_.SetCallbac k(boost::bind(& foo::Callback, this));
}
void Callback(void);
void RunTheBar(void) {bar_.RunTheBar ();}
};

See www.boost.org.

Tom
Jul 19 '05 #3


Simon Elliott wrote:
[snip]
Is there a way I can do this without using the non standard __closure,
but still without "bar" needing a definition of "foo"? It would be nice
if "bar" could be contained by objects other than "foo" without needing
to change it each time.


Yep.
Define a class, eg. 'CallbackReceiv er'
Every class wishing to use bar has to be derived
from that class. With multiple inheritance you
can add the 'feature' of being a callback receiver
very easily to each and every class.
bar::RunThebar expects a pointer to a CallbeckReceive r
object, which it uses to make the callback:

class CallbackReceive r
{
public:
void Callback() = 0;
};

class bar
{
public:
void RunTheBar( CallbackReceive r* pCalledFrom )
{
...
pCalledFrom->Callback();
...
}
};

class foo : public CallbackReceive r
{
private:
bar bar_;

public:
void Callback();
void RunTheBar()
{
bar_.RunTheBar( this );
}
...
};

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #4
Attila Feher writes
Is there a way I can do this without using the non standard __closure,
but still without "bar" needing a definition of "foo"? It would be
nice if "bar" could be contained by objects other than "foo" without
needing to change it each time.
Did you look at boost::function ?


Thanks for the quick response. I was put off boost::function by this
snippet from the documentation:

<quote>
Member functions

In many systems, callbacks often call to member functions of a
particular object. This is often referred to as "argument binding", and
is beyond the scope of Boost.Function.
</quote>

And the example which follows:

<quote>
struct X {
int foo(int);
};

boost::function 2<int, X*, int> f;

f = &X::foo;

X x;
f(&x, 5);
</quote>

When the function f is called, it seems to need an instance of X to be
visible. Even if this could be a forward declaration, it still limits
the lower level class to only being able to call methods of objects of
type X, rather than methods of objects of an arbitrary type.

I've only ever designed one solution to this problem, and it involves
having an ABC which the lower level class calls a virtual method in. The
higher level class then defines a descendent of the ABC which does the
necessary. It's standard, it's flexible, and it works, but it's always
seemed to me that there must be a better and simpler way.
--
Simon Elliott
http://www.ctsn.co.uk/


Jul 19 '05 #5
Simon Elliott wrote:
Attila Feher writes
Is there a way I can do this without using the non standard
__closure, but still without "bar" needing a definition of "foo"?
It would be nice if "bar" could be contained by objects other than
"foo" without needing to change it each time.

Did you look at boost::function ?


Thanks for the quick response. I was put off boost::function by this
snippet from the documentation:


See the post by tom_usenet in this thread.

--
Attila aka WW
Jul 19 '05 #6
tom_usenet <to********@hot mail.com> writes
typedef void (__closure *MyCallback)(vo id);Hmm, I think GCC has something like this too.


There was some talk about something along these lines being introduced
into the standard.
If you can use 3rd party libraries, then boost.function is what you
want:


I can use those bits of boost that are implemented solely in terms of
headers.

[snip]

This seems a nice clean solution. And I like the way that I don't need
to initialise callback_ in bar's constructor.

I use Borland C++ Builder quite a lot. I'd be interested in using this
technique with the C++ form objects which are generated by this tool. I
wonder if boost::bind and boost::function work with these. One day when
Borland get their news server back up and running I'll ask them!

--
Simon Elliott
http://www.ctsn.co.uk/


Jul 19 '05 #7
Karl Heinz Buchegger <kb******@gasca d.at> writes
Simon Elliott wrote:

Is there a way I can do this without using the non standard __closure,
but still without "bar" needing a definition of "foo"? It would be nice
if "bar" could be contained by objects other than "foo" without needing
to change it each time.


Yep.
Define a class, eg. 'CallbackReceiv er'
Every class wishing to use bar has to be derived
from that class. With multiple inheritance you
can add the 'feature' of being a callback receiver
very easily to each and every class.
bar::RunTheb ar expects a pointer to a CallbeckReceive r
object, which it uses to make the callback:


[snip]

Interesting idea, simpler and neater than my own inheritance based
solution.

Sadly it won't work in all the cases I'd like to use. I use Borland C++
Builder quite a lot, and any C++ object which inherits from an Object
Pascal/Delphi object can't use MI.
--
Simon Elliott
http://www.ctsn.co.uk/


Jul 19 '05 #8
Attila Feher <at**********@l mf.ericsson.se> writes
See the post by tom_usenet in this thread.


Yep. This has been my first venture into comp.lang.c++ and I'm well
impressed at the quality and speed of the responses!
--
Simon Elliott
http://www.ctsn.co.uk/


Jul 19 '05 #9
WW
Simon Elliott wrote:
tom_usenet <to********@hot mail.com> writes
typedef void (__closure *MyCallback)(vo id);

Hmm, I think GCC has something like this too.


There was some talk about something along these lines being introduced
into the standard.


It was dapoted for the Library TR:

http://www.cuj.com/documents/s=8464/cujcexp0308sutter/

--
WW aka Attila
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3776
by: John Wood | last post by:
Let's say you're provided with an instance of a class. The instantiation takes place in another module that you have no control over. However, you've extended that class with your own value-added functionality. In C#, given such an instance, and a derived class, there's no way to 'attach' the instance to the class -- you have to either change the way the class was instantiated (not possible in this case), or wrap the class and delegate...
4
10963
by: cmrchs | last post by:
Hi, how do I implement aggregation and how composition in C# ? When I say : an Airplane has a Pilot then I use aggregation but when I say : an Airplane has a Cockpit then I use composition. How do I implement the difference in C# ? Here's what I try : class Pilot
2
2623
by: Jozsef Bekes | last post by:
Hi, I would like to implement aggregation in C#, therefore I'd need to implement the queryinterface COM function of a class. I am not sure whether this can be done, and if yes where to start. If someone has an example, a hint or anything that might help, please share it with me. Thanks and Regards, Jozsi
4
14446
by: Frederik Vanderhaegen | last post by:
Hi, Can anyone explain me the difference between aggregation and composition? I know that they both are "whole-part" relationships and that composition parts are destroyed when the composition whole is destroyed. Under a "whole-part" relationship I understand the following: the whole can't exists without the parts, but can the parts exist without the hole? f.e.: a car can't exist without an engine private engine _Engine
5
2793
by: Nice Chap | last post by:
Aggregation in COM was defined as 'Exposing an interface of an inner object by the outer object as though it were an interface of the outer object'. Is this type of aggregation possible in vb.net( or c#).
35
2869
by: Michel Sanner | last post by:
Hello, One of the greatest feature of Python in my opinion is the way the interpreter can be used to integrate a wide variety of software packages by dynamically linking them. This approach has been extremely successful for us so far but now I run into a license nightmare. Some the libraries we wrapped using SWIG are under GPL but the applications we are distributing are not (mainly because
23
2118
by: SenthilVel | last post by:
Hi Can any one let me know the websites/Pdf for learning Aggragation in C#?? Thanks Senthil
0
1242
by: Karigar | last post by:
I have been so far developing COM servers and clients in C++. I am new to C#/NET way of doing COM and was wondering if it is possible to accomplish aggregation in .NET platform. By aggregation I mean classic COM aggregation in which the outer object controls which interfaces it of inner object it exposes, and to the outside world it appears as the inner object without having to implement all the interfaces. By aggregation I do not mean a...
1
1440
by: OneShed | last post by:
Hi, I am trying to solve one problem. I have one object and I create another different object from it (aggregation). How can I now call methods of first object within the second object? Here is how I was trying to do it (its obviously wrong): class second; class first { public:
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.