virtual binding surviving stream transport of objects

N

noone

I have what I believe is a common problem. I must pull protocol messages
in from a data stream and save them as subclassed objects. the messages
vary in length and only share a fixed length header of common format.

consider:

struct message {}

struct m_reset: public message {}

struct m_shutdown: public message {}

struct receiver {
message* get(); // read message from stream
}

The problem is that message* needs to point to an object of the correct
subclass when it is received, based on the content of the message. there
will be literally hundreds of subclassed message types. is there a sneaky
way to do this other than

1) coding a lookup table in my get() method that knows all message types?
2) using a complicated object brokerage framework?

suggestions, or references to resources that address this issue?

thanks
 
I

Ian Collins

noone said:
I have what I believe is a common problem. I must pull protocol messages
in from a data stream and save them as subclassed objects. the messages
vary in length and only share a fixed length header of common format.

consider:

struct message {}

struct m_reset: public message {}

struct m_shutdown: public message {}

struct receiver {
message* get(); // read message from stream
}

The problem is that message* needs to point to an object of the correct
subclass when it is received, based on the content of the message. there
will be literally hundreds of subclassed message types. is there a sneaky
way to do this other than

1) coding a lookup table in my get() method that knows all message types?
2) using a complicated object brokerage framework?

suggestions, or references to resources that address this issue?
Use a map, key = type, value = simple factory object to create the
appropriate subclass.
 
N

noone

Use a map, key = type, value = simple factory object to create the
appropriate subclass.

thanks for the input but doesn't this action simply implement option
number (1) listed above? I'm trying to figure out a way to not have to
"register" each class type.

If there is no way around registering each type then so be it. How would
you implement such a map to cast the returned object to the correct type?

"message* get()" reads a stream of characters then must construct and
return an object of the (correct) subclass.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top