Nokia N97 SDK
Example Applications Guide

shapelistmanager.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : shapelistmanager.cpp
00004 *  Part of     : GuiEngine
00005 *  Interface   :
00006 *  Description :
00007 *  Version     :
00008 *
00009 *  Copyright (c) 2004 - 2006 Nokia Corporation.
00010 *  This material, including documentation and any related
00011 *  computer programs, is protected by copyright controlled by
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 
00017 // INCLUDE FILES
00018 #include "ShapeListManager.h"
00019 #include "rectangle.h"
00020 #include "circle.h"
00021 
00022 
00023 using namespace NShapes;
00024 
00025 
00026 // ========================= MEMBER FUNCTIONS ==================================
00027 
00028 // -----------------------------------------------------------------------------
00029 // CShapeListManager::CShapeListManager()
00030 // C++ default constructor can NOT contain any code, that might leave.
00031 // -----------------------------------------------------------------------------
00032 //
00033 EXPORT_C CShapeListManager::CShapeListManager()
00034     {
00035     //  No implementation required
00036     }
00037 
00038 // -----------------------------------------------------------------------------
00039 // CShapeListManager::NewL()
00040 // Two-phased constructor.
00041 // -----------------------------------------------------------------------------
00042 //
00043 EXPORT_C CShapeListManager* CShapeListManager::NewL()
00044     {
00045     CShapeListManager* self = NewLC();
00046     CleanupStack::Pop( self );
00047     return self;
00048     }
00049 
00050 // -----------------------------------------------------------------------------
00051 // CShapeListManager::NewLC()
00052 // Two-phased constructor.
00053 // -----------------------------------------------------------------------------
00054 //
00055 EXPORT_C CShapeListManager* CShapeListManager::NewLC()
00056     {
00057     CShapeListManager* self = new ( ELeave ) CShapeListManager;
00058     CleanupStack::PushL( self );
00059     self->ConstructL();
00060     return self;
00061     }
00062 
00063 // -----------------------------------------------------------------------------
00064 // CShapeListManager::ConstructL()
00065 // Symbian 2nd phase constructor can leave.
00066 // -----------------------------------------------------------------------------
00067 //
00068 EXPORT_C void CShapeListManager::ConstructL()
00069     {
00070     //  No implementation required
00071     }
00072 
00073 // -----------------------------------------------------------------------------
00074 // CShapeListManager::~CShapeListManager()
00075 // Destructor.
00076 // -----------------------------------------------------------------------------
00077 //
00078 EXPORT_C CShapeListManager::~CShapeListManager()
00079     {
00080     iShapeList.ResetAndDestroy();
00081     }
00082 
00083 
00084 // -----------------------------------------------------------------------------
00085 // CShapeListManager::Clear()
00086 // Clear the list of shapes.
00087 // -----------------------------------------------------------------------------
00088 //
00089 EXPORT_C void CShapeListManager::Clear()
00090     {
00091     iNextShapeIndex = 0;    //  The first shape in the list
00092     iShapeList.ResetAndDestroy();
00093     }
00094 
00095 // -----------------------------------------------------------------------------
00096 // CShapeListManager::AddShapeL()
00097 // Add a shape to the list
00098 // -----------------------------------------------------------------------------
00099 //
00100 EXPORT_C void CShapeListManager::AddShapeL( TShape* aShape )
00101     {
00102     User::LeaveIfError( iShapeList.Append( aShape ) );
00103     }
00104 
00105 // -----------------------------------------------------------------------------
00106 // CShapeListManager::GetNextShape()
00107 // Returns the next shape in the list and increments the next shape index.
00108 // -----------------------------------------------------------------------------
00109 //
00110 EXPORT_C TShape* CShapeListManager::GetNextShape()
00111     {
00112     if ( iShapeList.Count() > 0)
00113         {
00114         if ( iNextShapeIndex < iShapeList.Count())
00115             {
00116             TShape* shape = iShapeList[ iNextShapeIndex ];
00117             ++iNextShapeIndex;
00118             return shape;
00119             }
00120         //  Gone past the end, so reset the index
00121         iNextShapeIndex = 0;
00122         }
00123     return NULL;
00124     }
00125 
00126 // -----------------------------------------------------------------------------
00127 // CShapeListManager::StoreL()
00128 // Store the instance to the specified store
00129 // -----------------------------------------------------------------------------
00130 //
00131 EXPORT_C TStreamId CShapeListManager::StoreL( CStreamStore& aStore ) const
00132     {
00133 
00134     RStoreWriteStream stream;
00135 
00136     // Create a stream within the store
00137     TStreamId id = stream.CreateLC( aStore );
00138 
00139     // Write the data to the stream
00140     ExternalizeL( stream );
00141     stream.CommitL();
00142 
00143     CleanupStack::PopAndDestroy(); // the stream
00144 
00145     return id; // return the stream ID
00146     }
00147 
00148 // -----------------------------------------------------------------------------
00149 // CShapeListManager::ExternalizeL()
00150 // Virtual method that writes the shape to the specified write stream.
00151 // -----------------------------------------------------------------------------
00152 //
00153 EXPORT_C void CShapeListManager::ExternalizeL( RWriteStream& aStream ) const
00154     {
00155 
00156     TInt count = iShapeList.Count();
00157     aStream.WriteInt16L( static_cast<TInt16>( count ) );
00158 
00159     for ( TInt index = 0; index < count; ++index )
00160         {
00161         const TShape* shape = static_cast<TShape*>( iShapeList[ index ] );
00162         shape->ExternalizeL( aStream );
00163         }
00164     }
00165 
00166 // -----------------------------------------------------------------------------
00167 // CShapeListManager::RestoreL()
00168 // Restore the instance from the specified stream within the specified store
00169 // -----------------------------------------------------------------------------
00170 //
00171 EXPORT_C void CShapeListManager::RestoreL( const CStreamStore& aStore,
00172                                            const TStreamId& aStreamId )
00173     {
00174 
00175     Clear();
00176     RStoreReadStream stream;
00177 
00178     // Open the stream
00179     stream.OpenLC( aStore, aStreamId );
00180 
00181     // Read the data from the stream
00182     InternalizeL( stream );
00183 
00184     CleanupStack::PopAndDestroy(); // the stream
00185     }
00186 
00187 // -----------------------------------------------------------------------------
00188 // CShapeListManager::InternalizeL()
00189 // Restore the instance from the specified stream
00190 // -----------------------------------------------------------------------------
00191 //
00192 EXPORT_C void CShapeListManager::InternalizeL( RReadStream& aStream )
00193     {
00194 
00195     TInt count = aStream.ReadInt16L();
00196     for ( TInt index = 0; index < count; ++index )
00197         {
00198 
00199         TShape* shape = NULL;
00200         switch ( TShape::ReadShapeTypeL( aStream ) )
00201             {
00202             case KRectangle:
00203                 shape = new ( ELeave ) TRectangle();
00204                 break;
00205 
00206             case KCircle:
00207                 shape = new ( ELeave ) TCircle();
00208                 break;
00209 
00210             default:
00211                 User::Leave( KErrCorrupt );
00212             }
00213 
00214 
00215         CleanupStack::PushL( shape );
00216         shape->InternalizeL( aStream );
00217         User::LeaveIfError( iShapeList.Append( shape ) );
00218 
00219         CleanupStack::Pop( shape ); //  Now owned by iShapeList
00220         }
00221     }
00222 
00223 
00224 // End of File

© Nokia 2009

Back to top