Advertisement
Danol

Untitled

Jul 27th, 2015
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.22 KB | None | 0 0
  1. class P {
  2.  
  3. public:
  4.     this() {
  5.         import std.stdio;
  6.         writeln( "P constructor" );
  7.     }
  8.  
  9. }
  10.  
  11. class C : P {
  12.  
  13. public:
  14.     alias instance = T!( typeof( this ) );
  15.  
  16. public:
  17.     int i;
  18.  
  19. public:
  20.     this() {
  21.         import std.stdio;
  22.         writeln( "C constructor" );
  23.  
  24.         i = 10;
  25.     }
  26.     void test() {
  27.         import std.stdio;
  28.         writeln( "TEST" );
  29.     }
  30.  
  31. }
  32.  
  33. template T( Type ) {
  34.     static __gshared Type T;
  35.  
  36.     mixin _moduleInit!({
  37.             import std.stdio;
  38.             writeln("moduleInit called");
  39.             T = new Type();
  40.             T.test();
  41.         });
  42. }
  43.  
  44. /// Registers a function #func to the _moduleInitList. All functions registered are then called using _moduleInit__call (recommended to put into your main function).
  45. mixin template _moduleInit( alias func )
  46.     if( is( typeof( func ) : void function() ) )
  47. {
  48.     private alias __moduleInit = __moduleInitRegisterer!func;
  49. }
  50.  
  51. private __gshared void function()[] _moduleInitList;
  52. template __moduleInitRegisterer( alias func ) {
  53.     shared static this() {
  54.         _moduleInitList ~= func;
  55.     }
  56. }
  57.  
  58. /// Calls all the function registered via _moduleInit.
  59. void _moduleInit__call() {
  60.     foreach( f; _moduleInitList ) f();
  61. }
  62.  
  63. void main() {
  64.     import std.stdio;
  65.     writeln("main called");
  66.  
  67.     _moduleInit__call();
  68.  
  69.     writeln( C.instance.i );
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement