1 2 //////////////////////////////////////////////////////////////////////////////////////////// 3 // Copyright (c) 2012 Christopher Nicholson-Sauls // 4 // // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy of this // 6 // software and associated documentation files (the "Software"), to deal in the // 7 // Software without restriction, including without limitation the rights to use, copy, // 8 // modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, // 9 // and to permit persons to whom the Software is furnished to do so, subject to the // 10 // following conditions: // 11 // // 12 // The above copyright notice and this permission notice shall be included in all // 13 // copies or substantial portions of the Software. // 14 // // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // 16 // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A // 17 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // 18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF // 19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE // 20 // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // 21 //////////////////////////////////////////////////////////////////////////////////////////// 22 23 module zeal.utils.singleton; 24 25 26 mixin template Singleton () { 27 mixin .SingletonBase; 28 } 29 30 mixin template Singleton ( alias Ctor ) { 31 mixin .SingletonBase; 32 33 protected this () { 34 static if ( is( typeof( super() ) ) ) { 35 super(); 36 } 37 Ctor(); 38 } 39 } 40 41 mixin template Singleton ( string CtorImpl ) { 42 mixin .SingletonBase; 43 44 protected this () { 45 static if ( is( typeof( super() ) ) ) { 46 super(); 47 } 48 mixin( CtorImpl ); 49 } 50 } 51 52 private mixin template SingletonBase () { 53 54 static @property RealClass instance ( this RealClass ) () { 55 static RealClass self; 56 57 if ( self is null ) { 58 self = new RealClass; 59 } 60 return self; 61 } 62 63 alias instance opCall; 64 65 }