Skip to content

Instantly share code, notes, and snippets.

@lomereiter
Created April 6, 2012 18:25
Show Gist options
  • Save lomereiter/2321873 to your computer and use it in GitHub Desktop.
Save lomereiter/2321873 to your computer and use it in GitHub Desktop.
...
// 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