1 // Written in the D programming language.
2 /*
3 NYSL Version 0.9982
4 
5 A. This software is "Everyone'sWare". It means:
6   Anybody who has this software can use it as if he/she is
7   the author.
8 
9   A-1. Freeware. No fee is required.
10   A-2. You can freely redistribute this software.
11   A-3. You can freely modify this software. And the source
12       may be used in any software with no limitation.
13   A-4. When you release a modified version to public, you
14       must publish it with your name.
15 
16 B. The author is not responsible for any kind of damages or loss
17   while using or misusing this software, which is distributed
18   "AS IS". No warranty of any kind is expressed or implied.
19   You use AT YOUR OWN RISK.
20 
21 C. Copyrighted to Kazuki KOMATSU
22 
23 D. Above three clauses are applied both to source and binary
24   form of this software.
25 */
26 
27 /**
28 このモジュールではstd.typecons.Tupleとは違った無名型を構築します。
29 */
30 module carbon.nonametype;
31 
32 
33 auto refT(alias var)() @safe
34 {
35     static struct RefT
36     {
37         auto ref get() pure nothrow @safe @nogc @property { return var; }
38         alias get this;
39     }
40 
41     return RefT();
42 }
43 
44 
45 auto refP(T)(T* p) @safe
46 {
47     static struct RefP
48     {
49         auto ref get() pure nothrow @safe @nogc inout @property { return *_p; }
50         alias get this;
51         private T* _p;
52     }
53 
54     return RefP(p);
55 }
56 
57 
58 auto scopeRef(string str = "system", T)(ref T v)
59 if(str == "system" || str == "trusted")
60 {
61     mixin(`return () @` ~ str ~ ` { return refP(&v); }();`);
62 }
63 
64 
65 @safe
66 unittest {
67     int a;
68     auto p = a.scopeRef!"trusted";
69     assert(p == 0);
70 
71     p = 12;
72     assert(a == 12);
73 
74     auto q = p;
75     ++q;
76     assert(a == p && a == 13);
77 }
78 
79 
80 class AssumeImplemented(C) : C
81 if(is(C == class))
82 {
83     import std.functional : forward;
84 
85     this(T...)(auto ref T args)
86     {
87         super(forward!args);
88     }
89 }
90 
91 
92 abstract class AssumeAbstract(C) : C
93 if(is(C == class))
94 {
95     import std.functional : forward;
96 
97     this(T...)(auto ref T args)
98     {
99         super(forward!args);
100     }
101 }
102 
103 
104 class Override(C, string method) : C
105 if(is(C == class) || is(C == interface))
106 {
107     import std.functional : forward;
108 
109     this(T...)(auto ref T args)
110     {
111         super(forward!args);
112     }
113 
114 
115     mixin("override " ~ method);
116 }
117 
118 unittest
119 {
120     static class C { this(){} int foo() { return 1; } }
121     C d = new Override!(C, "int foo(){ return 2; }");
122     assert(d.foo() == 2);
123 }
124 
125 
126 class Implement(C, string fields) : C
127 if(is(C == class) || is(C == interface))
128 {
129     import std.functional : forward;
130 
131     this(T...)(auto ref T args)
132     {
133         super(forward!args);
134     }
135 
136 
137     mixin(fields);
138 }
139 
140 unittest
141 {
142     static class C { this(){} }
143     auto d = new Implement!(C, "int foo(){ return 2; }")();
144     assert(d.foo() == 2);
145 }