OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
nestedidcollection.hpp
Go to the documentation of this file.
1 #ifndef CSM_WOLRD_NESTEDIDCOLLECTION_H
2 #define CSM_WOLRD_NESTEDIDCOLLECTION_H
3 
4 #include <map>
5 #include <stdexcept>
6 
7 #include "nestedcollection.hpp"
9 
10 namespace ESM
11 {
12  class ESMReader;
13 }
14 
15 namespace CSMWorld
16 {
17  struct NestedTableWrapperBase;
18  struct Cell;
19 
20  template<typename T, typename AT>
21  class IdCollection;
22 
23  template<typename ESXRecordT, typename IdAccessorT = IdAccessor<ESXRecordT> >
24  class NestedIdCollection : public IdCollection<ESXRecordT, IdAccessorT>, public NestedCollection
25  {
26  std::map<const ColumnBase*, NestedColumnAdapter<ESXRecordT>* > mAdapters;
27 
28  const NestedColumnAdapter<ESXRecordT>& getAdapter(const ColumnBase &column) const;
29 
30  public:
31 
34 
35  virtual void addNestedRow(int row, int column, int position);
36 
37  virtual void removeNestedRows(int row, int column, int subRow);
38 
39  virtual QVariant getNestedData(int row, int column, int subRow, int subColumn) const;
40 
41  virtual void setNestedData(int row, int column, const QVariant& data, int subRow, int subColumn);
42 
43  virtual NestedTableWrapperBase* nestedTable(int row, int column) const;
44 
45  virtual void setNestedTable(int row, int column, const NestedTableWrapperBase& nestedTable);
46 
47  virtual int getNestedRowsCount(int row, int column) const;
48 
49  virtual int getNestedColumnsCount(int row, int column) const;
50 
51  // this method is inherited from NestedCollection, not from Collection<ESXRecordT>
52  virtual NestableColumn *getNestableColumn(int column);
53 
54  void addAdapter(std::pair<const ColumnBase*, NestedColumnAdapter<ESXRecordT>* > adapter);
55  };
56 
57  template<typename ESXRecordT, typename IdAccessorT>
59  {}
60 
61  template<typename ESXRecordT, typename IdAccessorT>
63  {
64  for (typename std::map<const ColumnBase *, NestedColumnAdapter<ESXRecordT>* >::iterator
65  iter (mAdapters.begin()); iter!=mAdapters.end(); ++iter)
66  {
67  delete (*iter).second;
68  }
69  }
70 
71  template<typename ESXRecordT, typename IdAccessorT>
74  {
75  mAdapters.insert(adapter);
76  }
77 
78  template<typename ESXRecordT, typename IdAccessorT>
80  {
81  typename std::map<const ColumnBase *, NestedColumnAdapter<ESXRecordT>* >::const_iterator iter =
82  mAdapters.find (&column);
83 
84  if (iter==mAdapters.end())
85  throw std::logic_error("No such column in the nestedidadapter");
86 
87  return *iter->second;
88  }
89 
90  template<typename ESXRecordT, typename IdAccessorT>
91  void NestedIdCollection<ESXRecordT, IdAccessorT>::addNestedRow(int row, int column, int position)
92  {
93  Record<ESXRecordT> record;
95 
96  getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).addRow(record, position);
97 
99  }
100 
101  template<typename ESXRecordT, typename IdAccessorT>
103  {
104  Record<ESXRecordT> record;
106 
107  getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).removeRow(record, subRow);
108 
110  }
111 
112  template<typename ESXRecordT, typename IdAccessorT>
114  int column, int subRow, int subColumn) const
115  {
116  return getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).getData(
117  Collection<ESXRecordT, IdAccessorT>::getRecord(row), subRow, subColumn);
118  }
119 
120  template<typename ESXRecordT, typename IdAccessorT>
122  int column, const QVariant& data, int subRow, int subColumn)
123  {
124  Record<ESXRecordT> record;
126 
127  getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).setData(
128  record, data, subRow, subColumn);
129 
131  }
132 
133  template<typename ESXRecordT, typename IdAccessorT>
135  int column) const
136  {
137  return getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).table(
139  }
140 
141  template<typename ESXRecordT, typename IdAccessorT>
143  int column, const CSMWorld::NestedTableWrapperBase& nestedTable)
144  {
145  Record<ESXRecordT> record;
147 
148  getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).setTable(
149  record, nestedTable);
150 
152  }
153 
154  template<typename ESXRecordT, typename IdAccessorT>
156  {
157  return getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).getRowsCount(
159  }
160 
161  template<typename ESXRecordT, typename IdAccessorT>
163  {
164  const ColumnBase &nestedColumn = Collection<ESXRecordT, IdAccessorT>::getColumn(column);
166  if (row >= 0 && row < numRecords)
167  {
169  return getAdapter(nestedColumn).getColumnsCount(record);
170  }
171  else
172  {
173  // If the row is invalid (or there no records), retrieve the column count using a blank record
174  const Record<ESXRecordT> record;
175  return getAdapter(nestedColumn).getColumnsCount(record);
176  }
177  }
178 
179  template<typename ESXRecordT, typename IdAccessorT>
181  {
183  }
184 }
185 
186 #endif // CSM_WOLRD_NESTEDIDCOLLECTION_H
Single type collection of top level records.
Definition: idcollection.hpp:12
Definition: nestedtablewrapper.hpp:6
virtual int getNestedColumnsCount(int row, int column) const
Definition: nestedidcollection.hpp:162
virtual void removeNestedRows(int row, int column, int subRow)
Definition: nestedidcollection.hpp:102
virtual NestedTableWrapperBase * nestedTable(int row, int column) const
Definition: nestedidcollection.hpp:134
Definition: columnbase.hpp:167
virtual void setNestedTable(int row, int column, const NestedTableWrapperBase &nestedTable)
Definition: nestedidcollection.hpp:142
Single-type record collection.
Definition: collection.hpp:44
virtual const Record< ESXRecordT > & getRecord(const std::string &id) const
Definition: collection.hpp:430
std::map< const ColumnBase *, NestedColumnAdapter< ESXRecordT > * > mAdapters
Definition: nestedidcollection.hpp:26
virtual NestableColumn * getNestableColumn(int column)
Definition: nestedidcollection.hpp:180
virtual void setNestedData(int row, int column, const QVariant &data, int subRow, int subColumn)
Definition: nestedidcollection.hpp:121
Definition: nestedcolumnadapter.hpp:11
void setRecord(int index, const Record< ESXRecordT > &record)
Definition: collection.hpp:466
virtual void assign(const RecordBase &record)
Will throw an exception if the types don't match.
Definition: record.hpp:100
virtual const ColumnBase & getColumn(int column) const
Definition: collection.hpp:295
virtual int getNestedRowsCount(int row, int column) const
Definition: nestedidcollection.hpp:155
virtual QVariant getNestedData(int row, int column, int subRow, int subColumn) const
Definition: nestedidcollection.hpp:113
void addAdapter(std::pair< const ColumnBase *, NestedColumnAdapter< ESXRecordT > * > adapter)
Definition: nestedidcollection.hpp:72
Definition: columnbase.hpp:15
virtual int getSize() const
Definition: collection.hpp:254
Definition: nestedcolumnadapter.hpp:14
NestedIdCollection()
Definition: nestedidcollection.hpp:58
NestableColumn * getNestableColumn(int column) const
Definition: collection.hpp:301
~NestedIdCollection()
Definition: nestedidcollection.hpp:62
Definition: nestedidcollection.hpp:24
Definition: nestedcollection.hpp:13
virtual void addNestedRow(int row, int column, int position)
Definition: nestedidcollection.hpp:91
const NestedColumnAdapter< ESXRecordT > & getAdapter(const ColumnBase &column) const
Definition: nestedidcollection.hpp:79