Main Page   Modules   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

oscl_singleton.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00003 
00004 //                     O S C L _ S I N G L E T O N
00005 
00006 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00007 
00020 #ifndef OSCL_SINGLETON_H_INCLUDED
00021 #define OSCL_SINGLETON_H_INCLUDED
00022 
00023 #ifndef OSCL_BASE_H_INCLUDED
00024 #include "oscl_base.h"
00025 #endif
00026 
00027 #ifndef OSCL_DEFALLOC_H_INCLUDED
00028 #include "oscl_defalloc.h"
00029 #endif
00030 
00031 
00032 #if (OSCL_HAS_SINGLETON_SUPPORT)
00033 
00034 //verify config-- singleton support requires global var support
00035 
00036 // list of singleton objects
00037 const uint32 OSCL_SINGLETON_ID_TEST           =  0;
00038 const uint32 OSCL_SINGLETON_ID_OSCLMEM        =  1;
00039 const uint32 OSCL_SINGLETON_ID_PVLOGGER       =  2;
00040 const uint32 OSCL_SINGLETON_ID_PVSCHEDULER    =  3;
00041 const uint32 OSCL_SINGLETON_ID_PVERRORTRAP    =  4;
00042 const uint32 OSCL_SINGLETON_ID_SDPMEDIAPARSER =  5;
00043 const uint32 OSCL_SINGLETON_ID_PAYLOADPARSER  =  6;
00044 const uint32 OSCL_SINGLETON_ID_CPM_PLUGIN     =  7;
00045 const uint32 OSCL_SINGLETON_ID_PVMFRECOGNIZER =  8;
00046 const uint32 OSCL_SINGLETON_ID_OSCLREGISTRY   =  9;
00047 const uint32 OSCL_SINGLETON_ID_OMX            = 10;
00048 const uint32 OSCL_SINGLETON_ID_OMXMASTERCORE  = 11;
00049 const uint32 OSCL_SINGLETON_ID_TICKCOUNT      = 12;
00050 const uint32 OSCL_SINGLETON_ID_LAST           = 13;
00051 
00052 
00053 class OsclSingletonRegistry
00054 {
00055     public:
00056         /*
00057         ** Get an entry
00058         ** @param ID: identifier
00059         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
00060         ** @returns: the entry value
00061         */
00062         OSCL_IMPORT_REF static OsclAny* getInstance(uint32 ID, int32 &error);
00063         /*
00064         ** Set an entry
00065         ** @param ID: identifier
00066         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
00067         ** @returns: the entry value
00068         */
00069         OSCL_IMPORT_REF static void registerInstance(OsclAny* ptr, uint32 ID, int32 &error);
00070 
00071         /*
00072         //These two APIs can be used to do "test and set" operations on a singleton.
00073         //Be sure to always call both APIs to avoid deadlock.
00074         */
00075 
00076         /*
00077         * Return the current value of the singleton and leave the singleton table locked
00078         * on return.
00079         * @param ID the singleton ID
00080         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
00081         * @returns the singleton value.
00082         */
00083         OSCL_IMPORT_REF static OsclAny* lockAndGetInstance(uint32 ID, int32& error);
00084         /*
00085         * Set the value of the singleton.  Assume the singleton table is locked on entry.
00086         * @param ptr the singleton value
00087         * @param ID the singleton ID
00088         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
00089         */
00090         OSCL_IMPORT_REF static void registerInstanceAndUnlock(OsclAny* ptr, uint32 ID, int32& error);
00091 
00092     private:
00093         OsclSingletonRegistry()
00094         {}
00095         typedef OsclAny* registry_type;
00096         typedef registry_type* registry_pointer_type;
00097 
00098     private:
00099         OSCL_IMPORT_REF static void initialize(Oscl_DefAlloc &alloc, int32 &error);
00100         OSCL_IMPORT_REF static void cleanup(Oscl_DefAlloc &alloc, int32 &error);
00101         friend class OsclBase;
00102 
00103     private:
00104         class SingletonTable
00105         {
00106             public:
00107                 SingletonTable(): iRefCount(0)
00108                 {
00109                     for (uint32 i = 0;i < OSCL_SINGLETON_ID_LAST;i++)
00110                         iSingletons[i] = NULL;
00111                 }
00112                 _OsclBasicLock iTableLock;
00113                 uint32 iRefCount;
00114                 OsclAny* iSingletons[OSCL_SINGLETON_ID_LAST];
00115                 _OsclBasicLock iSingletonLocks[OSCL_SINGLETON_ID_LAST];
00116         };
00117         //The singleton table is a global variable.
00118         static SingletonTable* iSingletonTable;
00119 };
00120 
00121 template < class T, uint32 ID, class Registry = OsclSingletonRegistry > class OsclSingleton
00122 {
00123     private:
00124         // make the copy constructor and assignment operator private
00125         OsclSingleton& operator=(OsclSingleton& _Y)
00126         {
00127             return(*this);
00128         }
00129 
00130     protected:
00131         T* _Ptr;
00132 
00133     public:
00134         OsclSingleton()
00135         {
00136             int32 err;
00137             _Ptr = OSCL_STATIC_CAST(T*, Registry::getInstance(ID, err));
00138         }
00139 
00140         ~OsclSingleton() {};
00141 
00149         T& operator*() const
00150         {
00151             return(*_Ptr);
00152         }
00153 
00161         T *operator->() const
00162         {
00163             return(_Ptr);
00164         }
00165 
00166 
00173         bool set()
00174         {
00175             int32 err;
00176             _Ptr = OSCL_STATIC_CAST(T*, Registry::getInstance(ID, err));
00177             return (_Ptr ? true : false);
00178         }
00179 
00180 };
00181 
00182 
00183 #endif //OSCL_HAS_SINGLETON_SUPPORT
00184 
00185 #endif
00186 

OSCL API
Posting Version: OPENCORE_20090310