Created
April 6, 2012 18:25
-
-
Save lomereiter/2321873 to your computer and use it in GitHub Desktop.
...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// It turns out that combination of std.conv.emplace and FFI::MemoryPointer.new | |
// can make our life much easier (at least, for structs). | |
// See example at https://gist.github.com/2337729 | |
extern (C) void* malloc(size_t sz); | |
extern (C) void free(void *p); | |
class DefaultMemoryManagement { | |
mixin template memoryManagement() {} | |
} | |
class ManualMemoryManagement { | |
mixin template memoryManagement() { | |
new(size_t sz) | |
{ | |
void* p; | |
p = malloc(sz); | |
return p; | |
} | |
delete(void* p) | |
{ | |
free(p); | |
} | |
} | |
} | |
class TFileParser(T) | |
{ | |
mixin T.memoryManagement; | |
} | |
alias TFileParser!DefaultMemoryManagement FileParser; | |
alias TFileParser!ManualMemoryManagement FFIFileParser; | |
void main () { | |
auto f = new FileParser; | |
auto g = new FFIFileParser; delete g; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment