diff options
Diffstat (limited to 'source/tanya/memory/package.d')
| -rw-r--r-- | source/tanya/memory/package.d | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/source/tanya/memory/package.d b/source/tanya/memory/package.d index 3979fe0..9ecb45b 100644 --- a/source/tanya/memory/package.d +++ b/source/tanya/memory/package.d @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /** - * Copyright: Eugene Wissner 2016. + * Copyright: Eugene Wissner 2016-2017. * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, * Mozilla Public License, v. 2.0). * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) @@ -14,7 +14,69 @@ import core.exception; public import std.experimental.allocator : make, makeArray; import std.traits; public import tanya.memory.allocator; -public import tanya.memory.types; + +/** + * The mixin generates common methods for classes and structs using + * allocators. It provides a protected member, constructor and a read-only property, + * that checks if an allocator was already set and sets it to the default + * one, if not (useful for structs which don't have a default constructor). + */ +mixin template DefaultAllocator() +{ + /// Allocator. + protected shared Allocator allocator_; + + /** + * Params: + * allocator = The allocator should be used. + */ + this(shared Allocator allocator) + in + { + assert(allocator !is null); + } + body + { + this.allocator_ = allocator; + } + + /** + * This property checks if the allocator was set in the constructor + * and sets it to the default one, if not. + * + * Returns: Used allocator. + * + * Postcondition: $(D_INLINECODE allocator_ !is null) + */ + protected @property shared(Allocator) allocator() nothrow @safe @nogc + out (allocator) + { + assert(allocator !is null); + } + body + { + if (allocator_ is null) + { + allocator_ = defaultAllocator; + } + return allocator_; + } + + /// Ditto. + @property shared(Allocator) allocator() const nothrow @trusted @nogc + out (allocator) + { + assert(allocator !is null); + } + body + { + if (allocator_ is null) + { + return defaultAllocator; + } + return cast(shared Allocator) allocator_; + } +} // From druntime private extern (C) void _d_monitordelete(Object h, bool det) nothrow @nogc; @@ -28,11 +90,21 @@ shared static this() nothrow @trusted @nogc } @property ref shared(Allocator) defaultAllocator() nothrow @safe @nogc +out (allocator) +{ + assert(allocator !is null); +} +body { return allocator; } @property void defaultAllocator(shared(Allocator) allocator) nothrow @safe @nogc +in +{ + assert(allocator !is null); +} +body { .allocator = allocator; } |
