#ifndef CSM_WOLRD_NESTEDIDCOLLECTION_H #define CSM_WOLRD_NESTEDIDCOLLECTION_H #include #include #include #include "collection.hpp" #include "nestedcollection.hpp" #include "nestedcolumnadapter.hpp" namespace ESM { class ESMReader; } namespace CSMWorld { struct NestedTableWrapperBase; struct Cell; struct ColumnBase; template class IdCollection; template class NestedColumnAdapter; template class NestedIdCollection : public IdCollection, public NestedCollection { std::map*> mAdapters; const NestedColumnAdapter& getAdapter(const ColumnBase& column) const; public: NestedIdCollection(); ~NestedIdCollection() override; void addNestedRow(int row, int column, int position) override; void removeNestedRows(int row, int column, int subRow) override; QVariant getNestedData(int row, int column, int subRow, int subColumn) const override; void setNestedData(int row, int column, const QVariant& data, int subRow, int subColumn) override; NestedTableWrapperBase* nestedTable(int row, int column) const override; void setNestedTable(int row, int column, const NestedTableWrapperBase& nestedTable) override; int getNestedRowsCount(int row, int column) const override; int getNestedColumnsCount(int row, int column) const override; // this method is inherited from NestedCollection, not from Collection NestableColumn* getNestableColumn(int column) override; void addAdapter(std::pair*> adapter); }; template NestedIdCollection::NestedIdCollection() { } template NestedIdCollection::~NestedIdCollection() { for (typename std::map*>::iterator iter(mAdapters.begin()); iter != mAdapters.end(); ++iter) { delete (*iter).second; } } template void NestedIdCollection::addAdapter( std::pair*> adapter) { mAdapters.insert(adapter); } template const NestedColumnAdapter& NestedIdCollection::getAdapter(const ColumnBase& column) const { typename std::map*>::const_iterator iter = mAdapters.find(&column); if (iter == mAdapters.end()) throw std::logic_error("No such column in the nestedidadapter"); return *iter->second; } template void NestedIdCollection::addNestedRow(int row, int column, int position) { auto record = std::make_unique>(); record->assign(Collection::getRecord(row)); getAdapter(Collection::getColumn(column)).addRow(*record, position); Collection::setRecord(row, std::move(record)); } template void NestedIdCollection::removeNestedRows(int row, int column, int subRow) { auto record = std::make_unique>(); record->assign(Collection::getRecord(row)); getAdapter(Collection::getColumn(column)).removeRow(*record, subRow); Collection::setRecord(row, std::move(record)); } template QVariant NestedIdCollection::getNestedData(int row, int column, int subRow, int subColumn) const { return getAdapter(Collection::getColumn(column)) .getData(Collection::getRecord(row), subRow, subColumn); } template void NestedIdCollection::setNestedData( int row, int column, const QVariant& data, int subRow, int subColumn) { auto record = std::make_unique>(); record->assign(Collection::getRecord(row)); getAdapter(Collection::getColumn(column)).setData(*record, data, subRow, subColumn); Collection::setRecord(row, std::move(record)); } template CSMWorld::NestedTableWrapperBase* NestedIdCollection::nestedTable(int row, int column) const { return getAdapter(Collection::getColumn(column)).table(Collection::getRecord(row)); } template void NestedIdCollection::setNestedTable( int row, int column, const CSMWorld::NestedTableWrapperBase& nestedTable) { auto record = std::make_unique>(); record->assign(Collection::getRecord(row)); getAdapter(Collection::getColumn(column)).setTable(*record, nestedTable); Collection::setRecord(row, std::move(record)); } template int NestedIdCollection::getNestedRowsCount(int row, int column) const { return getAdapter(Collection::getColumn(column)) .getRowsCount(Collection::getRecord(row)); } template int NestedIdCollection::getNestedColumnsCount(int row, int column) const { const ColumnBase& nestedColumn = Collection::getColumn(column); int numRecords = Collection::getSize(); if (row >= 0 && row < numRecords) { const Record& record = Collection::getRecord(row); return getAdapter(nestedColumn).getColumnsCount(record); } else { // If the row is invalid (or there no records), retrieve the column count using a blank record const Record record; return getAdapter(nestedColumn).getColumnsCount(record); } } template CSMWorld::NestableColumn* NestedIdCollection::getNestableColumn(int column) { return Collection::getNestableColumn(column); } } #endif // CSM_WOLRD_NESTEDIDCOLLECTION_H