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