Changeset 1723
- Timestamp:
- 07/04/10 17:54:33 (2 years ago)
- Files:
-
- trunk/phobos/std/typecons.d (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/phobos/std/typecons.d
r1676 r1723 54 54 import core.stdc.stdlib, std.algorithm, std.array, std.contracts, std.conv, 55 55 std.metastrings, std.traits, std.typetuple, core.memory; 56 version(unittest) import std.stdio; 56 57 57 58 /** … … 323 324 // @@@BUG 2800 324 325 //alias field this; 325 326 326 327 /** 327 328 Default constructor. … … 329 330 this(U...)(U values) if (U.length == 0) 330 331 { 331 332 332 } 333 333 … … 367 367 ~typeof(this).stringof 368 368 ~" with a "~typeof(rhs).stringof); 369 foreach (i, f; field)370 { 371 if (f != rhs.field[i]) return false;369 foreach (i, Unused; Types) 370 { 371 if (field[i] != rhs.field[i]) return false; 372 372 } 373 373 return true; … … 383 383 ~typeof(this).stringof 384 384 ~" with a "~typeof(rhs).stringof); 385 foreach (i, f; field) 386 { 387 if (f != rhs.field[i]) return f < rhs.field[i] ? -1 : 1; 385 foreach (i, Unused; Types) 386 { 387 if (field[i] != rhs.field[i]) 388 { 389 return field[i] < rhs.field[i] ? -1 : 1; 390 } 388 391 } 389 392 return 0; 390 393 } 391 394 395 // Should really be opAssign, not assign 392 396 /** 393 397 Assignment from another tuple. Each element of the source must be 394 398 implicitly assignable to the respective element of the target. 395 399 */ 396 void opAssign(U)(U rhs) if (is(typeof(U.init.field[0])))397 { 398 foreach (i, Unused; noStrings!(T).Result)400 void assign(U)(U rhs) if (is(typeof(U.init.field[0]))) 401 { 402 foreach (i, Unused; Types) 399 403 { 400 404 field[i] = rhs.field[i]; 401 405 } 402 406 } 407 403 408 /** 404 409 Takes a slice of the tuple. … … 481 486 assert(nosh.toString == "Tuple!(int,int)(5, 0)", nosh.toString); 482 487 Tuple!(int, short) yessh; 483 nosh = yessh;488 nosh.assign(yessh); 484 489 485 490 Tuple!(int, "a", float, "b") x; … … 2032 2037 if (!is(T == class)) 2033 2038 { 2034 private Tuple!(T, " payload_", size_t, "count_") * refCountedStore_;2039 private Tuple!(T, "_payload", size_t, "_count") * _refCountedStore; 2035 2040 2036 2041 private void refCountedInitialize(A...)(A args) 2037 2042 { 2038 const sz = (*refCountedStore_).sizeof;2043 const sz = (*_refCountedStore).sizeof; 2039 2044 auto p = malloc(sz)[0 .. sz]; 2040 if (sz >= size_t.sizeof && p.ptr)2041 GC.addRange(p.ptr, sz);2045 if (sz >= size_t.sizeof && p.ptr) 2046 GC.addRange(p.ptr, sz); 2042 2047 emplace!T(p[0 .. T.sizeof], args); 2043 refCountedStore_ = cast(typeof(refCountedStore_)) p;2044 refCountedStore_.count_= 1;2048 _refCountedStore = cast(typeof(_refCountedStore)) p; 2049 _refCountedStore._count = 1; 2045 2050 } 2046 2051 … … 2051 2056 @property bool refCountedIsInitialized() const 2052 2057 { 2053 return refCountedStore_!is null;2058 return _refCountedStore !is null; 2054 2059 } 2055 2060 … … 2060 2065 void refCountedEnsureInitialized() 2061 2066 { 2062 if (refCountedIsInitialized ()) return;2067 if (refCountedIsInitialized) return; 2063 2068 refCountedInitialize(); 2064 2069 } … … 2081 2086 { 2082 2087 if (!refCountedIsInitialized) return; 2083 ++ refCountedStore_.count_;2088 ++_refCountedStore._count; 2084 2089 } 2085 2090 … … 2092 2097 ~this() 2093 2098 { 2094 if (! refCountedStore_ || --refCountedStore_.count_) return;2099 if (!_refCountedStore || --_refCountedStore._count) return; 2095 2100 // Done, deallocate 2096 clear(* refCountedStore_);2097 if ((*refCountedStore_).sizeof >= size_t.sizeof && refCountedStore_)2098 GC.removeRange(refCountedStore_);2099 free( refCountedStore_);2100 refCountedStore_= null;2101 clear(*_refCountedStore); 2102 if (hasIndirections!T && _refCountedStore) 2103 GC.removeRange(_refCountedStore); 2104 free(_refCountedStore); 2105 _refCountedStore = null; 2101 2106 } 2102 2107 … … 2104 2109 Assignment operators 2105 2110 */ 2106 void opAssign(RefCounted !Trhs)2107 { 2108 swap( refCountedStore_, rhs.refCountedStore_);2111 void opAssign(RefCounted rhs) 2112 { 2113 swap(_refCountedStore, rhs._refCountedStore); 2109 2114 } 2110 2115 … … 2140 2145 assert(refCountedIsInitialized); 2141 2146 } 2142 return refCountedStore_.payload_; 2147 return _refCountedStore._payload; 2148 } 2149 2150 // 2151 @property ref const(T) refCountedPayload() const { 2152 static if (autoInit == RefCountedAutoInitialize.yes) 2153 { 2154 // @@@ 2155 //refCountedEnsureInitialized(); 2156 assert(refCountedIsInitialized); 2157 } 2158 else 2159 { 2160 assert(refCountedIsInitialized); 2161 } 2162 return _refCountedStore._payload; 2143 2163 } 2144 2164 } … … 2151 2171 p = &rc1; 2152 2172 assert(rc1 == 5); 2153 assert(rc1. refCountedStore_.count_== 1);2173 assert(rc1._refCountedStore._count == 1); 2154 2174 auto rc2 = rc1; 2155 assert(rc1. refCountedStore_.count_== 2);2175 assert(rc1._refCountedStore._count == 2); 2156 2176 // Reference semantics 2157 2177 rc2 = 42; 2158 2178 assert(rc1 == 42); 2159 2179 rc2 = rc2; 2160 assert(rc2. refCountedStore_.count_== 2);2180 assert(rc2._refCountedStore._count == 2); 2161 2181 rc1 = rc2; 2162 assert(rc1.refCountedStore_.count_ == 2); 2163 } 2164 assert(p.refCountedStore_ == null); 2165 } 2166 2182 assert(rc1._refCountedStore._count == 2); 2183 } 2184 assert(p._refCountedStore == null); 2185 2186 // RefCounted as a member 2187 struct A 2188 { 2189 RefCounted!int x; 2190 this(int y) 2191 { 2192 x.refCountedInitialize(y); 2193 } 2194 A copy() 2195 { 2196 auto another = this; 2197 return another; 2198 } 2199 } 2200 auto a = A(4); 2201 auto b = a.copy(); 2202 if (a.x._refCountedStore._count != 2) { 2203 stderr.writeln("*** BUG 4356 still unfixed"); 2204 } 2205 } 2206
