C Struct Support
- Posted by mattlewis (admin) Dec 29, 2010
- 2094 views
Now that 4.0 is out, it's time to start thinking about...4.1! In fact, there is a wiki page with a high level overview of what's being planned:
One of the items on that list is for support for dealing with C-style structures in memory (not to be confused with data structures of native euphoria objects). Anyone who has dealt with an external API has probably needed to deal with these, managing offsets and data formats, peeking and poking...The goal is to make all of this easier for the euphoria programmer.
I've started playing around with some ideas, and put the code up into a mercurial repository here:
http://scm.openeuphoria.org/hg/mem_struct/
(You can also get it zipped: http://scm.openeuphoria.org/hg/mem_struct/archive/tip.zip )
That code has a couple of preprocessors for declaring and using structures, as well as a support library that implements the structures. This is really just something for testing the syntax, etc. A final implementation would probably be built into euphoria. There's a README with some documentation explaining some of the ideas more fully than this post, along with some examples.
In any case, it allows the coder to build structures, such as:
mem_struct numbers integer(8) byte integer(16) word integer(32) dword integer(64) longlong atom(32) float atom(64) double end mem_struct mem_union int_or_float integer(32) int atom(32) float end mem_union mem_struct wc_str pointer( integer( 16, array, null_terminated ) ) wchars end mem_struct
...and then to be used like:
atom example = allocate( sizeof("basic:numbers") ) example.basic:numbers.byte = 1 example.basic:numbers.word = 2 example.basic:numbers.dword = 3 example.basic:numbers.longlong = 4 example.basic:numbers.float = 5.5 example.basic:numbers.double = 6.6 printf( 1, "byte: %g\n", example.basic:numbers.byte ) printf( 1, "word: %g\n", example.basic:numbers.word ) printf( 1, "dword: %g\n", example.basic:numbers.dword ) printf( 1, "longlong: %g\n", example.basic:numbers.longlong ) printf( 1, "float: %g\n", example.basic:numbers.float ) printf( 1, "double: %g\n", example.basic:numbers.double )
The namespacing used here is a bit of a hack to make the preprocessors easier to build (please don't get hung up on that aspect).
I'd like to get some discussion going about this topic (but please feel free to ask questions or make other suggestions about other 4.1 topics).
Matt
Forked into: EUPHORIA struct support