1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> 3 <title>oscl_singleton.h Source File</title> 4 <link href="doxygen.css" rel="stylesheet" type="text/css"> 5 </head><body> 6 <!-- Generated by Doxygen 1.2.18 --> 7 <center> 8 <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="modules.html">Modules</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="annotated.html">Data Structures</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Data Fields</a> <a class="qindex" href="globals.html">Globals</a> </center> 9 <hr><h1>oscl_singleton.h</h1><a href="oscl__singleton_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">// -*- c++ -*-</span> 10 00002 <span class="comment">// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =</span> 11 00003 12 00004 <span class="comment">// O S C L _ S I N G L E T O N</span> 13 00005 14 00006 <span class="comment">// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =</span> 15 00007 16 00020 <span class="preprocessor">#ifndef OSCL_SINGLETON_H_INCLUDED</span> 17 00021 <span class="preprocessor"></span><span class="preprocessor">#define OSCL_SINGLETON_H_INCLUDED</span> 18 00022 <span class="preprocessor"></span> 19 00023 <span class="preprocessor">#ifndef OSCL_BASE_H_INCLUDED</span> 20 00024 <span class="preprocessor"></span><span class="preprocessor">#include "<a class="code" href="oscl__base_8h.html">oscl_base.h</a>"</span> 21 00025 <span class="preprocessor">#endif</span> 22 00026 <span class="preprocessor"></span> 23 00027 <span class="preprocessor">#ifndef OSCL_DEFALLOC_H_INCLUDED</span> 24 00028 <span class="preprocessor"></span><span class="preprocessor">#include "<a class="code" href="oscl__defalloc_8h.html">oscl_defalloc.h</a>"</span> 25 00029 <span class="preprocessor">#endif</span> 26 00030 <span class="preprocessor"></span> 27 00031 28 00032 <span class="preprocessor">#if (OSCL_HAS_SINGLETON_SUPPORT)</span> 29 00033 <span class="preprocessor"></span> 30 00034 <span class="comment">//verify config-- singleton support requires global var support</span> 31 00035 32 00036 <span class="comment">// list of singleton objects</span> 33 <a name="l00037"></a><a class="code" href="oscl__singleton_8h.html#a0">00037</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a0">OSCL_SINGLETON_ID_TEST</a> = 0; 34 <a name="l00038"></a><a class="code" href="oscl__singleton_8h.html#a1">00038</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a1">OSCL_SINGLETON_ID_OSCLMEM</a> = 1; 35 <a name="l00039"></a><a class="code" href="oscl__singleton_8h.html#a2">00039</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a2">OSCL_SINGLETON_ID_PVLOGGER</a> = 2; 36 <a name="l00040"></a><a class="code" href="oscl__singleton_8h.html#a3">00040</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a3">OSCL_SINGLETON_ID_PVSCHEDULER</a> = 3; 37 <a name="l00041"></a><a class="code" href="oscl__singleton_8h.html#a4">00041</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a4">OSCL_SINGLETON_ID_PVERRORTRAP</a> = 4; 38 <a name="l00042"></a><a class="code" href="oscl__singleton_8h.html#a5">00042</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a5">OSCL_SINGLETON_ID_SDPMEDIAPARSER</a> = 5; 39 <a name="l00043"></a><a class="code" href="oscl__singleton_8h.html#a6">00043</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a6">OSCL_SINGLETON_ID_PAYLOADPARSER</a> = 6; 40 <a name="l00044"></a><a class="code" href="oscl__singleton_8h.html#a7">00044</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a7">OSCL_SINGLETON_ID_CPM_PLUGIN</a> = 7; 41 <a name="l00045"></a><a class="code" href="oscl__singleton_8h.html#a8">00045</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a8">OSCL_SINGLETON_ID_PVMFRECOGNIZER</a> = 8; 42 <a name="l00046"></a><a class="code" href="oscl__singleton_8h.html#a9">00046</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a9">OSCL_SINGLETON_ID_OSCLREGISTRY</a> = 9; 43 <a name="l00047"></a><a class="code" href="oscl__singleton_8h.html#a10">00047</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a10">OSCL_SINGLETON_ID_OMX</a> = 10; 44 <a name="l00048"></a><a class="code" href="oscl__singleton_8h.html#a11">00048</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a11">OSCL_SINGLETON_ID_OMXMASTERCORE</a> = 11; 45 <a name="l00049"></a><a class="code" href="oscl__singleton_8h.html#a12">00049</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a12">OSCL_SINGLETON_ID_TICKCOUNT</a> = 12; 46 <a name="l00050"></a><a class="code" href="oscl__singleton_8h.html#a13">00050</a> <span class="keyword">const</span> uint32 <a class="code" href="oscl__singleton_8h.html#a13">OSCL_SINGLETON_ID_LAST</a> = 13; 47 00051 48 00052 49 <a name="l00053"></a><a class="code" href="classOsclSingletonRegistry.html">00053</a> <span class="keyword">class </span><a class="code" href="classOsclSingletonRegistry.html">OsclSingletonRegistry</a> 50 00054 { 51 00055 <span class="keyword">public</span>: 52 00056 <span class="comment">/*</span> 53 00057 <span class="comment"> ** Get an entry</span> 54 00058 <span class="comment"> ** @param ID: identifier</span> 55 00059 <span class="comment"> ** @param error (output) 0 for success or an error from TPVBaseErrorEnum</span> 56 00060 <span class="comment"> ** @returns: the entry value</span> 57 00061 <span class="comment"> */</span> 58 00062 OSCL_IMPORT_REF <span class="keyword">static</span> <a class="code" href="group__osclbase.html#a25">OsclAny</a>* <a class="code" href="classOsclSingletonRegistry.html#d0">getInstance</a>(uint32 ID, int32 &error); 59 00063 <span class="comment">/*</span> 60 00064 <span class="comment"> ** Set an entry</span> 61 00065 <span class="comment"> ** @param ID: identifier</span> 62 00066 <span class="comment"> ** @param error (output) 0 for success or an error from TPVBaseErrorEnum</span> 63 00067 <span class="comment"> ** @returns: the entry value</span> 64 00068 <span class="comment"> */</span> 65 00069 OSCL_IMPORT_REF <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="classOsclSingletonRegistry.html#d1">registerInstance</a>(<a class="code" href="group__osclbase.html#a25">OsclAny</a>* ptr, uint32 ID, int32 &error); 66 00070 67 00071 <span class="comment">/*</span> 68 00072 <span class="comment"> //These two APIs can be used to do "test and set" operations on a singleton.</span> 69 00073 <span class="comment"> //Be sure to always call both APIs to avoid deadlock.</span> 70 00074 <span class="comment"> */</span> 71 00075 72 00076 <span class="comment">/*</span> 73 00077 <span class="comment"> * Return the current value of the singleton and leave the singleton table locked</span> 74 00078 <span class="comment"> * on return.</span> 75 00079 <span class="comment"> * @param ID the singleton ID</span> 76 00080 <span class="comment"> ** @param error (output) 0 for success or an error from TPVBaseErrorEnum</span> 77 00081 <span class="comment"> * @returns the singleton value.</span> 78 00082 <span class="comment"> */</span> 79 00083 OSCL_IMPORT_REF <span class="keyword">static</span> <a class="code" href="group__osclbase.html#a25">OsclAny</a>* <a class="code" href="classOsclSingletonRegistry.html#d2">lockAndGetInstance</a>(uint32 ID, int32& error); 80 00084 <span class="comment">/*</span> 81 00085 <span class="comment"> * Set the value of the singleton. Assume the singleton table is locked on entry.</span> 82 00086 <span class="comment"> * @param ptr the singleton value</span> 83 00087 <span class="comment"> * @param ID the singleton ID</span> 84 00088 <span class="comment"> ** @param error (output) 0 for success or an error from TPVBaseErrorEnum</span> 85 00089 <span class="comment"> */</span> 86 00090 OSCL_IMPORT_REF <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="classOsclSingletonRegistry.html#d3">registerInstanceAndUnlock</a>(<a class="code" href="group__osclbase.html#a25">OsclAny</a>* ptr, uint32 ID, int32& error); 87 00091 88 00092 <span class="keyword">private</span>: 89 00093 <a class="code" href="classOsclSingletonRegistry.html">OsclSingletonRegistry</a>() 90 00094 {} 91 00095 <span class="keyword">typedef</span> <a class="code" href="group__osclbase.html#a25">OsclAny</a>* registry_type; 92 00096 <span class="keyword">typedef</span> registry_type* registry_pointer_type; 93 00097 94 00098 <span class="keyword">private</span>: 95 00099 OSCL_IMPORT_REF <span class="keyword">static</span> <span class="keywordtype">void</span> initialize(<a class="code" href="classOscl__DefAlloc.html">Oscl_DefAlloc</a> &alloc, int32 &error); 96 00100 OSCL_IMPORT_REF <span class="keyword">static</span> <span class="keywordtype">void</span> cleanup(<a class="code" href="classOscl__DefAlloc.html">Oscl_DefAlloc</a> &alloc, int32 &error); 97 <a name="l00101"></a><a class="code" href="classOsclSingletonRegistry.html#l0">00101</a> <span class="keyword">friend</span> <span class="keyword">class </span><a class="code" href="classOsclSingletonRegistry.html#l0">OsclBase</a>; 98 00102 99 00103 <span class="keyword">private</span>: 100 00104 <span class="keyword">class </span>SingletonTable 101 00105 { 102 00106 <span class="keyword">public</span>: 103 00107 SingletonTable(): iRefCount(0) 104 00108 { 105 00109 <span class="keywordflow">for</span> (uint32 i = 0;i < <a class="code" href="oscl__singleton_8h.html#a13">OSCL_SINGLETON_ID_LAST</a>;i++) 106 00110 iSingletons[i] = <a class="code" href="group__osclbase.html#a81">NULL</a>; 107 00111 } 108 00112 _OsclBasicLock iTableLock; 109 00113 uint32 iRefCount; 110 00114 <a class="code" href="group__osclbase.html#a25">OsclAny</a>* iSingletons[<a class="code" href="oscl__singleton_8h.html#a13">OSCL_SINGLETON_ID_LAST</a>]; 111 00115 _OsclBasicLock iSingletonLocks[<a class="code" href="oscl__singleton_8h.html#a13">OSCL_SINGLETON_ID_LAST</a>]; 112 00116 }; 113 00117 <span class="comment">//The singleton table is a global variable.</span> 114 00118 <span class="keyword">static</span> SingletonTable* iSingletonTable; 115 00119 }; 116 <a name="l00120"></a><a class="code" href="classOsclSingleton.html">00120</a> 117 00121 <span class="keyword">template</span> < <span class="keyword">class</span> T, u<span class="keywordtype">int</span>32 ID, <span class="keyword">class</span> Registry = OsclSingletonRegistry > <span class="keyword">class </span><a class="code" href="classOsclSingleton.html">OsclSingleton</a> 118 00122 { 119 00123 <span class="keyword">private</span>: 120 00124 <span class="comment">// make the copy constructor and assignment operator private</span> 121 00125 <a class="code" href="classOsclSingleton.html">OsclSingleton</a>& operator=(<a class="code" href="classOsclSingleton.html">OsclSingleton</a>& _Y) 122 00126 { 123 00127 <span class="keywordflow">return</span>(*this); 124 00128 } 125 00129 126 <a name="l00130"></a><a class="code" href="classOsclSingleton.html#n0">00130</a> <span class="keyword">protected</span>: 127 00131 T* <a class="code" href="classOsclSingleton.html#n0">_Ptr</a>; 128 00132 129 <a name="l00133"></a><a class="code" href="classOsclSingleton.html#a0">00133</a> <span class="keyword">public</span>: 130 00134 <a class="code" href="classOsclSingleton.html#a0">OsclSingleton</a>() 131 00135 { 132 00136 int32 err; 133 00137 <a class="code" href="classOsclSingleton.html#n0">_Ptr</a> = <a class="code" href="group__osclbase.html#a86">OSCL_STATIC_CAST</a>(T*, Registry::getInstance(ID, err)); 134 00138 } 135 <a name="l00139"></a><a class="code" href="classOsclSingleton.html#a1">00139</a> 136 00140 <a class="code" href="classOsclSingleton.html#a1">~OsclSingleton</a>() {}; 137 00141 138 00149 T& <a class="code" href="classOsclSingleton.html#a2">operator*</a>()<span class="keyword"> const</span> 139 00150 <span class="keyword"> </span>{ 140 00151 <span class="keywordflow">return</span>(*_Ptr); 141 00152 } 142 00153 143 00161 T *<a class="code" href="classOsclSingleton.html#a3">operator-></a>()<span class="keyword"> const</span> 144 00162 <span class="keyword"> </span>{ 145 00163 <span class="keywordflow">return</span>(_Ptr); 146 00164 } 147 00165 148 00166 149 00173 <span class="keywordtype">bool</span> <a class="code" href="classOsclSingleton.html#a4">set</a>() 150 00174 { 151 00175 int32 err; 152 00176 <a class="code" href="classOsclSingleton.html#n0">_Ptr</a> = <a class="code" href="group__osclbase.html#a86">OSCL_STATIC_CAST</a>(T*, Registry::getInstance(ID, err)); 153 00177 <span class="keywordflow">return</span> (<a class="code" href="classOsclSingleton.html#n0">_Ptr</a> ? <span class="keyword">true</span> : <span class="keyword">false</span>); 154 00178 } 155 00179 156 00180 }; 157 00181 158 00182 159 00183 <span class="preprocessor">#endif //OSCL_HAS_SINGLETON_SUPPORT</span> 160 00184 <span class="preprocessor"></span> 161 00185 <span class="preprocessor">#endif</span> 162 00186 <span class="preprocessor"></span> 163 </pre></div><hr size="1"><img src="pvlogo_small.jpg"><address style="align: right;"><small>OSCL API</small> 164 <address style="align: left;"><small>Posting Version: OPENCORE_20090310 </small> 165 </small></address> 166 </body> 167 </html> 168