examples/SysLibs/Streaming/SimpleClass/SimpleClass.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: Example to demonstrate simple use of Streaming  
00028 */
00029 
00030 
00031 
00032 
00033 #include "CommonStreamStore.h"
00034 #include <s32file.h>
00035 
00036                                 // Constructs a TSimple object and 
00037                                 // externalizes it to a single stream.
00038 static void doMakeAndExternalizeL(const TDesC& aName);
00039 
00040                                 // Internalizes a TSimple object from
00041                                 // the stream
00042 static void doInternalizeL(const TDesC& aName);
00043 
00044                                 // Displays content of a TSimple object
00045 class TSimple;
00046 static void doShow(const TDesC& aHeading,const TSimple& theSimple);
00047 
00048                                 // enumeration used by the TSimple class
00049 enum  TXxx {EX1,EX2,EX3};
00050 
00051                                 // definition of the TSimple class
00052 class TSimple
00053         {
00054 public :
00055         void ExternalizeL(RWriteStream& aStream) const;
00056         void InternalizeL(RReadStream& aStream);
00057 public :
00058         TXxx     iTheEnum;
00059         TBuf<32> iBuffer;
00060         TInt     iIntValue;
00061         TUint    iUintValue;
00062         TReal    iRealValue;
00063         };
00064 
00065 
00066 // The file name, extension and path for the file store
00067 _LIT(KFullNameOfFileStore,"\\epoc32ex\\data\\SimpleClassToSimpleStream.dat");
00068 
00069 
00070 //  Do the example
00071 static void doExampleL()
00072     {
00073                             // make sure directory exists
00074         fsSession.MkDirAll(KFullNameOfFileStore);
00075         doMakeAndExternalizeL(KFullNameOfFileStore);
00076         doInternalizeL(KFullNameOfFileStore);
00077         }
00078 
00079 static void doMakeAndExternalizeL(const TDesC& aName)
00080         {
00081         TParse  filestorename;
00082         fsSession.Parse(aName,filestorename);
00083                                 // construct file store object - the file to contain the
00084                                 // the store replaces any existing file of the same name.
00085         CFileStore* store = CDirectFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileWrite);
00086 
00087                                 // Must say what kind of file store
00088     store->SetTypeL(KDirectFileStoreLayoutUid);
00089                 
00090                                 // Construct an object of type TSimple and put some 
00091                                 // data into it
00092         _LIT(KTxtSomeText,"Some text");
00093         TSimple thesimple;
00094         thesimple.iTheEnum   = EX3;
00095         thesimple.iBuffer    = KTxtSomeText;
00096         thesimple.iIntValue  = -1;
00097         thesimple.iUintValue = 2; 
00098         thesimple.iRealValue = 3.4;
00099 
00100                                 // Show contents of the TSimple object
00101         _LIT(KTxtTSimpleContent,"TSimple content ...");
00102         doShow(KTxtTSimpleContent,thesimple);
00103                                         
00104                                 // Construct and create the output stream
00105                                 // object. 
00106                                 // The stream id (there is only one) will
00107                                 // be saved (later) in the store as the 
00108                                 // root stream id
00109         RStoreWriteStream outstream;
00110         TStreamId id = outstream.CreateLC(*store);
00111         
00112                                 // Stream out the TSimple object
00113         outstream  << thesimple; 
00114 
00115                                 // Commit changes to the stream
00116         outstream.CommitL();
00117 
00118                                 // Cleanup the stream object
00119         CleanupStack::PopAndDestroy();
00120                                 
00121                                 // Set the stream id as the root
00122         store->SetRootL(id);
00123 
00124                                 // Commit changes to the store
00125         store->CommitL();
00126 
00127                                 // destroy the store object (this also closes
00128                                 // the file containing the store) 
00129         CleanupStack::PopAndDestroy();
00130         }
00131 
00132 static void doInternalizeL(const TDesC& aName)
00133         {
00134         TParse  filestorename;
00135         fsSession.Parse(aName,filestorename);
00136                                 // construct file store object - specifying the file
00137                                 // containing the store.
00138         CFileStore* store = CDirectFileStore::OpenLC(fsSession,filestorename.FullName(),EFileRead);
00139         
00140                                 // Construct and open the input stream object. We want 
00141                                 // to access the root stream from the store
00142                                 // in this example. 
00143         RStoreReadStream instream;
00144         instream.OpenLC(*store,store->Root());
00145 
00146                                 // Stream in the TSimple object
00147         TSimple thesimple;
00148         instream >> thesimple;
00149 
00150                                 // Cleanup the stream object
00151         CleanupStack::PopAndDestroy();
00152 
00153                                 // destroy the store object (this also closes the file
00154                                 // containing the store) 
00155         CleanupStack::PopAndDestroy();
00156 
00157                                 // Show contents of the TSimple object
00158         _LIT(KTxtRestoredTSimpleContent,"Restored TSimple content ...");
00159         doShow(KTxtRestoredTSimpleContent,thesimple);
00160         }
00161                                                                         
00162 _LIT(KTxtNewLine,"\n");
00163 _LIT(KFormatType1,"\n%d");
00164 _LIT(KFormatType2,"\n%S");
00165 _LIT(KFormatType3,"\n%u");
00166 _LIT(KFormatType4,"\n%f");
00167 
00168 static void doShow(const TDesC& aHeading,const TSimple& aSimple)
00169         {
00170         console->Printf(KTxtNewLine);
00171         console->Printf(aHeading);
00172         console->Printf(KFormatType1,aSimple.iTheEnum);
00173         console->Printf(KFormatType2,&aSimple.iBuffer);
00174         console->Printf(KFormatType1,aSimple.iIntValue);
00175         console->Printf(KFormatType3,aSimple.iUintValue);
00176         console->Printf(KFormatType4,aSimple.iRealValue);
00177         console->Printf(KTxtNewLine);
00178         }
00179 
00180 //***************************************************************
00181 //***************************************************************
00182 
00183 // Explicit versions of the << and >> operators written to handle the
00184 // enumerator type TXxx 
00185 
00186 RWriteStream& operator<<(RWriteStream& aStream, const TXxx& anXxx)
00187         {
00188         aStream.WriteInt8L(anXxx);
00189         return aStream;
00190         }
00191 
00192 RReadStream&  operator>>(RReadStream&  aStream, TXxx& anXxx)
00193         {
00194         anXxx = TXxx(aStream.ReadInt8L());
00195         return aStream;
00196         }
00197 
00198 //***************************************************************
00199 //***************************************************************
00200 
00201 void TSimple::ExternalizeL(RWriteStream& aStream) const
00202         {
00203         aStream << iTheEnum;
00204         aStream << iBuffer;
00205         aStream.WriteInt32L(iIntValue);
00206         aStream.WriteUint32L(iUintValue);
00207         aStream.WriteReal64L(iRealValue);
00208         }  
00209  
00210 void TSimple::InternalizeL(RReadStream& aStream)
00211         {
00212         aStream >> iTheEnum;
00213         aStream >> iBuffer;
00214         iIntValue  = aStream.ReadInt32L();
00215         iUintValue = aStream.ReadUint32L();
00216         iRealValue = aStream.ReadReal64L();
00217         }  
00218                 
00219 
00220 
00221 
00222         
00223         

Generated by  doxygen 1.6.2