1076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian/*
2076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian * Copyright (C) 2007 The Android Open Source Project
3076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian *
4076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian * you may not use this file except in compliance with the License.
6076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian * You may obtain a copy of the License at
7076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian *
8076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian *
10076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian * Unless required by applicable law or agreed to in writing, software
11076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian * See the License for the specific language governing permissions and
14076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian * limitations under the License.
15076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian */
16076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
17076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#ifndef ANDROID_UTILS_SINGLETON_H
18076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#define ANDROID_UTILS_SINGLETON_H
19076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
20076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <stdint.h>
21076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <sys/types.h>
22076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <utils/threads.h>
235c78b6861fd4c9246cc88d37178a5567fb668f5bRomain Guy#include <cutils/compiler.h>
24076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
25076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopiannamespace android {
26076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian// ---------------------------------------------------------------------------
27076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
28076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopiantemplate <typename TYPE>
295c78b6861fd4c9246cc88d37178a5567fb668f5bRomain Guyclass ANDROID_API Singleton
30076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{
31076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopianpublic:
32076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    static TYPE& getInstance() {
33076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        Mutex::Autolock _l(sLock);
34076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        TYPE* instance = sInstance;
35076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        if (instance == 0) {
36076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian            instance = new TYPE();
37076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian            sInstance = instance;
38076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        }
39076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        return *instance;
40076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    }
414e0c1f27fdefdd9b0208ea64542c1e39d9327eb9Romain Guy
424e0c1f27fdefdd9b0208ea64542c1e39d9327eb9Romain Guy    static bool hasInstance() {
434e0c1f27fdefdd9b0208ea64542c1e39d9327eb9Romain Guy        Mutex::Autolock _l(sLock);
444e0c1f27fdefdd9b0208ea64542c1e39d9327eb9Romain Guy        return sInstance != 0;
454e0c1f27fdefdd9b0208ea64542c1e39d9327eb9Romain Guy    }
46076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
47076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopianprotected:
48076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    ~Singleton() { };
49076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    Singleton() { };
50076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
51076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopianprivate:
52076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    Singleton(const Singleton&);
53076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    Singleton& operator = (const Singleton&);
54076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    static Mutex sLock;
55076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    static TYPE* sInstance;
56076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian};
57076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
589f88afb013a7560bf1362d7999a4609e38d0ea77Mathias Agopian/*
599f88afb013a7560bf1362d7999a4609e38d0ea77Mathias Agopian * use ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) in your implementation file
609f88afb013a7560bf1362d7999a4609e38d0ea77Mathias Agopian * (eg: <TYPE>.cpp) to create the static instance of Singleton<>'s attributes,
619f88afb013a7560bf1362d7999a4609e38d0ea77Mathias Agopian * and avoid to have a copy of them in each compilation units Singleton<TYPE>
629f88afb013a7560bf1362d7999a4609e38d0ea77Mathias Agopian * is used.
63b8510b98b5eb92333157db1359df6f2bbbfb2b39Mathias Agopian * NOTE: we use a version of Mutex ctor that takes a parameter, because
64b8510b98b5eb92333157db1359df6f2bbbfb2b39Mathias Agopian * for some unknown reason using the default ctor doesn't emit the variable!
659f88afb013a7560bf1362d7999a4609e38d0ea77Mathias Agopian */
669f88afb013a7560bf1362d7999a4609e38d0ea77Mathias Agopian
67b8510b98b5eb92333157db1359df6f2bbbfb2b39Mathias Agopian#define ANDROID_SINGLETON_STATIC_INSTANCE(TYPE)                 \
68b8510b98b5eb92333157db1359df6f2bbbfb2b39Mathias Agopian    template<> Mutex Singleton< TYPE >::sLock(Mutex::PRIVATE);  \
69114e968482a02b9153d9a236376efb5cd43f4a9aTareq A. Siraj    template<> TYPE* Singleton< TYPE >::sInstance(0);           \
70114e968482a02b9153d9a236376efb5cd43f4a9aTareq A. Siraj    template class Singleton< TYPE >;
719f88afb013a7560bf1362d7999a4609e38d0ea77Mathias Agopian
729f88afb013a7560bf1362d7999a4609e38d0ea77Mathias Agopian
73076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian// ---------------------------------------------------------------------------
74076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}; // namespace android
75076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
76076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#endif // ANDROID_UTILS_SINGLETON_H
77076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
78