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> class RgaSingleton { 30 public: getInstance()31 static TYPE& getInstance() 32 { 33 RgaMutex::Autolock _l(sLock); 34 TYPE *instance = sInstance; 35 if (instance == nullptr) { 36 instance = new TYPE(); 37 sInstance = instance; 38 } 39 return *instance; 40 } hasInstance()41 static bool hasInstance() 42 { 43 RgaMutex::Autolock _l(sLock); 44 return sInstance != nullptr; 45 } 46 47 protected: ~RgaSingleton()48 ~RgaSingleton() 49 { 50 } RgaSingleton()51 RgaSingleton() 52 { 53 } 54 55 private: 56 RgaSingleton(const RgaSingleton &); 57 RgaSingleton &operator=(const RgaSingleton &); 58 static RgaMutex sLock; 59 static TYPE *sInstance; 60 }; 61 62 #if defined(__clang__) 63 #pragma clang diagnostic pop 64 #endif 65 66 #define RGA_SINGLETON_STATIC_INSTANCE(TYPE) \ 67 do { \ 68 template <>::RgaMutex(::RgaSingleton<TYPE>::sLock)(::RgaMutex::PRIVATE); \ 69 template <> TYPE * ::RgaSingleton<TYPE>::sInstance(nullptr); /* NOLINT */ \ 70 template class ::RgaSingleton<TYPE>; \ 71 } while (0) 72 73 #endif // _LIBS_RGA_SINGLETON_H 74