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         friend class OsclBase;
    00101
    00102     private:
    00103         class SingletonTable
    00104         {
    00105             public:
    00106                 SingletonTable()
    00107                 {
    00108                     for (uint32 i = 0; i < OSCL_SINGLETON_ID_LAST; i++)
    00109                         iSingletons[i] = NULL;
    00110                 }
    00111                 OsclAny* iSingletons[OSCL_SINGLETON_ID_LAST];
    00112                 _OsclBasicLock iSingletonLocks[OSCL_SINGLETON_ID_LAST];
    00113         };
    00114         //The singleton table is a global variable.
    00115         static SingletonTable sSingletonTable;
    00116 };
    00117
    00118 template < class T, uint32 ID, class Registry = OsclSingletonRegistry > class OsclSingleton
    00119 {
    00120     private:
    00121         // make the copy constructor and assignment operator private
    00122         OsclSingleton& operator=(OsclSingleton& _Y)
    00123         {
    00124             return(*this);
    00125         }
    00126
    00127     protected:
    00128         T* _Ptr;
    00129
    00130     public:
    00131         OsclSingleton()
    00132         {
    00133             int32 err;
    00134             _Ptr = OSCL_STATIC_CAST(T*, Registry::getInstance(ID, err));
    00135         }
    00136
    00137         ~OsclSingleton() {};
    00138
    00146         T& operator*() const
    00147         {
    00148             return(*_Ptr);
    00149         }
    00150
    00158         T *operator->() const
    00159         {
    00160             return(_Ptr);
    00161         }
    00162
    00163
    00170         bool set()
    00171         {
    00172             int32 err;
    00173             _Ptr = OSCL_STATIC_CAST(T*, Registry::getInstance(ID, err));
    00174             return (_Ptr ? true : false);
    00175         }
    00176
    00177 };
    00178
    00179
    00180 #endif //OSCL_HAS_SINGLETON_SUPPORT
    00181
    00182 #endif
    00183