examples/Base/ThreadsAndProcesses/TLS1/TLS1dll.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: 
00028 DLL example program (1) to demonstrate Thread Local Storage (TLS).
00029 This DLL implements the two classes CSetter and CGeneral
00030 CSetter is used to set up the static text string, delete it, change it and display
00031 its content.
00032 CGeneral can show the content.
00033 The point being made here is that the text string is, effectively, static writeable data.
00034 It is accessible by all classes implemented in the DLL.
00035 CSetter is the only class which sets the static data in this example but, in general,
00036 any class implemented in the DLL can change/delete/fetch the data
00037 through the pointer in thread local storage - it is a matter of application design. 
00038 */
00039 
00040 
00041 #include "TLS1dll.h"
00042 
00043 #include <e32uid.h>
00044 
00045 _LIT(KTxt1,"<>\n");
00046 _LIT(KFormat1,"<%S>\n");
00047 
00049 //
00050 // Class CSetter implementation
00051 //
00053 
00054         // C++ constructor sets reference to the console in the
00055         // initializer list.
00056     // Body of constructor is empty.
00057     // Constructor is exported because it is non-trivial
00058 EXPORT_C CSetter::CSetter(CConsoleBase& aConsole)
00059         : iConsole(aConsole)
00060         {
00061         }
00062 
00063 
00064         // Destructor, deletes the static string
00065 CSetter::~CSetter()
00066         {
00067         delete (HBufC*)Dll::Tls();
00068         Dll::SetTls(NULL);
00069         }
00070 
00071 
00072         // Delete any existing static string; allocates a new HBufC
00073         // and sets thread local storage to point to the HBufC.
00074 EXPORT_C void CSetter::SetStaticTextL(const TDesC& aString)
00075         {
00076         delete (HBufC*)Dll::Tls();
00077         HBufC* pD = aString.AllocL();
00078         Dll::SetTls(pD);
00079         }
00080 
00081 
00082         // Show static text
00083 EXPORT_C void  CSetter::ShowStaticText() const
00084         {
00085         TDesC* text = ((TDesC*)Dll::Tls());
00086         
00087         if (text)
00088                 iConsole.Printf(KFormat1, text);
00089         else
00090                 iConsole.Printf(KTxt1);
00091         }
00092 
00093 
00094 
00096 //
00097 // Class CGeneral implementation
00098 //
00100 
00101         // C++ constructor sets refrence to the console in the
00102         // initializer list.
00103     // Body of constructor is empty.
00104     // Constructor is exported because it is non-trivial
00105 EXPORT_C CGeneral::CGeneral(CConsoleBase& aConsole)
00106         : iConsole(aConsole)
00107         {
00108         }
00109 
00110 
00111         // Show static text
00112 EXPORT_C void  CGeneral::ShowStaticText() const
00113         {
00114         TDesC* text = ((TDesC*)Dll::Tls());
00115         
00116         if (text)
00117                 iConsole.Printf(KFormat1, text);
00118         else
00119                 iConsole.Printf(KTxt1);
00120         }
00121 
00122 

Generated by  doxygen 1.6.2