RgaSingleton.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (C) 2016 Rockchip Electronics Co., Ltd.
  3. * Authors:
  4. * Zhiqin Wei <wzq@rock-chips.com>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef _LIBS_RGA_SINGLETON_H
  19. #define _LIBS_RGA_SINGLETON_H
  20. #ifndef ANDROID
  21. #include "RgaMutex.h"
  22. #if defined(__clang__)
  23. #pragma clang diagnostic push
  24. #pragma clang diagnostic ignored "-Wundefined-var-template"
  25. #endif
  26. template <typename TYPE>
  27. class Singleton {
  28. public:
  29. static TYPE& getInstance() {
  30. Mutex::Autolock _l(sLock);
  31. TYPE* instance = sInstance;
  32. if (instance == nullptr) {
  33. instance = new TYPE();
  34. sInstance = instance;
  35. }
  36. return *instance;
  37. }
  38. static bool hasInstance() {
  39. Mutex::Autolock _l(sLock);
  40. return sInstance != nullptr;
  41. }
  42. protected:
  43. ~Singleton() { }
  44. Singleton() { }
  45. private:
  46. Singleton(const Singleton&);
  47. Singleton& operator = (const Singleton&);
  48. static Mutex sLock;
  49. static TYPE* sInstance;
  50. };
  51. #if defined(__clang__)
  52. #pragma clang diagnostic pop
  53. #endif
  54. #define RGA_SINGLETON_STATIC_INSTANCE(TYPE) \
  55. template<> ::Mutex \
  56. (::Singleton< TYPE >::sLock)(::Mutex::PRIVATE); \
  57. template<> TYPE* ::Singleton< TYPE >::sInstance(nullptr); /* NOLINT */ \
  58. template class ::Singleton< TYPE >;
  59. #endif //ANDROID
  60. #endif //_LIBS_RGA_SINGLETON_H