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

# 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_WMDRMLOCK      = 13;
    00051 const uint32 OSCL_SINGLETON_ID_LAST           = 14;
    00052
    00053
    00054 class OsclSingletonRegistry
    00055 {
    00056     public:
    00057         /*
    00058         ** Get an entry
    00059         ** @param ID: identifier
    00060         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
    00061         ** @returns: the entry value
    00062         */
    00063         OSCL_IMPORT_REF static OsclAny* getInstance(uint32 ID, int32 &error);
    00064         /*
    00065         ** Set an entry
    00066         ** @param ID: identifier
    00067         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
    00068         ** @returns: the entry value
    00069         */
    00070         OSCL_IMPORT_REF static void registerInstance(OsclAny* ptr, uint32 ID, int32 &error);
    00071
    00072         /*
    00073         //These two APIs can be used to do "test and set" operations on a singleton.
    00074         //Be sure to always call both APIs to avoid deadlock.
    00075         */
    00076
    00077         /*
    00078         * Return the current value of the singleton and leave the singleton table locked
    00079         * on return.
    00080         * @param ID the singleton ID
    00081         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
    00082         * @returns the singleton value.
    00083         */
    00084         OSCL_IMPORT_REF static OsclAny* lockAndGetInstance(uint32 ID, int32& error);
    00085         /*
    00086         * Set the value of the singleton.  Assume the singleton table is locked on entry.
    00087         * @param ptr the singleton value
    00088         * @param ID the singleton ID
    00089         ** @param error (output) 0 for success or an error from TPVBaseErrorEnum
    00090         */
    00091         OSCL_IMPORT_REF static void registerInstanceAndUnlock(OsclAny* ptr, uint32 ID, int32& error);
    00092
    00093     private:
    00094         OsclSingletonRegistry()
    00095         {}
    00096         typedef OsclAny* registry_type;
    00097         typedef registry_type* registry_pointer_type;
    00098
    00099     private:
    00100         OSCL_IMPORT_REF static void initialize(Oscl_DefAlloc &alloc, int32 &error);
    00101         OSCL_IMPORT_REF static void cleanup(Oscl_DefAlloc &alloc, int32 &error);
    00102         friend class OsclBase;
    00103
    00104     private:
    00105         class SingletonTable
    00106         {
    00107             public:
    00108                 SingletonTable(): iRefCount(0)
    00109                 {
    00110                     for (uint32 i = 0; i < OSCL_SINGLETON_ID_LAST; i++)
    00111                         iSingletons[i] = NULL;
    00112                 }
    00113                 _OsclBasicLock iTableLock;
    00114                 uint32 iRefCount;
    00115                 OsclAny* iSingletons[OSCL_SINGLETON_ID_LAST];
    00116                 _OsclBasicLock iSingletonLocks[OSCL_SINGLETON_ID_LAST];
    00117         };
    00118         //The singleton table is a global variable.
    00119         static SingletonTable* iSingletonTable;
    00120 };
    00121
    00122 template < class T, uint32 ID, class Registry = OsclSingletonRegistry > class OsclSingleton
    00123 {
    00124     private:
    00125         // make the copy constructor and assignment operator private
    00126         OsclSingleton& operator=(OsclSingleton& _Y)
    00127         {
    00128             return(*this);
    00129         }
    00130
    00131     protected:
    00132         T* _Ptr;
    00133
    00134     public:
    00135         OsclSingleton()
    00136         {
    00137             int32 err;
    00138             _Ptr = OSCL_STATIC_CAST(T*, Registry::getInstance(ID, err));
    00139         }
    00140
    00141         ~OsclSingleton() {};
    00142
    00150         T& operator*() const
    00151         {
    00152             return(*_Ptr);
    00153         }
    00154
    00162         T *operator->() const
    00163         {
    00164             return(_Ptr);
    00165         }
    00166
    00167
    00174         bool set()
    00175         {
    00176             int32 err;
    00177             _Ptr = OSCL_STATIC_CAST(T*, Registry::getInstance(ID, err));
    00178             return (_Ptr ? true : false);
    00179         }
    00180
    00181 };
    00182
    00183
    00184 #endif //OSCL_HAS_SINGLETON_SUPPORT
    00185
    00186 #endif
    00187