00001 #ifndef __LISTDATA_H__
00002 #define __LISTDATA_H__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "Data.h"
00029
00030 #include <QList>
00031 #include <QDebug>
00032
00033 #include <typeinfo>
00034 #include <cassert>
00035
00036 template <typename T>
00037 class ListData : public Data
00038 {
00039 private:
00040 QList<T*> m_datas;
00041
00042 public:
00043 ListData():Data("ListData") {}
00044 ~ListData()
00045 {
00046 foreach(T* pData, m_datas)
00047 {
00048 delete pData;
00049 }
00050 }
00051
00052 void add(T* pData)
00053 {
00054 assert(pData);
00055
00056 m_datas.append(pData);
00057 }
00058
00059 const QList<T*>& dataList()const { return m_datas; }
00060
00061 QByteArray data()const
00062 {
00063 QByteArray dataArray;
00064 foreach(T* pData, m_datas)
00065 {
00066 dataArray += pData->data();
00067 }
00068
00069 return dataArray;
00070 }
00071
00072 int load(const QByteArray& data, int index=0)
00073 {
00074 T* pData = new T();
00075 if ( pData == NULL )
00076 {
00077 qDebug() << "Fail to allocate memory for " << typeid(T).name();
00078 return -1;
00079 }
00080
00081 int nbRead = pData->load(data,index);
00082 if ( nbRead != -1 )
00083 {
00084 this->add(pData);
00085 }
00086 else
00087 {
00088 qDebug() << "Fail to read OperationData";
00089 }
00090
00091 return nbRead;
00092 }
00093
00094 void remove(T* pData)
00095 {
00096 assert(pData);
00097
00098 m_datas.removeOne(pData);
00099 }
00100 };
00101
00123 #endif