Given the typical following code:
void Listener::Handl eEvent(const Event& event)
{
// handling code
}
In a "clean" fashion, how is it possible to add custom data (to be
subsequently accessed) to the Event instance? By custom data i mean
practically anything, from a class to a single int. Particularly to my case,
it would be assumed that such data would be appropriately destroyed as the
Event is consumed/destroyed.
I have considered several options but still can't find one that satisfies
the "clean" requisite. One implies keeping a void* "userData" in the Event
class, and using setData<T>/getData<T> template methods to access and
automagically cast the data to the proper type, with type safety checks in
debug. Problems arise if I want the Event class to handle the destruction of
the custom data because internally to Event, the custom data's type is
unknown. Again particularly to my case, I could use a memory pool for the
custom data, write the custom data to it using placement new and then clean
the pool after each frame as all events are considered "consumed" between
event iterations. Potential problems arise if classes are used as custom
data, since cleaning the memory pool wouldn't call the destructors of the
custom data.
I would guess Boost::any would also be a solution, but we are trying to
avoid using it in our project.
Subclassing the Event class and adding the custom data as members, and then
downcasting to the proper type, is probably also a solution. I am thinking
it is possibly the only interesting solution (as it is very common), but I
hope to think someone thought of an elegant alternative. I think it is
likely that there is a templated solution, yet i can't figure it out.
It has to be kept in mind that the Event class, in my case, would
potentially be part of an external library, so using a union member of all
possible custom data types isn't a viable solution (not to say it is quite
ugly...)
Regards,
Luc Tremblay 7 1948
On Mon, 5 Jul 2004 18:00:03 -0300, Luc Tremblay <an*******@symp atico.ca>
wrote: Given the typical following code:
void Listener::Handl eEvent(const Event& event) { // handling code }
In a "clean" fashion, how is it possible to add custom data (to be subsequently accessed) to the Event instance? By custom data i mean practically anything, from a class to a single int. Particularly to my case, it would be assumed that such data would be appropriately destroyed as the Event is consumed/destroyed.
[snip]
I hope to think someone thought of an elegant alternative. I think it is likely that there is a templated solution, yet i can't figure it out.
Assuming HandleEvent is not virtual then the following might work
template <class T>
void Listener::Handl eEvent(const Event<T>& event)
{
// handling code
}
with Event defined like this
template <class T>
class Event
{
// standard event stuff
...
// user data
T user;
};
Something to think about anyway.
john
How about having an interface and call it as ICustData, with a virtual
destructor.
Any class which wants to be appendable will need to implement this
interface.
Your host class will then have a member ICustData* m_pCustData, with
get/set methods to access the ICustData member. The client can then
use dynamic_cast to get the actual pointer that he needs.
And when the host class goes out of scope, just call delete on
m_pCustData. Since the destructor is virtual, the correct destructor
will be called.
It's a pretty straight forward solution, but is this what you are
looking for? And do you find it intutive?
"Luc Tremblay" <an*******@symp atico.ca> wrote in message news:<z9******* *************@n ews20.bellgloba l.com>... Given the typical following code:
void Listener::Handl eEvent(const Event& event) { // handling code }
In a "clean" fashion, how is it possible to add custom data (to be subsequently accessed) to the Event instance? By custom data i mean practically anything, from a class to a single int. Particularly to my case, it would be assumed that such data would be appropriately destroyed as the Event is consumed/destroyed.
I have considered several options but still can't find one that satisfies the "clean" requisite. One implies keeping a void* "userData" in the Event class, and using setData<T>/getData<T> template methods to access and automagically cast the data to the proper type, with type safety checks in debug. Problems arise if I want the Event class to handle the destruction of the custom data because internally to Event, the custom data's type is unknown. Again particularly to my case, I could use a memory pool for the custom data, write the custom data to it using placement new and then clean the pool after each frame as all events are considered "consumed" between event iterations. Potential problems arise if classes are used as custom data, since cleaning the memory pool wouldn't call the destructors of the custom data.
I would guess Boost::any would also be a solution, but we are trying to avoid using it in our project.
Subclassing the Event class and adding the custom data as members, and then downcasting to the proper type, is probably also a solution. I am thinking it is possibly the only interesting solution (as it is very common), but I hope to think someone thought of an elegant alternative. I think it is likely that there is a templated solution, yet i can't figure it out.
It has to be kept in mind that the Event class, in my case, would potentially be part of an external library, so using a union member of all possible custom data types isn't a viable solution (not to say it is quite ugly...)
Regards,
Luc Tremblay
"John Harrison" wrote On Mon, 5 Jul 2004 18:00:03 -0300, Luc Tremblay wrote:
Assuming HandleEvent is not virtual then the following might work
Unfortunately, in my case, it indeed needs to be virtual.
"Raghupathy " wrote : How about having an interface and call it as ICustData, with a virtual destructor.
Any class which wants to be appendable will need to implement this interface. Your host class will then have a member ICustData* m_pCustData, with get/set methods to access the ICustData member. The client can then use dynamic_cast to get the actual pointer that he needs.
And when the host class goes out of scope, just call delete on m_pCustData. Since the destructor is virtual, the correct destructor will be called.
It's a pretty straight forward solution, but is this what you are looking for? And do you find it intutive?
Unfortunately your solution rules out base types, which is unacceptable in
my case.
Luc Tremblay wrote : In a "clean" fashion, how is it possible to add custom data (to be subsequently accessed) to the Event instance? By custom data i mean practically anything, from a class to a single int. Particularly to my
case, it would be assumed that such data would be appropriately destroyed as the Event is consumed/destroyed.
I should correct myself. I do not mean dynamically as in "during process
itself"; a pre-definition of the data type to use for each Event is
possible, considering Events are uniquely defined by their UId. This leads
me to think that a solution would be a templated factory that maps Event Ids
to data types.
* Luc Tremblay: Given the typical following code:
void Listener::Handl eEvent(const Event& event) { // handling code }
In a "clean" fashion, how is it possible to add custom data (to be subsequently accessed) to the Event instance? By custom data i mean practically anything, from a class to a single int. Particularly to my case, it would be assumed that such data would be appropriately destroyed as the Event is consumed/destroyed.
You need downcasting, but that downcasting should be centralized, not
forced upon each event consumer.
To centralize the downcasting use the visitor pattern.
// General event definitions.
class Event;
class IEventConsumer
{
protected:
virtual onEvent( Event const& anEvent ) {};
};
class Event
{
public:
virtual void visit( IEventConsumer& aConsumer )
{
aConsumer.onEve nt( *this );
};
};
// A particular event with custom data.
class CustomData;
class CustomEvent;
class ICustomEventCon sumer: public IEventConsumer
{
protected:
virtual onCustomEvent( CustomEvent const& anEvent ) {};
};
class CustomEvent: public Event
{
public:
typedef Event Base;
virtual void visit( IEventConsumer& aConsumer )
{
ICustomEventCon sumer* pi = dynamic_cast<IC ustomEventConsu mer*>( &aConsumer );
if( pi != NULL )
{
pi->onCustomEven t( *this );
}
else
{
Base::visit( aConsumer );
}
}
CustomData const& data() const { ... }
};
// A client
class Client: protected ICustomEventCon sumer
{
protected:
virtual void onCustomEvent( CustomEvent const& anEvent )
{
CustomData const& = anEvent.data(); ¨
// Whatever.
}
};
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Luc Tremblay wrote: "John Harrison" wrote On Mon, 5 Jul 2004 18:00:03 -0300, Luc Tremblay wrote:
Assuming HandleEvent is not virtual then the following might work
Unfortunately, in my case, it indeed needs to be virtual.
Then you could rearrange it like this:
class EventBase {
// standard event stuff
};
void Listener::Handl eEvent(const EventBase& event)
{
// handling code
}
template <class T>
class Event : public EventBase {
// user data
T user;
};
Since Listener::Handl eEvent() seems pretty much fixed in that
that it receives a reference of some base type of event, the
real question is how you do the dispatching based on the
dynamic type of an event. I think the solution may depend
a lot on the typical kind of the user data T and the way you
have to handle it.
For the simplest case, if handling of all possible types of
the user data could be provided in a uniform way:
class Action {
public:
template <typename T>
void handle(T const& v);
};
then declaring the following function in EventBase
virtual void handleUserData( Action& pa) const = 0;
and implementing it in Event<T> as
void handleUserData( Action& a) const {a.handle(user) ;}
would solve the most of the problem.
Denis This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: dave |
last post by:
I have created a control that inherits from datagrid. Before I render the grid html I want to create a linkbutton. I am using the following code to do so
Dim lnk As New LinkButto
lnk.Text = "Here
lnk.ID = "LinkID
AddHandler lnk.Command, AddressOf lnkSort_Comman
lnk.CommandName = "cmdSort
lnk.CommandArgument = col.HeaderTex
cell.Controls.Add(lnk
|
by: Ken Varn |
last post by:
I want to add my own custom <STYLE> section in the <HEAD> section of my
ASP.NET page within a custom control. Can someone tell me how I can have my
custom control add tags to the <HEAD> section of the page dynamically when
the page is rendered?
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
|
by: Earl Teigrob |
last post by:
I am building a custom control that I want to server as a container for child
controls that can be dynamically added to this control. I can persist the
child controls that are added to my custom control but can to get them to
retain view state.
Below is a striped down version of my control. It exposes one property of
type System.Web.UI.Control that the containing page my set with a dynamically
created control My custom control will then...
|
by: Amit Maheshwari |
last post by:
I have my aspx page on which i am creating <input type=text> on client side
using javascript. Now when i submit my page i need to access these controls
to get the value entered by the user.
the code is like
<script language="javascript">
<!--
//to count no of file upload controls
var intRowCount = 2;
function AddApp()
|
by: |
last post by:
I have made some user controls with custom properties. I can set those
properties on instances of my user controls, and I have programmed my user
control to do useful visual things in response to how those properties are
set.
I want to be able to do two other things:
a) add User control instances to my page, filling in the place of
placeholder controls, and
b) programmatically setting custom properties on those dynamically spawned...
| |
by: m.wanstall |
last post by:
I have a crosstab query that compiles data for Months of the year. I
have a stacked select query on top of that crosstab query that uses
the latest 2 months data and exports it to a fixed length flat file.
Ideally I would like to be able to just select the Last 2 Columns of
the Crosstab query as inputs to the Select query WITHOUT having to go
in month after month and manually change it...
I may be asking a bit much here but is there a...
|
by: Smithers |
last post by:
I have a Windows Forms application that implements a plug-in architecture
whereby required assemblies are identified and loaded dynamically.
Here are the relevant classes:
A = application = Windows Forms class
B = a singleton hosted within A. B is responsible for dynamically loading
classes X, Y, and Z.
|
by: gnewsgroup |
last post by:
I have a GridView, in which the header of one column changes depending
on the selected value of a DropDownList outside of this GridView.
I did this dynamic header through
protected void GridView1_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
|
by: RichB |
last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am
trying to add a tabbed control to the page.
I have no problems within the aspx file, and can dynamically manipulate a
tabcontainer which has 1 panel already, however I want to try create the
TabPanels dynamically. I followed the advice here:
http://www.asp.net/learn/ajax-videos/video-156.aspx (3rd comment - Joe
Stagner)
|
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,...
|
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...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |