Templates and VC++ event handling and generics

The difference between templates and generics is a hotly debated topic. I am trying to understand all of this and Bruce Eckel recently wrote about a form of ‘Curiously Recurring Template Pattern’ in his blog.

Let us look at Java code that uses this pattern


public class PropertySupportAspect<T extends PropertySupportAspect> {

PropertyChangeSupport support = new PropertyChangeSupport (this);

//Other methods are not shown

public void firePropertyChange( String property,
String oldval,
String newval) {
support.firePropertyChange( property,
( oldval == null ) ?
oldval :
new String(oldval),
new String(newval));
}
}
class Bean extends PropertySupportAspect{
private String name;

public String getName() {
return name;
}
public void setName( String name ) {
firePropertyChange( "name",
getName(),
name );
this.name = name;
}
}

Since ‘CRTP’ is usually associated with C++ templates I decided to try the same code with similar functionality using VC++.

This is what I could manage with VC++.

#include

template<T>
class PropertyChangeListener {
public:
void propertyChange( std::string oldValue, std::string newValue ) {
printf("Changed from " + oldValue + " to " + newValue );

}

void hookEvent(T* bean) {
__hook(&T::MyEvent, bean, &PropertyChangeListener::propertyChange);
}

void unhookEvent(T* bean) {
__unhook(&T::MyEvent, bean, &PropertyChangeListener::propertyChange);
}
};

class Bean : public PropertyChangeListener {
std::string name;
typedef PropertyChangeListener listener;

public:
__event void MyEvent(std::string value);

public:
void setName( std::string name ){
listener::hookEvent(this);
__raise this->MyEvent( name );
}
};

int main() {
Bean bean;
bean.setName( "Test" );
}

This code does not compile because methods ‘hookEvent’ and ‘unhookEvent’ have a problem. I tried the VC++ forum and I will update if there is a solution. I myself don’t have experience with VC++.

Update :
I corrected my code error based on the reply I received from the VC++ team in the MSDN forum. I hadn’t noticed the description of these errors.

1. C3713: ‘PropertyChangeListener::propertyChange’: an event handler method must have the same function parameters as the source ‘Bean::MyEvent’

class Bean : public PropertyChangeListener {
std::string name;
typedef PropertyChangeListener listener;

public:
__event void MyEvent(std::string oldValue, std::string newValue);

public:
void setName( std::string name1 ){
listener::hookEvent(this);
__raise this->MyEvent( name, name1 );
}
};

1. C3740: ‘PropertyChangeListener’: templates cannot source or receive events

Quote from the team:
We don’t support templates both in event source and event receiver. You can not hook an event to a member template function. Further more, we might cut Native Event support entirely in the future release. We really encourage you to write your own event codes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: