www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Templates for structures

reply "novice2" <sorry noem.ail> writes:
Hello.

I need write some wrapper around legacy data structure.
May be it should be class. May be structure with methods.
The problem is writing repetitive code for underlying data.
For example:
  - code to read length-byte-prefixed string to D string for every 
field in every structure;
  - code to write D string back to length-byte-prefixed string;

Is it possible to write some template or mixin, and then just 
write
myStruct.addFiled(type=ByteLengthString, name="name")
myStruct.addFiled(type=ShortLengthString, name="filed2")
myStruct.addFiled(type=WeirdStoredInteger, name="counter")

May be it can be done with template, mixin, UDA, ...?

May be something already realized, and i can see sources as 
example?

Any ideas or examples please.
Just show direction for me )

Thanx.
Nov 02 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/02/2014 04:10 AM, novice2 wrote:
 Hello.

 I need write some wrapper around legacy data structure.
 May be it should be class. May be structure with methods.
 The problem is writing repetitive code for underlying data.
 For example:
   - code to read length-byte-prefixed string to D string for every field
 in every structure;
   - code to write D string back to length-byte-prefixed string;

 Is it possible to write some template or mixin, and then just write
 myStruct.addFiled(type=ByteLengthString, name="name")
 myStruct.addFiled(type=ShortLengthString, name="filed2")
 myStruct.addFiled(type=WeirdStoredInteger, name="counter")

 May be it can be done with template, mixin, UDA, ...?

 May be something already realized, and i can see sources as example?

 Any ideas or examples please.
 Just show direction for me )

 Thanx.
It sounds possible but I don't understand it yet. Can you give an example of the input and output to the D code? Ali
Nov 03 2014
parent "novice2" <sorry noem.ail> writes:
On Monday, 3 November 2014 at 14:53:29 UTC, Ali Çehreli wrote:
 It sounds possible but I don't understand it yet. Can you give 
 an example of the input and output to the D code?

 Ali
Thank you Ali. I realized, that my wishes look like serialization. So i decide read and learn code from existent serialization libraries.
Nov 04 2014