OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
idcollection.hpp
Go to the documentation of this file.
1 #ifndef CSM_WOLRD_IDCOLLECTION_H
2 #define CSM_WOLRD_IDCOLLECTION_H
3 
5 
6 #include "collection.hpp"
7 
8 namespace CSMWorld
9 {
11  template<typename ESXRecordT, typename IdAccessorT = IdAccessor<ESXRecordT> >
12  class IdCollection : public Collection<ESXRecordT, IdAccessorT>
13  {
14  virtual void loadRecord (ESXRecordT& record, ESM::ESMReader& reader, bool& isDeleted);
15 
16  public:
17 
19  int load (ESM::ESMReader& reader, bool base);
20 
26  int load (const ESXRecordT& record, bool base, int index = -2);
27 
28  bool tryDelete (const std::string& id);
32  };
33 
34  template<typename ESXRecordT, typename IdAccessorT>
36  ESM::ESMReader& reader,
37  bool& isDeleted)
38  {
39  record.load (reader, isDeleted);
40  }
41 
42  template<typename ESXRecordT, typename IdAccessorT>
44  {
45  ESXRecordT record;
46  bool isDeleted = false;
47 
48  loadRecord (record, reader, isDeleted);
49 
50  std::string id = IdAccessorT().getId (record);
51  int index = this->searchId (id);
52 
53  if (isDeleted)
54  {
55  if (index==-1)
56  {
57  // deleting a record that does not exist
58  // ignore it for now
60  return -1;
61  }
62 
63  if (base)
64  {
65  this->removeRows (index, 1);
66  return -1;
67  }
68 
69  Record<ESXRecordT> baseRecord = this->getRecord (index);
70  baseRecord.mState = RecordBase::State_Deleted;
71  this->setRecord (index, baseRecord);
72  return index;
73  }
74 
75  return load (record, base, index);
76  }
77 
78  template<typename ESXRecordT, typename IdAccessorT>
79  int IdCollection<ESXRecordT, IdAccessorT>::load (const ESXRecordT& record, bool base,
80  int index)
81  {
82  if (index==-2)
83  index = this->searchId (IdAccessorT().getId (record));
84 
85  if (index==-1)
86  {
87  // new record
88  Record<ESXRecordT> record2;
90  (base ? record2.mBase : record2.mModified) = record;
91 
92  index = this->getSize();
93  this->appendRecord (record2);
94  }
95  else
96  {
97  // old record
99 
100  if (base)
101  record2.mBase = record;
102  else
103  record2.setModified (record);
104 
105  this->setRecord (index, record2);
106  }
107 
108  return index;
109  }
110 
111  template<typename ESXRecordT, typename IdAccessorT>
113  {
114  int index = this->searchId (id);
115 
116  if (index==-1)
117  return false;
118 
120 
121  if (record.isDeleted())
122  return false;
123 
125  {
127  }
128  else
129  {
131  this->setRecord (index, record);
132  }
133 
134  return true;
135  }
136 }
137 
138 #endif
Single type collection of top level records.
Definition: idcollection.hpp:12
virtual void loadRecord(ESXRecordT &record, ESM::ESMReader &reader, bool &isDeleted)
Definition: idcollection.hpp:35
int getId(const std::string &name)
Will return -1 for an invalid name.
Definition: columns.cpp:377
State mState
Definition: record.hpp:19
Definition: esmreader.hpp:21
Single-type record collection.
Definition: collection.hpp:44
int load(ESM::ESMReader &reader, bool base)
Definition: idcollection.hpp:43
virtual const Record< ESXRecordT > & getRecord(const std::string &id) const
Definition: collection.hpp:430
MWWorld::Ptr searchId(MWWorld::CellRefList< T > &list, const std::string &id, MWWorld::ContainerStore *store)
Definition: containerstore.cpp:42
Definition: nestedcolumnadapter.hpp:11
ESXRecordT mBase
Definition: record.hpp:40
int load(Arguments &info)
Definition: esmtool.cpp:302
Definition: record.hpp:12
Definition: record.hpp:15
ESXRecordT mModified
Definition: record.hpp:41
virtual void removeRows(int index, int count)
< Merge modified into base.
Definition: collection.hpp:339
void setModified(const ESXRecordT &modified)
Throws an exception, if the record is deleted.
Definition: record.hpp:133
bool tryDelete(const std::string &id)
Definition: idcollection.hpp:112
bool isDeleted() const
Definition: record.cpp:5