www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3517] New: Allocators proposal

http://d.puremagic.com/issues/show_bug.cgi?id=3517

           Summary: Allocators proposal
           Product: D
           Version: 2.036
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: k-foley onu.edu



Created an attachment (id=498)
proposed implementation and test/example

I'm proposing a standard way to define and pass around allocators.  My primary
motivation for proposing this is because I intend to later submit a containers
proposal that would then use this allocators structure, where an allocator
would be one of the parameters of a container type.

But allocators could also be directly usable through some create!(MyType)
syntax, as demonstrated in the example.  All criticism is welcome :)

If there's a better way to submit proposals like this, I want to know about it.
 The following is test.d from the attachment demonstrating how allocators could
be directly usable:

---

import std.stdio;

import memory.allocators.gc;
import memory.allocators.cmallocator;

// overload set: choose the gc
alias memory.allocators.gc.create create;
alias memory.allocators.gc.destroy destroy;

struct TestStruct
{
    int a = 42;
    int b = 23;

    ~this() { writeln("Struct Destructed"); }
}

interface TestInterface
{
    bool test();
}

class TestClass1 : TestInterface
{
    int a = 99;
    int b = 1337;
    string id = "";

    this(string rhs) { id = rhs; writeln(id, ".__ctor(string)"); }
    ~this() { writeln(id, ".__dtor()"); }

    void print() { writeln("a: ", a, " and b: ", b); }
    override bool test() { return a < 100; }
}

class TestClass2 : TestInterface
{
    int a = 23;

    override bool test() { return a < 20; }
}

int main()
{
    TestInterface aa = create!(TestClass1)("rawr");

    writeln("1: ", aa.test);

    with ( CMallocator_ClassCreator!TestClass1 )
    {
        auto a = create("CM First"); scope (exit) destroy(a);
        write("2: "); a.print;
    }

    with ( CMallocator_ClassCreator!TestClass2 )
    {
        aa = create(); scope(exit) destroy( cast(TestClass2)aa );
        writeln("3: ", (cast(TestClass2)aa).a, " < 20 is ", aa.test);
    }

    auto b = create!(TestStruct);
    auto bb = b;

    writeln(b.a);
    bb.a = 92;
    writeln(b.a);

    auto c = create!(TestClass2);
    c.a = 19;
    writeln("c.a = ", c.a);

    aa = c;
    writeln("5: c.a < 20 is ", aa.test);

    return 0;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 16 2009