Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class P {
- public:
- this() {
- import std.stdio;
- writeln( "P constructor" );
- }
- }
- class C : P {
- public:
- alias instance = T!( typeof( this ) );
- public:
- int i;
- public:
- this() {
- import std.stdio;
- writeln( "C constructor" );
- i = 10;
- }
- void test() {
- import std.stdio;
- writeln( "TEST" );
- }
- }
- template T( Type ) {
- static __gshared Type T;
- mixin _moduleInit!({
- import std.stdio;
- writeln("moduleInit called");
- T = new Type();
- T.test();
- });
- }
- /// Registers a function #func to the _moduleInitList. All functions registered are then called using _moduleInit__call (recommended to put into your main function).
- mixin template _moduleInit( alias func )
- if( is( typeof( func ) : void function() ) )
- {
- private alias __moduleInit = __moduleInitRegisterer!func;
- }
- private __gshared void function()[] _moduleInitList;
- template __moduleInitRegisterer( alias func ) {
- shared static this() {
- _moduleInitList ~= func;
- }
- }
- /// Calls all the function registered via _moduleInit.
- void _moduleInit__call() {
- foreach( f; _moduleInitList ) f();
- }
- void main() {
- import std.stdio;
- writeln("main called");
- _moduleInit__call();
- writeln( C.instance.i );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement