digitalmars.D.learn - Policy-oriented programming.
- Gor F. Gyolchanyan (24/24) Sep 25 2011 Hi, D.learn!
- Philippe Sigaud (39/47) Sep 25 2011 I'm not sure I understand what you'd like your code to look like. This
- Gor F. Gyolchanyan (3/3) Sep 26 2011 Thanks! This is exactly what i wanted!
- Philippe Sigaud (5/8) Sep 26 2011 Keep us informed on your progress, because since I read "Modern C++
Hi, D.learn! I encountered a problem, while i was designing a fast, performance-critical image storage mechanism. Here's what i want to do: Have a FragmentTraits struct, which contains fields, like number of components, size in bytes of the components, padding of the components in bytes, etc. Have a Fragment struct, which takes a FragmentTraits object as a template parameter and defines the fragment components as it's fields according to given traits. Have a BufferTraits struct, which contains fields, like row padding in bytes, etc. Have a Buffer class, which takes a Fragment type and a BufferTraits object and defines appropriate storage and indexing routines. I know, structs are CTFE-able, so there should be no problem reading the fields of those compile-time defined struct objects. However, D does not allow me to have structs as template parameters and if I define them as alias parameters, it refuses to read the fields at compile-time. Having the user pass all those fields separately will lead to a very ugly API and having the user define a template, containing enum-constants as fields will lead to unnecessarily complicated and difficult interface and additional templates to test, whether the templates are defined correctly or not. How do i work around this limitation and are there any plans to include structs in the list of allowed template parameters?
Sep 25 2011
On Sun, Sep 25, 2011 at 16:49, Gor F. Gyolchanyan <gor.f.gyolchanyan gmail.com> wrote:Have a FragmentTraits struct, which contains fields, like number of components, size in bytes of the components, padding of the components in bytes, etc. Have a Fragment struct, which takes a FragmentTraits object as a template parameter and defines the fragment components as it's fields according to given traits.How do i work around this limitation and are there any plans to include structs in the list of allowed template parameters?I'm not sure I understand what you'd like your code to look like. This works for me (DMD 2.055): struct FragmentTraits { uint componentsNumber; uint componentsSize; } struct Fragment(alias trait) if (is(typeof(trait) == FragmentTraits)) { mixin(getTraits!trait); } string getTraits(alias trait)() if (is(typeof(trait) == FragmentTraits)) { return selectSize(trait.componentsSize) ~ "[" ~ to!string(trait.componentsNumber) ~ "] components;"; } string selectSize(uint size) { if (size == 1) return "byte"; if (size <= 2) return "short"; if (size <= 4) return "int"; if (size <= 8) return "long"; assert(0, "Bad component size: " ~ to!string(size) ~ ". Size must be between 1 and 8."); } void main(string[] args) { enum f = FragmentTraits(100,2); Fragment!f ff; // ff will contain a "short[100] components" member. writeln(typeof(ff.components).stringof); }
Sep 25 2011
Thanks! This is exactly what i wanted! Seems like the problem of not being able to pass struct template parameters can be worked around by putting the struct attribute access in a mixin.
Sep 26 2011
On Mon, Sep 26, 2011 at 11:19, Gor F. Gyolchanyan <gor.f.gyolchanyan gmail.com> wrote:Thanks! This is exactly what i wanted! Seems like the problem of not being able to pass struct template parameters can be worked around by putting the struct attribute access in a mixin.Keep us informed on your progress, because since I read "Modern C++ Design", I wondered how to do that in D (and if that is indeed idiomatic in D).
Sep 26 2011