Policy-oriented programming.
Philippe Sigaud
philippe.sigaud at gmail.com
Sun Sep 25 12:11:36 PDT 2011
On Sun, Sep 25, 2011 at 16:49, Gor F. Gyolchanyan
<gor.f.gyolchanyan at 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);
}
More information about the Digitalmars-d-learn
mailing list