help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Syntactic sugar for complex variable-length aggregates


From: spacewrench
Subject: Syntactic sugar for complex variable-length aggregates
Date: Mon, 22 Dec 2008 22:19:26 -0800 (PST)
User-agent: G2/1.0

I'm working on a USB device framework for an ARM microcontroller.  USB
uses a lot of variable-length structures, and I'd like to be able to
declare them in my programs without calling constructors, allocating
memory, etc.  (Essentially, all the info is available at compile time;
I just need to trick the compiler into putting it in the right place
and doing type-checking.)  The structures are conceptually similar to
Pascal strings, where you have a length byte, followed by the
appropriate number of characters, but some of the USB aggregates use
wchar_t.  Furthermore, some of the structures are referred to by an
integer index, rather than directly by address.  And...since the
strings are often const, it would be nice to have them stored
in .rodata.

Here's what I'd like to do:

struct StringDescriptor {
  unsigned len;
  wchar_t str[0];
} __attribute__ (( packed ));

struct DeviceDescriptor {
  // ...
  int MfgIndex;
  // ...
} ;

DeviceDescriptor myDev = { ... , StringDescriptor( "Acme
Industries" ), ... } ;

This would turn into some bytes in Flash containing 30 (== strlen
("Acme Industries") * sizeof(wchar_t)) and "A\0c\0m\0e\0 ....".  The
address of those bytes would be in an array of StringDescriptor
pointers, and the index of the address in the array would go into the
MfgIndex field of myDev.  And this would all happen at compile time.

Any chance of accomplishing this with gcc/g++, gas & gnu-ld?  (I've
gotten partway there with a cpp macro and asm() block, but perhaps
there's a better way?)  I'm sorely tempted to use a Perl preprocessor,
but down that path lies madness...

Thanks!


reply via email to

[Prev in Thread] Current Thread [Next in Thread]