Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.54 KB | None | 0 0
  1. /+
  2.  
  3.  +  This is an exercise in ranges.
  4.  +/
  5.  
  6. private enum allTypesStackSize = 2 * 1024;
  7.  
  8. struct AllTypes(alias types)
  9.  
  10. {
  11.  
  12.     struct Frame
  13.  
  14.     {
  15.  
  16.         private
  17.  
  18.         {
  19.  
  20.             MetaType[] _types;
  21.  
  22.             typeof(this)* _next;        
  23.  
  24.             void* _stackBottom;
  25.  
  26.         }
  27.  
  28.    
  29.  
  30.         private void allocNext()
  31.  
  32.         {            
  33.  
  34.             _next = cast(typeof(this)*)(cast(void*)&this - this.sizeof);            
  35.  
  36.             if (_next < _stackBottom)
  37.  
  38.                 onRangeError(__FILE__, __LINE__);
  39.  
  40.                
  41.  
  42.             _next.__ctor(_types[0], _stackBottom);
  43.  
  44.         }
  45.  
  46.  
  47.  
  48.         private void freeNext()
  49.  
  50.         {
  51.  
  52.             _next = null;
  53.  
  54.         }
  55.  
  56.  
  57.  
  58.         this(MetaType type, void* stackBottom)
  59.  
  60.         {
  61.  
  62.             _types = *cast(MetaType[]*)(cast(void*)type + offsetOf!types);
  63.  
  64.             _next = null;
  65.  
  66.             _stackBottom = stackBottom;
  67.  
  68.             writeln("Allocating frame for ", type.name, " ", _types, " ", cast(void*)&this);
  69.  
  70.         }
  71.  
  72.  
  73.  
  74.         @property MetaType front()
  75.  
  76.         {            
  77.  
  78.             return _next ? _next.front() : _types[0];
  79.  
  80.         }
  81.  
  82.  
  83.  
  84.         void popFront()
  85.  
  86.         {        
  87.  
  88.             if (!_next)
  89.  
  90.             {
  91.  
  92.                 assert(_types.length);
  93.  
  94.                 if ((*cast(MetaType[]*)(cast(void*)_types[0] + offsetOf!types)).length)
  95.  
  96.                     allocNext();                                                          
  97.  
  98.                 _types = _types[1..$];
  99.  
  100.             }
  101.  
  102.             else
  103.  
  104.             {
  105.  
  106.                 _next.popFront();
  107.  
  108.                 if (_next.empty)
  109.  
  110.                     freeNext();                
  111.  
  112.             }
  113.  
  114.         }
  115.  
  116.    
  117.  
  118.         @property bool empty()
  119.  
  120.         {
  121.  
  122.             if (_next && !_next.empty())
  123.  
  124.                 return false;
  125.  
  126.  
  127.  
  128.             return _types.length == 0;
  129.  
  130.         }
  131.  
  132.  
  133.  
  134.         /+
  135.  
  136.         @disable this(this)
  137.  
  138.         {
  139.  
  140.             assert(false);
  141.  
  142.         }
  143.  
  144.         +/
  145.  
  146.     }
  147.  
  148.  
  149.  
  150.     private Frame* _frame;
  151.  
  152.  
  153.  
  154.     @property Frame* frame()
  155.  
  156.     {
  157.  
  158.         return _frame;
  159.  
  160.     }
  161.  
  162.        
  163.  
  164.     this(MetaType type)
  165.  
  166.     {
  167.  
  168.         auto stackBottom = qtd_stack.alloc(allTypesStackSize);
  169.  
  170.         _frame = cast(Frame*)(stackBottom + allTypesStackSize - Frame.sizeof);
  171.  
  172.         _frame.__ctor(type, stackBottom);
  173.  
  174.     }
  175.  
  176.  
  177.  
  178.     alias frame this;
  179.  
  180.  
  181.  
  182.     ~this()
  183.  
  184.     {
  185.  
  186.         qtd_stack.free(allTypesStackSize);                  
  187.  
  188.     }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement