Extensible struct type via variadic template arguments
ag0aep6g via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Apr 11 15:06:00 PDT 2016
On 10.04.2016 20:45, Joseph Rushton Wakeling wrote:
> One very basic idea is to allow graph edges to have arbitrary
> properties, and to do that, I've tried to come up with a basic data
> structure that can be extended via variadic template arguments.
> However, I'm not entirely satisfied with the design, as I suspect it can
> be simplified/made nicer. Here's what I have at present:
>
>
> //////////////////////////////////////////
[...]
> //////////////////////////////////////////
>
>
> There are two things I'm wondering about the above design:
>
> * is it possible to redesign EdgeProperty as a template that would take
> a type parameter directly rather than as a string, so one could write
> e.g. EdgeProperty!("weight", double) ... ?
>
> * is it possible to rework things to avoid the string mixins ...?
----
struct ExtensibleEdgeList (EdgePropertyList...)
{
public size_t[] tail;
public size_t[] head;
mixin multipleEdgeProperties!(EdgePropertyList);
}
template EdgeProperty(Type_, string name_)
{
alias Type = Type_;
enum string name = name_;
}
mixin template multipleEdgeProperties (EdgePropertyList...)
{
static if (EdgePropertyList.length >= 1)
{
mixin singleEdgeProperty!(EdgePropertyList[0]);
mixin multipleEdgeProperties!(EdgePropertyList[1 .. $]);
}
}
mixin template singleEdgeProperty (alias property)
{
mixin(`public property.Type[] ` ~ property.name ~ `;`);
}
----
The string mixin is hidden away in singleEdgeProperty, but it's still there.
To get rid of it, you'd have take a different approach, I think. For
example:
----
struct Foo
{
mixin EdgeListHeadTail;
EdgeProperty!double weight;
EdgeProperty!int bar;
}
mixin template EdgeListHeadTail ()
{
public size_t[] tail;
public size_t[] head;
}
alias EdgeProperty (Type) = Type[];
----
No code generation anymore. But now the user must not forget the mixin
of EdgeListHeadTail. And if you need to generate something from the
number of edge properties, then this way isn't feasible at all, of course.
More information about the Digitalmars-d-learn
mailing list