• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 // -*- c++ -*-
19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
20 
21 //                     O S C L _ S I N G L E T O N
22 
23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
24 
25 /**
26  *  @file oscl_singleton.h
27  *  @brief This file defines the OsclSingleton class. This class
28  *         provides a container which used to give access to a set of
29  *         process-level singleton objects.  Each object is indexed
30  *         by an integer ID, listed below.  There can only be one instance of
31  *         each object per process at a given time.
32  *
33  *         OsclSingleton is initialized in OsclBase::Init.
34  *
35  */
36 
37 #ifndef OSCL_SINGLETON_H_INCLUDED
38 #define OSCL_SINGLETON_H_INCLUDED
39 
40 #ifndef OSCL_BASE_H_INCLUDED
41 #include "oscl_base.h"
42 #endif
43 
44 #ifndef OSCL_DEFALLOC_H_INCLUDED
45 #include "oscl_defalloc.h"
46 #endif
47 
48 
49 #if (OSCL_HAS_SINGLETON_SUPPORT)
50 
51 //verify config-- singleton support requires global var support
52 
53 // list of singleton objects
54 const uint32 OSCL_SINGLETON_ID_TEST           =  0;
55 const uint32 OSCL_SINGLETON_ID_OSCLMEM        =  1;
56 const uint32 OSCL_SINGLETON_ID_PVLOGGER       =  2;
57 const uint32 OSCL_SINGLETON_ID_PVSCHEDULER    =  3;
58 const uint32 OSCL_SINGLETON_ID_PVERRORTRAP    =  4;
59 const uint32 OSCL_SINGLETON_ID_SDPMEDIAPARSER =  5;
60 const uint32 OSCL_SINGLETON_ID_PAYLOADPARSER  =  6;
61 const uint32 OSCL_SINGLETON_ID_CPM_PLUGIN     =  7;
62 const uint32 OSCL_SINGLETON_ID_PVMFRECOGNIZER =  8;
63 const uint32 OSCL_SINGLETON_ID_OSCLREGISTRY   =  9;
64 const uint32 OSCL_SINGLETON_ID_OMX            = 10;
65 const uint32 OSCL_SINGLETON_ID_OMXMASTERCORE  = 11;
66 const uint32 OSCL_SINGLETON_ID_TICKCOUNT      = 12;
67 const uint32 OSCL_SINGLETON_ID_LAST           = 13;
68 
69 
70 class OsclSingletonRegistry
71 {
72     public:
73         /*
74         ** Get an entry
75         ** @param ID: identifier
76         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
77         ** @returns: the entry value
78         */
79         OSCL_IMPORT_REF static OsclAny* getInstance(uint32 ID, int32 &error);
80         /*
81         ** Set an entry
82         ** @param ID: identifier
83         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
84         ** @returns: the entry value
85         */
86         OSCL_IMPORT_REF static void registerInstance(OsclAny* ptr, uint32 ID, int32 &error);
87 
88         /*
89         //These two APIs can be used to do "test and set" operations on a singleton.
90         //Be sure to always call both APIs to avoid deadlock.
91         */
92 
93         /*
94         * Return the current value of the singleton and leave the singleton table locked
95         * on return.
96         * @param ID the singleton ID
97         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
98         * @returns the singleton value.
99         */
100         OSCL_IMPORT_REF static OsclAny* lockAndGetInstance(uint32 ID, int32& error);
101         /*
102         * Set the value of the singleton.  Assume the singleton table is locked on entry.
103         * @param ptr the singleton value
104         * @param ID the singleton ID
105         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
106         */
107         OSCL_IMPORT_REF static void registerInstanceAndUnlock(OsclAny* ptr, uint32 ID, int32& error);
108 
109     private:
OsclSingletonRegistry()110         OsclSingletonRegistry()
111         {}
112         typedef OsclAny* registry_type;
113         typedef registry_type* registry_pointer_type;
114 
115     private:
116         // FIXME:
117         // these methods are obsolete and can be removed
initialize(Oscl_DefAlloc & alloc,int32 & error)118         OSCL_IMPORT_REF static void initialize(Oscl_DefAlloc &alloc, int32 &error)
119         {
120             error = 0;
121         }
cleanup(Oscl_DefAlloc & alloc,int32 & error)122         OSCL_IMPORT_REF static void cleanup(Oscl_DefAlloc &alloc, int32 &error)
123         {
124             error = 0;
125         }
126         friend class OsclBase;
127 
128     private:
129         class SingletonTable
130         {
131             public:
SingletonTable()132                 SingletonTable()
133                 {
134                     for (uint32 i = 0; i < OSCL_SINGLETON_ID_LAST; i++)
135                         iSingletons[i] = NULL;
136                 }
137                 OsclAny* iSingletons[OSCL_SINGLETON_ID_LAST];
138                 _OsclBasicLock iSingletonLocks[OSCL_SINGLETON_ID_LAST];
139         };
140         //The singleton table is a global variable.
141         static SingletonTable sSingletonTable;
142 };
143 
144 template < class T, uint32 ID, class Registry = OsclSingletonRegistry > class OsclSingleton
145 {
146     private:
147         // make the copy constructor and assignment operator private
148         OsclSingleton& operator=(OsclSingleton& _Y)
149         {
150             return(*this);
151         }
152 
153     protected:
154         T* _Ptr;
155 
156     public:
OsclSingleton()157         OsclSingleton()
158         {
159             int32 err;
160             _Ptr = OSCL_STATIC_CAST(T*, Registry::getInstance(ID, err));
161         }
162 
~OsclSingleton()163         ~OsclSingleton() {};
164 
165         /**
166         * @brief The indirection operator (*) accesses a value indirectly,
167         * through a pointer
168         *
169         * This operator ensures that the OsclSingleton can be used like the
170         * regular pointer that it was initialized with.
171         */
172         T& operator*() const
173         {
174             return(*_Ptr);
175         }
176 
177         /**
178         * @brief The indirection operator (->) accesses a value indirectly,
179         * through a pointer
180         *
181         * This operator ensures that the OsclSingleton can be used like the
182         * regular pointer that it was initialized with.
183         */
184         T *operator->() const
185         {
186             return(_Ptr);
187         }
188 
189 
190         /**
191         * @brief set() method sets ownership to the pointer, passed.
192         * This method is needed when the class is created with a default
193         * constructor. Returns false in case the class is non-empty.
194         *
195         */
set()196         bool set()
197         {
198             int32 err;
199             _Ptr = OSCL_STATIC_CAST(T*, Registry::getInstance(ID, err));
200             return (_Ptr ? true : false);
201         }
202 
203 };
204 
205 
206 #endif //OSCL_HAS_SINGLETON_SUPPORT
207 
208 #endif
209 
210