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 19 #ifndef _LIBS_RGA_SINGLETON_H 20 #define _LIBS_RGA_SINGLETON_H 21 22 #ifndef ANDROID 23 #include "RgaMutex.h" 24 25 #if defined(__clang__) 26 #pragma clang diagnostic push 27 #pragma clang diagnostic ignored "-Wundefined-var-template" 28 #endif 29 30 template <typename TYPE> 31 class Singleton { 32 public: getInstance()33 static TYPE& getInstance() { 34 Mutex::Autolock _l(sLock); 35 TYPE* instance = sInstance; 36 if (instance == nullptr) { 37 instance = new TYPE(); 38 sInstance = instance; 39 } 40 return *instance; 41 } 42 hasInstance()43 static bool hasInstance() { 44 Mutex::Autolock _l(sLock); 45 return sInstance != nullptr; 46 } 47 48 protected: ~Singleton()49 ~Singleton() { } Singleton()50 Singleton() { } 51 52 private: 53 Singleton(const Singleton&); 54 Singleton& operator = (const Singleton&); 55 static Mutex sLock; 56 static TYPE* sInstance; 57 }; 58 59 #if defined(__clang__) 60 #pragma clang diagnostic pop 61 #endif 62 63 #define RGA_SINGLETON_STATIC_INSTANCE(TYPE) \ 64 template<> ::Mutex \ 65 (::Singleton< TYPE >::sLock)(::Mutex::PRIVATE); \ 66 template<> TYPE* ::Singleton< TYPE >::sInstance(nullptr); /* NOLINT */ \ 67 template class ::Singleton< TYPE >; 68 69 #endif //ANDROID 70 #endif //_LIBS_RGA_SINGLETON_H 71