Re: [LAD] Inter thread Communication: Design Approach

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: The Linux Audio Developers' Mailing List <linux-audio-dev@...>
Date: Saturday, August 20, 2011 - 3:04 pm

On 08/20/2011 09:19 AM, Harry van Haaren wrote:

IMHO, You're more or less going in the right direction. There's two
main ways to structure your EventType.. and they are both essentially
using inheritance. We'll call them "The C way" and "The C++ Way". They
are more or less equivalent.

Consider the following to be pseudocode. I didn't test it.

THE C WAY
=========

struct base_event_t_ {
uint16_t type;
uint16_t size;
};
typedef struct base_event_t_ base_event_t;

#define EVENT_ONE 1
struct event_one_t_ {
uint16_t type;
uint16_t size; /* must be EVENT_ONE */
/* extra event-specific data */
};
typedef struct event_one_t_ event_one_t;

#define EVENT_TWO 2
struct event_two_t_ {
uint16_t type;
uint16_t size; /* must be EVENT_TWO */
/* extra event-specific data */
};
typedef struct event_one_t_;

/* ... */

void my_function(event_t *ev)
{
switch(ev->type) {
case EVENT_ONE:
handle_event_one( (event_one_t*) ev );
break;
case EVENT_TWO:
handle_event_two( (event_two_t*) ev );
break;
}
}

DISADVANTAGE: You have to manually manage the type and size arguments.

THE C++ WAY
===========

class event_t
{
public:
enum {
EVENT_ONE = 1,
EVENT_TWO = 2
}; /* note: using an enum isn't necc. */

virtual ~event_t() {}

int type() = 0;
uint32_t size() = 0;
};

class event_one_t : public event_t
{
public:
int type() { return int(EVENT_ONE); }
uint32_t size() { return sizeof(event_one); }

/* event-specific stuff */
};

class event_two_t : public event_t
{
public:
int type() { return int(EVENT_TWO); }
uint32_t size() { return sizeof(event_two); }

/* event-specific stuff */
};

/* ... */

void my_function(const event_t& ev)
{
switch(ev->type()) {
case event_t::EVENT_ONE:
handle_event_one( dynamic_cast(ev) );
break;
case event_t::EVENT_TWO:
handle_event_two( dynamic_cast(ev) );
break;
}
}

DISADVANTAGE: Using dynamic_cast<> uses RTTI and can have a performance
penalty... but guards against programming mistakes (throwing an
exception if cast is invalid). Switch to reinterpret_cast<> if speed is
a problem.

-gabriel
_______________________________________________
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[LAD] Inter thread Communication: Design Approach, Harry van Haaren, (Sat Aug 20, 2:19 pm)
Re: [LAD] Inter thread Communication: Design Approach, Gabriel Beddingfield, (Sat Aug 20, 3:04 pm)
Re: [LAD] Inter thread Communication: Design Approach, David Robillard, (Thu Sep 1, 6:18 pm)
Re: [LAD] Inter thread Communication: Design Approach, Gabriel Beddingfield, (Fri Sep 2, 2:29 am)
Re: [LAD] Inter thread Communication: Design Approach, David Robillard, (Fri Sep 2, 3:22 pm)
Re: [LAD] Inter thread Communication: Design Approach, Gabriel Beddingfield, (Fri Sep 2, 4:03 pm)
Re: [LAD] Inter thread Communication: Design Approach, David Robillard, (Fri Sep 2, 4:29 pm)
Re: [LAD] Inter thread Communication: Design Approach, Harry van Haaren, (Thu Sep 1, 8:37 pm)
Re: [LAD] Inter thread Communication: Design Approach, Florian Paul Schmidt, (Fri Sep 2, 8:36 am)
Re: [LAD] Inter thread Communication: Design Approach, David Robillard, (Fri Sep 2, 3:13 pm)
Re: [LAD] Inter thread Communication: Design Approach, Florian Paul Schmidt, (Fri Sep 2, 5:29 pm)
Re: [LAD] Inter thread Communication: Design Approach, Harry van Haaren, (Fri Sep 2, 11:59 am)
Re: [LAD] Inter thread Communication: Design Approach, Fons Adriaensen, (Thu Sep 1, 6:37 pm)
Re: [LAD] Inter thread Communication: Design Approach, David Robillard, (Thu Sep 1, 8:30 pm)
Re: [LAD] Inter thread Communication: Design Approach, Fons Adriaensen, (Fri Sep 2, 10:04 am)
Re: [LAD] Inter thread Communication: Design Approach, Fons Adriaensen, (Sat Sep 3, 9:25 pm)
Re: [LAD] Inter thread Communication: Design Approach, Florian Paul Schmidt, (Sun Sep 4, 10:22 am)
Re: [LAD] Inter thread Communication: Design Approach, Florian Paul Schmidt, (Fri Sep 2, 10:50 am)
Re: [LAD] Inter thread Communication: Design Approach, David Robillard, (Thu Sep 1, 8:34 pm)
Re: [LAD] Inter thread Communication: Design Approach, Harry van Haaren, (Sat Aug 20, 4:17 pm)
Re: [LAD] Inter thread Communication: Design Approach, Arnold Krille, (Sat Aug 20, 3:57 pm)