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 34 auto refT(alias var)() @safe 35 { 36 static struct RefT 37 { 38 auto ref get() pure nothrow @safe @nogc @property { return var; } 39 alias get this; 40 } 41 42 return RefT(); 43 } 44 45 46 auto refP(T)(T* p) @safe 47 { 48 static struct RefP 49 { 50 auto ref get() pure nothrow @safe @nogc inout @property { return *_p; } 51 alias get this; 52 private T* _p; 53 } 54 55 return RefP(p); 56 } 57 58 59 auto scopeRef(string str = "system", T)(ref T v) 60 if(str == "system" || str == "trusted") 61 { 62 mixin(`return () @` ~ str ~ ` { return refP(&v); }();`); 63 } 64 65 66 @safe 67 unittest { 68 int a; 69 auto p = a.scopeRef!"trusted"; 70 assert(p == 0); 71 72 p = 12; 73 assert(a == 12); 74 75 auto q = p; 76 ++q; 77 assert(a == p && a == 13); 78 } 79 80 81 class AssumeImplemented(C) : C 82 if(is(C == class)) 83 { 84 import std.functional : forward; 85 86 this(T...)(auto ref T args) 87 { 88 super(forward!args); 89 } 90 } 91 92 93 abstract class AssumeAbstract(C) : C 94 if(is(C == class)) 95 { 96 import std.functional : forward; 97 98 this(T...)(auto ref T args) 99 { 100 super(forward!args); 101 } 102 } 103 104 105 class Override(C, string method) : C 106 if(is(C == class) || is(C == interface)) 107 { 108 import std.functional : forward; 109 110 this(T...)(auto ref T args) 111 { 112 super(forward!args); 113 } 114 115 116 mixin("override " ~ method); 117 } 118 119 unittest 120 { 121 static class C { this(){} int foo() { return 1; } } 122 C d = new Override!(C, "int foo(){ return 2; }"); 123 assert(d.foo() == 2); 124 } 125 126 127 class Implement(C, string fields) : C 128 if(is(C == class) || is(C == interface)) 129 { 130 import std.functional : forward; 131 132 this(T...)(auto ref T args) 133 { 134 super(forward!args); 135 } 136 137 138 mixin(fields); 139 } 140 141 unittest 142 { 143 static class C { this(){} } 144 auto d = new Implement!(C, "int foo(){ return 2; }")(); 145 assert(d.foo() == 2); 146 }