00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). 00002 // All rights reserved. 00003 // This component and the accompanying materials are made available 00004 // under the terms of "Eclipse Public License v1.0" 00005 // which accompanies this distribution, and is available 00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". 00007 // 00008 // Initial Contributors: 00009 // Nokia Corporation - initial contribution. 00010 // 00011 // Contributors: 00012 // 00013 // Description: 00014 // DLL example program (1) to demonstrate Thread Local Storage (TLS). 00015 // This DLL implements the two classes CSetter and CGeneral 00016 // CSetter is used to set up the static text string, delete it, change it and display 00017 // its content. 00018 // CGeneral can show the content. 00019 // The point being made here is that the text string is, effectively, static writeable data. 00020 // It is accessible by all classes implemented in the DLL. 00021 // CSetter is the only class which sets the static data in this example but, in general, 00022 // any class implemented in the DLL can change/delete/fetch the data 00023 // through the pointer in thread local storage - it is a matter of application design. 00024 // 00025 00026 #include "TLS1dll.h" 00027 00028 #include <e32uid.h> 00029 00030 _LIT(KTxt1,"<>\n"); 00031 _LIT(KFormat1,"<%S>\n"); 00032 00034 // 00035 // Class CSetter implementation 00036 // 00038 00039 // C++ constructor sets reference to the console in the 00040 // initializer list. 00041 // Body of constructor is empty. 00042 // Constructor is exported because it is non-trivial 00043 EXPORT_C CSetter::CSetter(CConsoleBase& aConsole) 00044 : iConsole(aConsole) 00045 { 00046 } 00047 00048 00049 // Destructor, deletes the static string 00050 CSetter::~CSetter() 00051 { 00052 delete (HBufC*)Dll::Tls(); 00053 Dll::SetTls(NULL); 00054 } 00055 00056 00057 // Delete any existing static string; allocates a new HBufC 00058 // and sets thread local storage to point to the HBufC. 00059 EXPORT_C void CSetter::SetStaticTextL(const TDesC& aString) 00060 { 00061 delete (HBufC*)Dll::Tls(); 00062 HBufC* pD = aString.AllocL(); 00063 Dll::SetTls(pD); 00064 } 00065 00066 00067 // Show static text 00068 EXPORT_C void CSetter::ShowStaticText() const 00069 { 00070 TDesC* text = ((TDesC*)Dll::Tls()); 00071 00072 if (text) 00073 iConsole.Printf(KFormat1, text); 00074 else 00075 iConsole.Printf(KTxt1); 00076 } 00077 00078 00079 00081 // 00082 // Class CGeneral implementation 00083 // 00085 00086 // C++ constructor sets refrence to the console in the 00087 // initializer list. 00088 // Body of constructor is empty. 00089 // Constructor is exported because it is non-trivial 00090 EXPORT_C CGeneral::CGeneral(CConsoleBase& aConsole) 00091 : iConsole(aConsole) 00092 { 00093 } 00094 00095 00096 // Show static text 00097 EXPORT_C void CGeneral::ShowStaticText() const 00098 { 00099 TDesC* text = ((TDesC*)Dll::Tls()); 00100 00101 if (text) 00102 iConsole.Printf(KFormat1, text); 00103 else 00104 iConsole.Printf(KTxt1); 00105 } 00106 00107