Changeset 1723

Show
Ignore:
Timestamp:
07/04/10 17:54:33 (2 years ago)
Author:
andrei
Message:

Changed broken opAssign to assign, plus minor changes to RefCounted?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/phobos/std/typecons.d

    r1676 r1723  
    5454import core.stdc.stdlib, std.algorithm, std.array, std.contracts, std.conv, 
    5555    std.metastrings, std.traits, std.typetuple, core.memory; 
     56version(unittest) import std.stdio; 
    5657 
    5758/** 
     
    323324    // @@@BUG 2800 
    324325    //alias field this; 
    325      
     326 
    326327/** 
    327328   Default constructor. 
     
    329330    this(U...)(U values) if (U.length == 0) 
    330331    { 
    331          
    332332    } 
    333333 
     
    367367                ~typeof(this).stringof 
    368368                ~" 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; 
    372372        } 
    373373        return true; 
     
    383383                ~typeof(this).stringof 
    384384                ~" 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            } 
    388391        } 
    389392        return 0; 
    390393    } 
    391394 
     395// Should really be opAssign, not assign 
    392396/** 
    393397   Assignment from another tuple. Each element of the source must be 
    394398   implicitly assignable to the respective element of the target. 
    395399 */ 
    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
    399403        { 
    400404            field[i] = rhs.field[i]; 
    401405        } 
    402406    } 
     407 
    403408/** 
    404409   Takes a slice of the tuple. 
     
    481486    assert(nosh.toString == "Tuple!(int,int)(5, 0)", nosh.toString); 
    482487    Tuple!(int, short) yessh; 
    483     nosh = yessh
     488    nosh.assign(yessh)
    484489 
    485490    Tuple!(int, "a", float, "b") x; 
     
    20322037if (!is(T == class)) 
    20332038{ 
    2034     private Tuple!(T, "payload_", size_t, "count_") * refCountedStore_
     2039    private Tuple!(T, "_payload", size_t, "_count") * _refCountedStore
    20352040 
    20362041    private void refCountedInitialize(A...)(A args) 
    20372042    { 
    2038    const sz = (*refCountedStore_).sizeof; 
     2043        const sz = (*_refCountedStore).sizeof; 
    20392044        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); 
    20422047        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; 
    20452050    } 
    20462051 
     
    20512056    @property bool refCountedIsInitialized() const 
    20522057    { 
    2053         return refCountedStore_ !is null; 
     2058        return _refCountedStore !is null; 
    20542059    } 
    20552060 
     
    20602065    void refCountedEnsureInitialized() 
    20612066    { 
    2062         if (refCountedIsInitialized()) return; 
     2067        if (refCountedIsInitialized) return; 
    20632068        refCountedInitialize(); 
    20642069    } 
     
    20812086    { 
    20822087        if (!refCountedIsInitialized) return; 
    2083         ++refCountedStore_.count_
     2088        ++_refCountedStore._count
    20842089    } 
    20852090 
     
    20922097    ~this() 
    20932098    { 
    2094         if (!refCountedStore_ || --refCountedStore_.count_) return; 
     2099        if (!_refCountedStore || --_refCountedStore._count) return; 
    20952100        // 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; 
    21012106    } 
    21022107 
     
    21042109Assignment operators 
    21052110 */ 
    2106     void opAssign(RefCounted!T rhs) 
    2107     { 
    2108         swap(refCountedStore_, rhs.refCountedStore_); 
     2111    void opAssign(RefCounted rhs) 
     2112    { 
     2113        swap(_refCountedStore, rhs._refCountedStore); 
    21092114    } 
    21102115 
     
    21402145            assert(refCountedIsInitialized); 
    21412146        } 
    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; 
    21432163    } 
    21442164} 
     
    21512171        p = &rc1; 
    21522172        assert(rc1 == 5); 
    2153         assert(rc1.refCountedStore_.count_ == 1); 
     2173        assert(rc1._refCountedStore._count == 1); 
    21542174        auto rc2 = rc1; 
    2155         assert(rc1.refCountedStore_.count_ == 2); 
     2175        assert(rc1._refCountedStore._count == 2); 
    21562176        // Reference semantics 
    21572177        rc2 = 42; 
    21582178        assert(rc1 == 42); 
    21592179        rc2 = rc2; 
    2160         assert(rc2.refCountedStore_.count_ == 2); 
     2180        assert(rc2._refCountedStore._count == 2); 
    21612181        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