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 #include "RgaMutex.h" 23 24 #if defined(__clang__) 25 #pragma clang diagnostic push 26 #pragma clang diagnostic ignored "-Wundefined-var-template" 27 #endif 28 29 template <typename TYPE> 30 class RgaSingleton { 31 public: getInstance()32 static TYPE& getInstance() 33 { 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 } hasInstance()42 static bool hasInstance() 43 { 44 Mutex::Autolock _l(sLock); 45 return sInstance != nullptr; 46 } 47 48 protected: ~RgaSingleton()49 ~RgaSingleton() { } RgaSingleton()50 RgaSingleton() { } 51 52 private: 53 RgaSingleton(const RgaSingleton&); 54 RgaSingleton& operator = (const RgaSingleton&); 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) do { \ 64 template<> ::RgaMutex \ 65 (::RgaSingleton< TYPE >::sLock)(::RgaMutex::PRIVATE); \ 66 template<> TYPE* ::RgaSingleton< TYPE >::sInstance(nullptr); /* NOLINT */ \ 67 template class ::RgaSingleton< TYPE >; \ 68 } while (0) 69 70 #endif // _LIBS_RGA_SINGLETON_H 71