1 //===-- MIUtilSingletonBase.h -----------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #pragma once 11 12 namespace MI { 13 14 // MI::ISingleton base class usage: 15 // 16 // class CMIDerivedClass 17 // : public MI::ISingleton< CMIDerivedClass > 18 // { 19 // friend MI::ISingleton< CMIDerivedClass >; 20 // 21 // // Overridden: 22 // public: 23 // // From MI::ISingleton 24 // bool Initialize() override; 25 // bool Shutdown() override; 26 // }; 27 28 //++ 29 //============================================================================ 30 // Details: Base class for the singleton pattern. 31 // Gotchas: Derived class must specify MI::ISingleton<> as a friend class. 32 //-- 33 template <typename T> class ISingleton { 34 // Statics: 35 public: 36 // Return an instance of the derived class Instance()37 static T &Instance() { 38 // This will fail if the derived class has not 39 // declared itself to be a friend of MI::ISingleton 40 static T instance; 41 42 return instance; 43 } 44 45 // Overrideable: 46 public: 47 virtual bool Initialize() = 0; 48 virtual bool Shutdown() = 0; 49 // ~ISingleton()50 /* dtor */ virtual ~ISingleton() {} 51 }; 52 53 } // namespace MI 54