Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /+
- + This is an exercise in ranges.
- +/
- private enum allTypesStackSize = 2 * 1024;
- struct AllTypes(alias types)
- {
- struct Frame
- {
- private
- {
- MetaType[] _types;
- typeof(this)* _next;
- void* _stackBottom;
- }
- private void allocNext()
- {
- _next = cast(typeof(this)*)(cast(void*)&this - this.sizeof);
- if (_next < _stackBottom)
- onRangeError(__FILE__, __LINE__);
- _next.__ctor(_types[0], _stackBottom);
- }
- private void freeNext()
- {
- _next = null;
- }
- this(MetaType type, void* stackBottom)
- {
- _types = *cast(MetaType[]*)(cast(void*)type + offsetOf!types);
- _next = null;
- _stackBottom = stackBottom;
- writeln("Allocating frame for ", type.name, " ", _types, " ", cast(void*)&this);
- }
- @property MetaType front()
- {
- return _next ? _next.front() : _types[0];
- }
- void popFront()
- {
- if (!_next)
- {
- assert(_types.length);
- if ((*cast(MetaType[]*)(cast(void*)_types[0] + offsetOf!types)).length)
- allocNext();
- _types = _types[1..$];
- }
- else
- {
- _next.popFront();
- if (_next.empty)
- freeNext();
- }
- }
- @property bool empty()
- {
- if (_next && !_next.empty())
- return false;
- return _types.length == 0;
- }
- /+
- @disable this(this)
- {
- assert(false);
- }
- +/
- }
- private Frame* _frame;
- @property Frame* frame()
- {
- return _frame;
- }
- this(MetaType type)
- {
- auto stackBottom = qtd_stack.alloc(allTypesStackSize);
- _frame = cast(Frame*)(stackBottom + allTypesStackSize - Frame.sizeof);
- _frame.__ctor(type, stackBottom);
- }
- alias frame this;
- ~this()
- {
- qtd_stack.free(allTypesStackSize);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement