|
Nokia N97 SDK Example Applications Guide |
00001 /** 00002 * @file Stack.h 00003 * Part of : Open C / ProducerConsumer 00004 * 00005 * Description : ProducerConsumer Problem solution 00006 * Version: 0.1 00007 * 00008 * Copyright (c) 2007 Nokia Corporation. 00009 * This material, including documentation and any related 00010 * computer programs, is protected by copyright controlled by 00011 * Nokia Corporation. 00012 * 00013 */ 00014 00015 #ifndef STACK_H 00016 #define STACK_H 00017 00018 /* INCLUDE FILES */ 00019 #include <string.h> 00020 #include "CommanHeader.h" 00021 00022 00023 /* 00024 C++ Class that defines Stack 00025 */ 00026 class Stack { 00027 enum 00028 { 00029 EMaxProduction = 1000 00030 }; 00031 00032 public: 00033 00034 Stack() : iStatckTop(-1) 00035 { 00036 } 00037 00038 /** 00039 * Destructor 00040 * Delete all the items those are not yet consumed by the consumers 00041 */ 00042 ~Stack() 00043 { 00044 ProducedItem* item = NULL; 00045 do 00046 { 00047 item = Pop(); 00048 delete item; 00049 } while(item != NULL); 00050 } 00051 00052 /** 00053 * Push 00054 * New Item is created on Stack for every push operation 00055 * 00056 * @param aItem - Item to be Pushed 00057 * 00058 * @return Nothing 00059 */ 00060 void Push( ProducedItem* aItem ) 00061 { 00062 //If there is enough space to put item onto stack 00063 if(iStatckTop < EMaxProduction-1) 00064 { 00065 ProducedItem* item = new ProducedItem; 00066 memcpy(item, aItem, sizeof(ProducedItem)); 00067 iItems[++iStatckTop] = item; 00068 } 00069 //else Ignore it! 00070 } 00071 00072 /** 00073 * Pop 00074 * Item is sent to the Consumer who is doing the Pop. 00075 * Ownership will also ne given. 00076 * So, this item has to be deleted by the Consumer. 00077 * 00078 * @param Nothing 00079 * 00080 * @return - Item Poped 00081 */ 00082 ProducedItem* Pop() 00083 { 00084 if(iStatckTop != -1) 00085 { 00086 ProducedItem* item = iItems[iStatckTop]; 00087 iItems[iStatckTop--] = NULL; 00088 return item; 00089 } 00090 return NULL; 00091 } 00092 00093 private: 00094 ProducedItem* iItems[EMaxProduction]; 00095 int iStatckTop; 00096 }; 00097 00098 #endif /*STACK_H*/ 00099 00100 /* End of File */
|
© Nokia 2009 |