Hey,
I recently read an interesting blog Why should I have written ZeroMQ in C, not C++ (part II) by Martin Sústrik. The title is misleading; to me its main observation is that object oriented program may not lead to the most performant implementation. After reading the blog I asked myself I would I implement this in D? I thought we could use mixin and template mixin to implement this in a more manageable/scalable way. The complete source code is at Embedded Container.
mixin template DoubleLinkable()
{
typeof(this) next;
typeof(this) prev;
}
T* next(T, string name)(T node) pure nothrow const
{
mixin("return &(node." ~ name ~ ".next);");
}
T* prev(T, string name)(T node) pure nothrow const
{
mixin("return &(node." ~ name ~ ".prev);");
}
class Person
{
int age;
int weight;
mixin DoubleLinkable people;
}
void main()
{
auto list = new DoubleLinkedList!(Person, "people")();
auto person1 = new Person(6, 60);
list.pushFront(person1);
auto person2 = new Person(25, 160);
list.pushFront(person2);
auto person3 = new Person(11, 100);
list.pushFront(person3);
list.erase(person2);
list.erase(person3);
list.erase(person1);
}