mandag den 1. november 2010

coding

#include < iostream >
#include < vector >
#include < assert.h >


// Okay, so this solution might be a bit too loose. However, it can be implemented
// to do all checks when registering new listeners. To use it, you add a value to
// an enum and create a wrapper class to hold your event arguments.
// The key point is that the manager doesn't need to know anything about the event arguments.


// ---------------------------------------------------------------------------
// Framework


// All event type must be enumerated. An enum is the easiest way to go if we want to avoid templates.
enum EEventType
{
EFT_Fruit = 0,
EFT_UNUSED_LAST
};

// Base class for events.
// Should in "principle" be completely abstract, but there's no real need.
class ZEventArgs
{
public:
ZEventArgs( const EEventType eType ) : m_eType( eType ) {}
EEventType GetType() const { return m_eType; }
private:
EEventType m_eType;
};

// This would be a member function in your case, but I don't want to go through all the wrapping syntax.
// I suspect your delegate wrapper doesn't care.
//
// Like the ZEventArgs, this could also be made into a class, e.g ZEventReciever.
// It could also have a EEventType member so correspondence could be checked when registering the listener.
// Making it a class would also enable you to register/unregister on contruction/destruction.
typedef void (*ZEventCB)( const ZEventArgs* );

// Manager class never needs to be updated.
class ZManager
{
public:
void Register( const EEventType eType, const ZEventCB EventListener )
{
// maybe also check it's not there already.
m_aaListeners[eType].push_back( EventListener );
}

void RecieveEvent( const ZEventArgs* Args ) const
{
const std::vector& aListeners = m_aaListeners[ Args->GetType() ];
for( size_t i = 0; i < aListeners.size(); ++i )
{
aListeners[i]( Args );
}
}

private:
// array of listeners
std::vector< ZEventCB > m_aaListeners[EFT_UNUSED_LAST];
};


// ---------------------------------------------------------------------------
// Example

// Implementation of Fruit args.
class ZFruitEventArgs : public ZEventArgs
{
public:
// constructor initializes type.
ZFruitEventArgs() : ZEventArgs( EFT_Fruit ) {}

// real args go here
float m_fTastines;
int m_nHowManyMore;
};

void TastyPrinter( const ZEventArgs* Args )
{
assert( Args->GetType() == EFT_Fruit );
const ZFruitEventArgs* FruitArgs = (ZFruitEventArgs*) Args;
std::cout << "mmm, tasty: " << FruitArgs->m_fTastines << std::endl;
}

void ManyPrinter( const ZEventArgs* Args )
{
assert( Args->GetType() == EFT_Fruit );
const ZFruitEventArgs* FruitArgs = (ZFruitEventArgs*) Args;
std::cout << "more: " << FruitArgs->m_nHowManyMore << std::endl;
}

int main()
{
ZManager TheMan;
TheMan.Register( EFT_Fruit, TastyPrinter );
TheMan.Register( EFT_Fruit, ManyPrinter );

ZFruitEventArgs Fruity;
Fruity.m_fTastines = 1.2f;
Fruity.m_nHowManyMore = 6;

TheMan.RecieveEvent( &Fruity );
}

mandag den 9. august 2010

Last stop Fuji

So my last day here has arrived. It's both a bit sad to leave this strange place, but I'm also starting to look forward to regular life (or just any kind of stability).

The goal for today is Mt Fuji, where I'm aiming for the top. See you there.

onsdag den 4. august 2010

Kumamoto

Er i det sydlige japan i Kumamoto. Er noget traet. Det er mest vandskab til at vandre i, men varmen inviterer ikke ligefrem til det. Smutter til Tokyo igen om nogle dage for at se Studio Ghibli.

mandag den 2. august 2010

Nagasaki

Hiroshima was not much fun in itself, but the peace musuem was touching.

I'm currently in Hataka, but it isnt very interesting. So I'm going to Nagasaki and then nearby Unzen. Unzen has many hot springs and, daa daa daaa, a volcano :)

søndag den 1. august 2010

Hiroshima

The Shinkansen was pretty fast. Not even enough time to sleep - even though I could use some relax very much.

Got to (hot) hiroshima and spend most of the day wandering around the parks and especially the peace museum. Seeing this is a must-do when visiting japan, very emotional.

Otherwise, I've had some great okonomiyaki, a few beers and heading to bed now. No more energy left :(

I'm getting lazy with the picture updates, but I'll throw everything on flickr, when I come home.

fredag den 30. juli 2010

Rain rain go away

It's only raining in the north, so I decided to take the train 20 hours to the south. So now I'm in Osaka, with a whole day of tiredness in the body.