Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Struct with dynamic sized array member

Reply
Thread Tools

Struct with dynamic sized array member

 
 
joe
Guest
Posts: n/a
 
      08-30-2011
An external interface is sending over the wire basically a variable
sized struct, similar to:

struct ExternalMessage
{
int m_numberOfOranges;
int m_numberOfApples;
Apple m_foo[=m_numberOfApples];
Orange m_orange[=m_numberOfOrange];
};

Having to access this struct which is of unknown size until it is
received is not straightforward.

I was considering trying to use templates to pre-create every possible
struct layout and use the accessors that way.

Something like:

class VarLenBase {};

template<int NUM_ORANGE,int NUM_APPLE>
struct VarLenMsg : public VarLenBase
{
int m_numberOfOranges;
int m_numberOfApples;
Apple m_foo[NUM_APPLE];
Orange m_orange[NUM_ORANGE];

// then accessors such as:
Orange getOrange(int index) { return m_orange[index]; }
// would just work without counting bytes and hopping
};

To pre-create them, i need some sort of mapping of the known params to
the concrete struct that matches:
std::map<std:air<int,int>, VarLenBase*> ;

But I'm strugglnig how to then use this information to cast the
received buffer as the type that I now know it is.

Is there a better way (probably yes). using std::vectors is not the
answer since i am not in control of the sender, and it has to be sent
over a network anyway.
Thanks,
JC
 
Reply With Quote
 
 
 
 
Goran
Guest
Posts: n/a
 
      08-31-2011
On Aug 30, 7:08*pm, joe <(E-Mail Removed)> wrote:
> An external interface is sending over the wire basically a variable
> sized struct, similar to:
>
> struct ExternalMessage
> {
> * int m_numberOfOranges;
> * int m_numberOfApples;
> * Apple m_foo[=m_numberOfApples];
> * Orange m_orange[=m_numberOfOrange];
>
> };
>
> Having to access this struct which is of unknown size until it is
> received is not straightforward.
>
> I was considering trying to use templates to pre-create every possible
> struct layout and use the accessors that way.
>
> Something like:
>
> class VarLenBase *{};


class VarLenBase
{
int m_numberOfOranges;
int m_numberOfApples;
};

would probably serve you better.

But...

> template<int NUM_ORANGE,int NUM_APPLE>
> struct VarLenMsg : public VarLenBase
> {
> * int m_numberOfOranges;
> * int m_numberOfApples;
> * Apple m_foo[NUM_APPLE];
> * Orange m_orange[NUM_ORANGE];
>
> * // then accessors such as:
> * Orange getOrange(int index) *{ return m_orange[index]; }
> * // would just work without counting bytes and hopping
>
> };
>
> To pre-create them, i need some sort of mapping of the known params to
> the concrete struct that matches:
> * std::map<std:air<int,int>, VarLenBase*> ;
>
> But I'm strugglnig how to then use this information to cast the
> received buffer as the type that I now know it is.
>
> Is there a better way (probably yes). *using std::vectors is not the
> answer since i am not in control of the sender, and it has to be sent
> over a network anyway.
> Thanks,


Even if you got your idea to work, as soon as wire-representation of
Apple and Orange isn't fixed size, it wouldn't work again. In other
words, the total number of "type" combination is massive for any but
the "smallest" cases.

I'd advise not to do that. Ever. Separate wire-(disk?) representation
from actual data you'd use in code. Only try to match the two in
lowest-level code dedicated to creation of non-wire data
representation. Do use, however, "internal" representation, for wire-
reading/writing purposes only; you still need to observe bit-count and
endiannes issues that Paavo mentions. For example "base" with counts
only is reasonable IMO. After that, go back to reading off to another
type of a buffer (Apples).

e.g.

// Internal. Matches wire-representation, sizes, layout, and endiannes
included.
// Platform-specific.
struct sizes
{
int capples, coranges; // "c" meant "count.
};

struct data
{
std::vector<apple> apples;
std::vector<oranges> oranges;
};

template<typename T>
read(stream& s, T* first, size_t count)
{
s.read(first, count * sizeof(*first));
}

template<typename T>
read(stream& s, vector<T>& v)
{
read(&v[0], v.size());
}

data read(stream& str)
{
sizes s;
str.read(&s, sizeof s);
data result;
result.apples.resize(s.capples);
read(s, result.apples);
result.oranges.resize(s.coranges);
read(s, result.oranges);
}

Goran.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List as a dynamic array of increasing-sized arrays MartinBroadhurst C Programming 13 11-03-2010 12:05 PM
Dynamic sized array? Daniel T. C++ 4 10-17-2006 12:19 PM
Oddly sized sized password textbox thechaosengine ASP .Net 1 09-15-2005 07:34 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments