1 /++
2 Copyright (C) 2012 Nick Sabalausky <http://semitwist.com/contact>
3 4 This program is free software. It comes without any warranty, to
5 the extent permitted by applicable law. You can redistribute it
6 and/or modify it under the terms of the Do What The Fuck You Want
7 To Public License, Version 2, as published by Sam Hocevar. See
8 http://www.wtfpl.net/ for more details.
9 10 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11 Version 2, December 2004
12 13 Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
14 15 Everyone is permitted to copy and distribute verbatim or modified
16 copies of this license document, and changing it is allowed as long
17 as the name is changed.
18 19 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
20 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
21 22 0. You just DO WHAT THE FUCK YOU WANT TO.
23 +/24 25 /++
26 Should work with DMD 2.059 and up
27 28 For more info on this, see:
29 http://semitwist.com/articles/article/view/combine-coroutines-and-input-ranges-for-dead-simple-d-iteration
30 +/31 modulegfx.decl.sdlang.libInputVisitor;
32 33 importcore.thread;
34 35 classInputVisitor(Obj, Elem) : Fiber36 {
37 boolstarted = false;
38 Objobj;
39 this(Objobj)
40 {
41 this.obj = obj;
42 43 version(Windows) // Issue #144 {
45 importcore.sys.windows.windows : SYSTEM_INFO, GetSystemInfo;
46 SYSTEM_INFOinfo;
47 GetSystemInfo(&info);
48 autoPAGESIZE = info.dwPageSize;
49 50 super(&run, PAGESIZE * 16);
51 }
52 else53 super(&run);
54 }
55 56 this(Objobj, size_tstackSize)
57 {
58 this.obj = obj;
59 super(&run, stackSize);
60 }
61 62 privatevoidrun()
63 {
64 obj.visit(this);
65 }
66 67 privatevoidensureStarted()
68 {
69 if(!started)
70 {
71 call();
72 started = true;
73 }
74 }
75 76 // Member 'front' must be a function due to DMD Issue #540377 privateElem_front = Elem.init; // Default initing here avoids "Error: field _front must be initialized in constructor"78 @propertyElemfront()
79 {
80 ensureStarted();
81 return_front;
82 }
83 84 voidpopFront()
85 {
86 ensureStarted();
87 call();
88 }
89 90 @propertyboolempty()
91 {
92 ensureStarted();
93 returnstate == Fiber.State.TERM;
94 }
95 96 voidyield(Elemelem)
97 {
98 _front = elem;
99 Fiber.yield();
100 }
101 }
102 103 templateinputVisitor(Elem)
104 {
105 @propertyInputVisitor!(Obj, Elem) inputVisitor(Obj)(Objobj)
106 {
107 returnnewInputVisitor!(Obj, Elem)(obj);
108 }
109 110 @propertyInputVisitor!(Obj, Elem) inputVisitor(Obj)(Objobj, size_tstackSize)
111 {
112 returnnewInputVisitor!(Obj, Elem)(obj, stackSize);
113 }
114 }