blob: 82d7db531069eeb314ff5bd5b3b3374e9ea38438 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>oscl_singleton.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.18 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="modules.html">Modules</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="annotated.html">Data Structures</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Data Fields</a> &nbsp; <a class="qindex" href="globals.html">Globals</a> &nbsp; </center>
<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>
00002 <span class="comment">// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =</span>
00003
00004 <span class="comment">// O S C L _ S I N G L E T O N</span>
00005
00006 <span class="comment">// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =</span>
00007
00020 <span class="preprocessor">#ifndef OSCL_SINGLETON_H_INCLUDED</span>
00021 <span class="preprocessor"></span><span class="preprocessor">#define OSCL_SINGLETON_H_INCLUDED</span>
00022 <span class="preprocessor"></span>
00023 <span class="preprocessor">#ifndef OSCL_BASE_H_INCLUDED</span>
00024 <span class="preprocessor"></span><span class="preprocessor">#include "<a class="code" href="oscl__base_8h.html">oscl_base.h</a>"</span>
00025 <span class="preprocessor">#endif</span>
00026 <span class="preprocessor"></span>
00027 <span class="preprocessor">#ifndef OSCL_DEFALLOC_H_INCLUDED</span>
00028 <span class="preprocessor"></span><span class="preprocessor">#include "<a class="code" href="oscl__defalloc_8h.html">oscl_defalloc.h</a>"</span>
00029 <span class="preprocessor">#endif</span>
00030 <span class="preprocessor"></span>
00031
00032 <span class="preprocessor">#if (OSCL_HAS_SINGLETON_SUPPORT)</span>
00033 <span class="preprocessor"></span>
00034 <span class="comment">//verify config-- singleton support requires global var support</span>
00035
00036 <span class="comment">// list of singleton objects</span>
<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;
<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;
<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;
<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;
<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;
<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;
<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;
<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;
<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;
<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;
<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;
<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;
<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;
<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;
00051
00052
<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>
00054 {
00055 <span class="keyword">public</span>:
00056 <span class="comment">/*</span>
00057 <span class="comment"> ** Get an entry</span>
00058 <span class="comment"> ** @param ID: identifier</span>
00059 <span class="comment"> ** @param error (output) 0 for success or an error from TPVBaseErrorEnum</span>
00060 <span class="comment"> ** @returns: the entry value</span>
00061 <span class="comment"> */</span>
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 &amp;error);
00063 <span class="comment">/*</span>
00064 <span class="comment"> ** Set an entry</span>
00065 <span class="comment"> ** @param ID: identifier</span>
00066 <span class="comment"> ** @param error (output) 0 for success or an error from TPVBaseErrorEnum</span>
00067 <span class="comment"> ** @returns: the entry value</span>
00068 <span class="comment"> */</span>
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 &amp;error);
00070
00071 <span class="comment">/*</span>
00072 <span class="comment"> //These two APIs can be used to do "test and set" operations on a singleton.</span>
00073 <span class="comment"> //Be sure to always call both APIs to avoid deadlock.</span>
00074 <span class="comment"> */</span>
00075
00076 <span class="comment">/*</span>
00077 <span class="comment"> * Return the current value of the singleton and leave the singleton table locked</span>
00078 <span class="comment"> * on return.</span>
00079 <span class="comment"> * @param ID the singleton ID</span>
00080 <span class="comment"> ** @param error (output) 0 for success or an error from TPVBaseErrorEnum</span>
00081 <span class="comment"> * @returns the singleton value.</span>
00082 <span class="comment"> */</span>
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&amp; error);
00084 <span class="comment">/*</span>
00085 <span class="comment"> * Set the value of the singleton. Assume the singleton table is locked on entry.</span>
00086 <span class="comment"> * @param ptr the singleton value</span>
00087 <span class="comment"> * @param ID the singleton ID</span>
00088 <span class="comment"> ** @param error (output) 0 for success or an error from TPVBaseErrorEnum</span>
00089 <span class="comment"> */</span>
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&amp; error);
00091
00092 <span class="keyword">private</span>:
00093 <a class="code" href="classOsclSingletonRegistry.html">OsclSingletonRegistry</a>()
00094 {}
00095 <span class="keyword">typedef</span> <a class="code" href="group__osclbase.html#a25">OsclAny</a>* registry_type;
00096 <span class="keyword">typedef</span> registry_type* registry_pointer_type;
00097
00098 <span class="keyword">private</span>:
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> &amp;alloc, int32 &amp;error);
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> &amp;alloc, int32 &amp;error);
<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>;
00102
00103 <span class="keyword">private</span>:
00104 <span class="keyword">class </span>SingletonTable
00105 {
00106 <span class="keyword">public</span>:
00107 SingletonTable(): iRefCount(0)
00108 {
00109 <span class="keywordflow">for</span> (uint32 i = 0;i &lt; <a class="code" href="oscl__singleton_8h.html#a13">OSCL_SINGLETON_ID_LAST</a>;i++)
00110 iSingletons[i] = <a class="code" href="group__osclbase.html#a81">NULL</a>;
00111 }
00112 _OsclBasicLock iTableLock;
00113 uint32 iRefCount;
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>];
00115 _OsclBasicLock iSingletonLocks[<a class="code" href="oscl__singleton_8h.html#a13">OSCL_SINGLETON_ID_LAST</a>];
00116 };
00117 <span class="comment">//The singleton table is a global variable.</span>
00118 <span class="keyword">static</span> SingletonTable* iSingletonTable;
00119 };
<a name="l00120"></a><a class="code" href="classOsclSingleton.html">00120</a>
00121 <span class="keyword">template</span> &lt; <span class="keyword">class</span> T, u<span class="keywordtype">int</span>32 ID, <span class="keyword">class</span> Registry = OsclSingletonRegistry &gt; <span class="keyword">class </span><a class="code" href="classOsclSingleton.html">OsclSingleton</a>
00122 {
00123 <span class="keyword">private</span>:
00124 <span class="comment">// make the copy constructor and assignment operator private</span>
00125 <a class="code" href="classOsclSingleton.html">OsclSingleton</a>&amp; operator=(<a class="code" href="classOsclSingleton.html">OsclSingleton</a>&amp; _Y)
00126 {
00127 <span class="keywordflow">return</span>(*this);
00128 }
00129
<a name="l00130"></a><a class="code" href="classOsclSingleton.html#n0">00130</a> <span class="keyword">protected</span>:
00131 T* <a class="code" href="classOsclSingleton.html#n0">_Ptr</a>;
00132
<a name="l00133"></a><a class="code" href="classOsclSingleton.html#a0">00133</a> <span class="keyword">public</span>:
00134 <a class="code" href="classOsclSingleton.html#a0">OsclSingleton</a>()
00135 {
00136 int32 err;
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));
00138 }
<a name="l00139"></a><a class="code" href="classOsclSingleton.html#a1">00139</a>
00140 <a class="code" href="classOsclSingleton.html#a1">~OsclSingleton</a>() {};
00141
00149 T&amp; <a class="code" href="classOsclSingleton.html#a2">operator*</a>()<span class="keyword"> const</span>
00150 <span class="keyword"> </span>{
00151 <span class="keywordflow">return</span>(*_Ptr);
00152 }
00153
00161 T *<a class="code" href="classOsclSingleton.html#a3">operator-&gt;</a>()<span class="keyword"> const</span>
00162 <span class="keyword"> </span>{
00163 <span class="keywordflow">return</span>(_Ptr);
00164 }
00165
00166
00173 <span class="keywordtype">bool</span> <a class="code" href="classOsclSingleton.html#a4">set</a>()
00174 {
00175 int32 err;
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));
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>);
00178 }
00179
00180 };
00181
00182
00183 <span class="preprocessor">#endif //OSCL_HAS_SINGLETON_SUPPORT</span>
00184 <span class="preprocessor"></span>
00185 <span class="preprocessor">#endif</span>
00186 <span class="preprocessor"></span>
</pre></div><hr size="1"><img src="pvlogo_small.jpg"><address style="align: right;"><small>OSCL API</small>
<address style="align: left;"><small>Posting Version: OPENCORE_20090310 </small>
</small></address>
</body>
</html>