#ifndef CSM_WOLRD_IDCOLLECTION_H #define CSM_WOLRD_IDCOLLECTION_H #include #include #include #include #include #include #include "record.hpp" namespace CSMWorld { template struct Column { std::string mTitle; Column (const std::string& title) : mTitle (title) {} virtual ~Column() {} virtual QVariant get (const Record& record) const = 0; }; class IdCollectionBase { // not implemented IdCollectionBase (const IdCollectionBase&); IdCollectionBase& operator= (const IdCollectionBase&); public: IdCollectionBase(); virtual ~IdCollectionBase(); virtual int getSize() const = 0; virtual std::string getId (int index) const = 0; virtual int getColumns() const = 0; virtual std::string getTitle (int column) const = 0; virtual QVariant getData (int index, int column) const = 0; }; ///< \brief Collection of ID-based records template class IdCollection : public IdCollectionBase { std::vector > mRecords; std::map mIndex; std::vector *> mColumns; // not implemented IdCollection (const IdCollection&); IdCollection& operator= (const IdCollection&); public: IdCollection(); virtual ~IdCollection(); void add (const ESXRecordT& record); ///< Add a new record (modified) virtual int getSize() const; virtual std::string getId (int index) const; virtual int getColumns() const; virtual QVariant getData (int index, int column) const; virtual std::string getTitle (int column) const; virtual void addColumn (Column *column); }; template IdCollection::IdCollection() {} template IdCollection::~IdCollection() { for (typename std::vector *>::iterator iter (mColumns.begin()); iter!=mColumns.end(); ++iter) delete *iter; } template void IdCollection::add (const ESXRecordT& record) { std::string id; std::transform (record.mId.begin(), record.mId.end(), std::back_inserter (id), (int(*)(int)) std::tolower); std::map::iterator iter = mIndex.find (id); if (iter==mIndex.end()) { Record record2; record2.mState = Record::State_ModifiedOnly; record2.mModified = record; mRecords.push_back (record2); mIndex.insert (std::make_pair (id, mRecords.size()-1)); } else { mRecords[iter->second].setModified (record); } } template int IdCollection::getSize() const { return mRecords.size(); } template std::string IdCollection::getId (int index) const { return mRecords.at (index).get().mId; } template int IdCollection::getColumns() const { return mColumns.size(); } template QVariant IdCollection::getData (int index, int column) const { return mColumns.at (column)->get (mRecords.at (index)); } template std::string IdCollection::getTitle (int column) const { return mColumns.at (column)->mTitle; } template void IdCollection::addColumn (Column *column) { mColumns.push_back (column); } } #endif