2014-05-16 18:51:01 +08:00
|
|
|
//===-- MIUtilSingletonBase.h -----------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
namespace MI {
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2014-11-18 02:06:21 +08:00
|
|
|
// MI::ISingleton base class usage:
|
2014-05-16 18:51:01 +08:00
|
|
|
//
|
2014-11-18 02:06:21 +08:00
|
|
|
// class CMIDerivedClass
|
|
|
|
// : public MI::ISingleton< CMIDerivedClass >
|
|
|
|
// {
|
|
|
|
// friend MI::ISingleton< CMIDerivedClass >;
|
2014-05-16 18:51:01 +08:00
|
|
|
//
|
2014-11-18 02:06:21 +08:00
|
|
|
// // Overridden:
|
|
|
|
// public:
|
|
|
|
// // From MI::ISingleton
|
2015-08-04 18:24:20 +08:00
|
|
|
// bool Initialize() override;
|
|
|
|
// bool Shutdown() override;
|
2014-11-18 02:06:21 +08:00
|
|
|
// };
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//============================================================================
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Base class for the singleton pattern.
|
|
|
|
// Gotchas: Derived class must specify MI::ISingleton<> as a friend class.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
template <typename T> class ISingleton {
|
|
|
|
// Statics:
|
|
|
|
public:
|
|
|
|
// Return an instance of the derived class
|
|
|
|
static T &Instance() {
|
|
|
|
// This will fail if the derived class has not
|
|
|
|
// declared itself to be a friend of MI::ISingleton
|
|
|
|
static T instance;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return instance;
|
|
|
|
}
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Overrideable:
|
|
|
|
public:
|
|
|
|
virtual bool Initialize() = 0;
|
|
|
|
virtual bool Shutdown() = 0;
|
|
|
|
//
|
|
|
|
/* dtor */ virtual ~ISingleton() {}
|
2014-05-16 18:51:01 +08:00
|
|
|
};
|
|
|
|
|
2014-06-25 03:18:28 +08:00
|
|
|
} // namespace MI
|