Newer
Older
dub_jkp / source / dub / internal / libInputVisitor.d
@Jan Jurzitza Jan Jurzitza on 7 Dec 2020 1 KB Normalize line endings & UTF8 BOMs
  1. module dub.internal.libInputVisitor;
  2.  
  3. version (Have_libInputVisitor) public import libInputVisitor;
  4. else:
  5.  
  6. /++
  7. Copyright (C) 2012 Nick Sabalausky <http://semitwist.com/contact>
  8.  
  9. This program is free software. It comes without any warranty, to
  10. the extent permitted by applicable law. You can redistribute it
  11. and/or modify it under the terms of the Do What The Fuck You Want
  12. To Public License, Version 2, as published by Sam Hocevar. See
  13. http://www.wtfpl.net/ for more details.
  14.  
  15. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  16. Version 2, December 2004
  17.  
  18. Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
  19.  
  20. Everyone is permitted to copy and distribute verbatim or modified
  21. copies of this license document, and changing it is allowed as long
  22. as the name is changed.
  23.  
  24. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  25. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  26.  
  27. 0. You just DO WHAT THE FUCK YOU WANT TO.
  28. +/
  29.  
  30. /++
  31. Should work with DMD 2.059 and up
  32.  
  33. For more info on this, see:
  34. http://semitwist.com/articles/article/view/combine-coroutines-and-input-ranges-for-dead-simple-d-iteration
  35. +/
  36.  
  37. import core.thread;
  38.  
  39. class InputVisitor(Obj, Elem) : Fiber
  40. {
  41. bool started = false;
  42. Obj obj;
  43. this(Obj obj)
  44. {
  45. this.obj = obj;
  46. super(&run);
  47. }
  48.  
  49. private void run()
  50. {
  51. obj.visit(this);
  52. }
  53.  
  54. private void ensureStarted()
  55. {
  56. if(!started)
  57. {
  58. call();
  59. started = true;
  60. }
  61. }
  62.  
  63. // Member 'front' must be a function due to DMD Issue #5403
  64. private Elem _front;
  65. @property Elem front()
  66. {
  67. ensureStarted();
  68. return _front;
  69. }
  70.  
  71. void popFront()
  72. {
  73. ensureStarted();
  74. call();
  75. }
  76.  
  77. @property bool empty()
  78. {
  79. ensureStarted();
  80. return state == Fiber.State.TERM;
  81. }
  82.  
  83. void yield(Elem elem)
  84. {
  85. _front = elem;
  86. Fiber.yield();
  87. }
  88. }
  89.  
  90. template inputVisitor(Elem)
  91. {
  92. @property InputVisitor!(Obj, Elem) inputVisitor(Obj)(Obj obj)
  93. {
  94. return new InputVisitor!(Obj, Elem)(obj);
  95. }
  96. }