OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
containerstore.hpp
Go to the documentation of this file.
1 #ifndef GAME_MWWORLD_CONTAINERSTORE_H
2 #define GAME_MWWORLD_CONTAINERSTORE_H
3 
4 #include <iterator>
5 #include <map>
6 #include <utility>
7 
20 
21 #include "ptr.hpp"
22 #include "cellreflist.hpp"
23 
24 namespace ESM
25 {
26  struct InventoryList;
27  struct InventoryState;
28 }
29 
30 namespace MWWorld
31 {
32  class ContainerStore;
33 
34  template<class PtrType>
36 
39 
40 
42  {
43  public:
44  virtual void itemAdded(const ConstPtr& item, int count) {}
45  virtual void itemRemoved(const ConstPtr& item, int count) {}
46  };
47 
49  {
50  public:
51 
52  static const int Type_Potion = 0x0001;
53  static const int Type_Apparatus = 0x0002;
54  static const int Type_Armor = 0x0004;
55  static const int Type_Book = 0x0008;
56  static const int Type_Clothing = 0x0010;
57  static const int Type_Ingredient = 0x0020;
58  static const int Type_Light = 0x0040;
59  static const int Type_Lockpick = 0x0080;
60  static const int Type_Miscellaneous = 0x0100;
61  static const int Type_Probe = 0x0200;
62  static const int Type_Repair = 0x0400;
63  static const int Type_Weapon = 0x0800;
64 
65  static const int Type_Last = Type_Weapon;
66 
67  static const int Type_All = 0xffff;
68 
69  static const std::string sGoldId;
70 
71  private:
72 
85 
86  std::map<std::pair<std::string, std::string>, int> mLevelledItemMap;
89 
91 
92  mutable float mCachedWeight;
93  mutable bool mWeightUpToDate;
94  ContainerStoreIterator addImp (const Ptr& ptr, int count);
95  void addInitialItem (const std::string& id, const std::string& owner, int count, bool topLevel=true, const std::string& levItem = "");
96 
97  template<typename T>
99  const ESM::ObjectState& state);
100 
101  template<typename T>
102  void storeState (const LiveCellRef<T>& ref, ESM::ObjectState& state) const;
103 
104  template<typename T>
105  void storeStates (const CellRefList<T>& collection,
106  ESM::InventoryState& inventory, int& index,
107  bool equipable = false) const;
108 
109 
110  virtual void storeEquipmentState (const MWWorld::LiveCellRefBase& ref, int index, ESM::InventoryState& inventory) const;
111 
112  virtual void readEquipmentState (const MWWorld::ContainerStoreIterator& iter, int index, const ESM::InventoryState& inventory);
113 
114  public:
115 
116  ContainerStore();
117 
118  virtual ~ContainerStore();
119 
120  virtual ContainerStore* clone() { return new ContainerStore(*this); }
121 
122  ConstContainerStoreIterator cbegin (int mask = Type_All) const;
124  ConstContainerStoreIterator begin (int mask = Type_All) const;
126 
129 
130  virtual ContainerStoreIterator add (const Ptr& itemPtr, int count, const Ptr& actorPtr, bool setOwner=false);
141 
142  ContainerStoreIterator add(const std::string& id, int count, const Ptr& actorPtr);
144 
145  int remove(const std::string& itemId, int count, const Ptr& actor);
149 
150  virtual int remove(const Ptr& item, int count, const Ptr& actor);
154 
155  ContainerStoreIterator unstack (const Ptr& ptr, const Ptr& container, int count = 1);
159 
164 
165  int count (const std::string& id);
167 
168  int restockCount (const std::string& id);
171 
173  void setContListener(ContainerStoreListener* listener);
174 
175  protected:
178 
179  virtual void flagAsModified();
180 
181  public:
182 
183  virtual bool stacks (const ConstPtr& ptr1, const ConstPtr& ptr2) const;
185 
186  void fill (const ESM::InventoryList& items, const std::string& owner);
188 
189  void restock (const ESM::InventoryList& items, const MWWorld::Ptr& ptr, const std::string& owner);
190 
191  virtual void clear();
193 
194  float getWeight() const;
196 
197  static int getType (const ConstPtr& ptr);
200 
201  Ptr findReplacement(const std::string& id);
203 
204  Ptr search (const std::string& id);
205 
206  virtual void writeState (ESM::InventoryState& state) const;
207 
208  virtual void readState (const ESM::InventoryState& state);
209 
212  };
213 
214 
215  template<class PtrType>
217  : public std::iterator<std::forward_iterator_tag, PtrType, std::ptrdiff_t, PtrType *, PtrType&>
218  {
219  template<class From, class To, class Dummy>
221  {
222  static const bool value = true;
223  };
224 
225  template<class Dummy>
226  struct IsConvertible<ConstPtr, Ptr, Dummy>
227  {
228  static const bool value = false;
229  };
230 
231  template<class T, class U>
233  {
235  };
236 
237  template<class T>
239  {
241  };
242 
243  template<class T>
244  struct Iterator : IteratorTrait<T, PtrType>
245  {
246  };
247 
248  template<class T, class Dummy>
250  {
252  };
253 
254  template<class Dummy>
256  {
257  typedef const ContainerStore* type;
258  };
259 
261 
262  int mType;
263  int mMask;
265  mutable PtrType mPtr;
266 
279 
282 
283  ContainerStoreIteratorBase (int mask, ContainerStoreType container);
285 
286  // construct iterator using a CellRefList iterator
299 
300  template<class T>
301  void copy (const ContainerStoreIteratorBase<T>& src);
302 
303  void incType ();
304 
305  void nextType ();
306 
307  bool resetIterator ();
311 
312  bool incIterator ();
316 
317  public:
318  template<class T>
320  {
321  char CANNOT_CONVERT_CONST_ITERATOR_TO_ITERATOR[IsConvertible<T, PtrType, void>::value ? 1 : -1];
322  ((void)CANNOT_CONVERT_CONST_ITERATOR_TO_ITERATOR);
323  copy (other);
324  }
325 
326  template<class T>
327  bool isEqual(const ContainerStoreIteratorBase<T>& other) const;
328 
329  PtrType *operator->() const;
330  PtrType operator*() const;
331 
335 
336  int getType() const;
337  const ContainerStore *getContainerStore() const;
338 
339  friend class ContainerStore;
342  };
343 
344  template<class T, class U>
346  template<class T, class U>
348 }
349 #endif
ContainerStoreIteratorBase(ContainerStoreType container)
End-iterator.
Definition: containerstore.cpp:1146
MWWorld::CellRefList< T >::List::iterator type
Definition: containerstore.hpp:234
Iterator< ESM::Repair >::type mRepair
Definition: containerstore.hpp:277
Used to create pointers to hold any type of LiveCellRef<> object.
Definition: livecellref.hpp:22
ContainerStoreListener * getContListener() const
Definition: containerstore.cpp:170
MWWorld::CellRefList< ESM::Apparatus > appas
Definition: containerstore.hpp:74
virtual void writeState(ESM::InventoryState &state) const
Definition: containerstore.cpp:780
int restockCount(const std::string &id)
Definition: containerstore.cpp:160
virtual ContainerStore * clone()
Definition: containerstore.hpp:120
int getType() const
Definition: containerstore.cpp:1134
MWWorld::CellRefList< ESM::Miscellaneous > miscItems
Definition: containerstore.hpp:81
static const int Type_Clothing
Definition: containerstore.hpp:56
static const int Type_Light
Definition: containerstore.hpp:58
MWWorld::CellRefList< ESM::Probe > probes
Definition: containerstore.hpp:82
ConstContainerStoreIterator cend() const
Definition: containerstore.cpp:126
bool operator==(const LiveCellRef< X > &ref, int pRefnum)
Definition: cellstore.cpp:207
virtual void flagAsModified()
Definition: containerstore.cpp:604
Iterator< ESM::Lockpick >::type mLockpick
Definition: containerstore.hpp:274
ContainerStoreIteratorBase< Ptr > ContainerStoreIterator
Definition: containerstore.hpp:35
void restock(const ESM::InventoryList &items, const MWWorld::Ptr &ptr, const std::string &owner)
Definition: containerstore.cpp:511
Definition: containerstore.hpp:244
ConstContainerStoreIterator end() const
Definition: containerstore.cpp:136
ContainerStoreIterator addNewStack(const ConstPtr &ptr, int count)
Add the item to this container (do not try to stack it onto existing items)
Definition: containerstore.cpp:382
MWWorld::CellRefList< ESM::Armor > armors
Definition: containerstore.hpp:75
virtual bool stacks(const ConstPtr &ptr1, const ConstPtr &ptr2) const
Definition: containerstore.cpp:223
bool isEqual(const ContainerStoreIteratorBase< T > &other) const
Definition: containerstore.cpp:1038
static const bool value
Definition: containerstore.hpp:222
Iterator< ESM::Weapon >::type mWeapon
Definition: containerstore.hpp:278
bool mWeightUpToDate
Definition: containerstore.hpp:93
InventoryList, NPCO subrecord.
Definition: loadcont.hpp:26
virtual void readEquipmentState(const MWWorld::ContainerStoreIterator &iter, int index, const ESM::InventoryState &inventory)
Definition: containerstore.cpp:87
static const int Type_Armor
Definition: containerstore.hpp:54
static const int Type_Ingredient
Definition: containerstore.hpp:57
virtual ~ContainerStore()
Definition: containerstore.cpp:119
Definition: livecellref.hpp:77
static const int Type_Apparatus
Definition: containerstore.hpp:53
static const int Type_Weapon
Definition: containerstore.hpp:63
MWWorld::CellRefList< ESM::Book > books
Definition: containerstore.hpp:76
ContainerStoreIteratorBase & operator++()
Definition: containerstore.cpp:1103
ContainerStoreListener * mListener
Definition: containerstore.hpp:90
static const int Type_Last
Definition: containerstore.hpp:65
bool resetIterator()
Definition: containerstore.cpp:898
void copy(const ContainerStoreIteratorBase< T > &src)
Definition: containerstore.cpp:844
MWWorld::CellRefList< T >::List::const_iterator type
Definition: containerstore.hpp:240
static const int Type_Repair
Definition: containerstore.hpp:62
static const std::string sGoldId
Definition: containerstore.hpp:69
ContainerStoreIteratorBase & operator=(const ContainerStoreIteratorBase &rhs)
Definition: containerstore.cpp:1124
ConstContainerStoreIterator cbegin(int mask=Type_All) const
Definition: containerstore.cpp:121
void fill(const ESM::InventoryList &items, const std::string &owner)
Insert items into *this.
Definition: containerstore.cpp:449
Definition: containerstore.hpp:232
MWWorld::CellRefList< ESM::Repair > repairs
Definition: containerstore.hpp:83
virtual void itemAdded(const ConstPtr &item, int count)
Definition: containerstore.hpp:44
ConstContainerStoreIterator begin(int mask=Type_All) const
Definition: containerstore.cpp:131
Iterator< ESM::Clothing >::type mClothing
Definition: containerstore.hpp:271
const ContainerStore * getContainerStore() const
Definition: containerstore.cpp:1140
float getWeight() const
Return total weight of the items contained in *this.
Definition: containerstore.cpp:609
Ptr search(const std::string &id)
Definition: containerstore.cpp:703
MWWorld::CellRefList< ESM::Ingredient > ingreds
Definition: containerstore.hpp:78
static const int Type_Book
Definition: containerstore.hpp:55
bool incIterator()
Definition: containerstore.cpp:967
ContainerStoreTrait< PtrType, void >::type ContainerStoreType
Definition: containerstore.hpp:260
MWWorld::CellRefList< ESM::Weapon > weapons
Definition: containerstore.hpp:84
Definition: containerstore.hpp:41
int mType
Definition: containerstore.hpp:262
ContainerStoreIterator addImp(const Ptr &ptr, int count)
Definition: containerstore.cpp:339
<
Definition: objectstate.hpp:19
Definition: containerstore.hpp:35
MWWorld::CellRefList< ESM::Clothing > clothes
Definition: containerstore.hpp:77
Iterator< ESM::Probe >::type mProbe
Definition: containerstore.hpp:276
Iterator< ESM::Apparatus >::type mApparatus
Definition: containerstore.hpp:268
virtual void clear()
Empty container.
Definition: containerstore.cpp:596
void incType()
Definition: containerstore.cpp:871
Iterator< ESM::Miscellaneous >::type mMiscellaneous
Definition: containerstore.hpp:275
MWWorld::ContainerStoreIterator restack(const MWWorld::Ptr &item)
Definition: containerstore.cpp:195
Pointer to a const LiveCellRef.
Definition: ptr.hpp:90
virtual ContainerStoreIterator add(const Ptr &itemPtr, int count, const Ptr &actorPtr, bool setOwner=false)
Definition: containerstore.cpp:263
ContainerStoreIteratorBase(const ContainerStoreIteratorBase< T > &other)
Definition: containerstore.hpp:319
MWWorld::CellRefList< ESM::Lockpick > lockpicks
Definition: containerstore.hpp:80
static const int Type_Probe
Definition: containerstore.hpp:61
State for inventories and containers.
Definition: inventorystate.hpp:16
MWWorld::CellRefList< ESM::Potion > potions
Definition: containerstore.hpp:73
void setContListener(ContainerStoreListener *listener)
Definition: containerstore.cpp:176
Definition: containerstore.hpp:48
ContainerStore * type
Definition: containerstore.hpp:251
int mMask
Definition: containerstore.hpp:263
ContainerStoreIterator getState(CellRefList< T > &collection, const ESM::ObjectState &state)
static const int Type_Potion
Definition: containerstore.hpp:52
virtual void storeEquipmentState(const MWWorld::LiveCellRefBase &ref, int index, ESM::InventoryState &inventory) const
Definition: containerstore.cpp:83
void nextType()
Definition: containerstore.cpp:885
static const int Type_All
Definition: containerstore.hpp:67
void storeState(const LiveCellRef< T > &ref, ESM::ObjectState &state) const
Definition: containerstore.cpp:92
ContainerStoreIteratorBase< ConstPtr > ConstContainerStoreIterator
Definition: containerstore.hpp:38
Ptr findReplacement(const std::string &id)
Returns replacement for object with given id. Prefer used items (with low durability left)...
Definition: containerstore.cpp:679
Definition: containerstore.hpp:220
ContainerStore()
Definition: containerstore.cpp:117
static int getType(const ConstPtr &ptr)
Definition: containerstore.cpp:634
PtrType operator*() const
Definition: containerstore.cpp:1074
PtrType mPtr
Definition: containerstore.hpp:265
Iterator< ESM::Potion >::type mPotion
Definition: containerstore.hpp:267
std::map< std::pair< std::string, std::string >, int > mLevelledItemMap
Definition: containerstore.hpp:86
void addInitialItem(const std::string &id, const std::string &owner, int count, bool topLevel=true, const std::string &levItem="")
Definition: containerstore.cpp:461
ContainerStoreType mContainer
Definition: containerstore.hpp:264
PtrType * operator->() const
Definition: containerstore.cpp:1067
Iterator< ESM::Book >::type mBook
Definition: containerstore.hpp:270
Pointer to a LiveCellRef.
Definition: ptr.hpp:19
Iterator< ESM::Ingredient >::type mIngredient
Definition: containerstore.hpp:272
bool operator!=(const CellStore &left, const CellStore &right)
Definition: cellstore.cpp:930
Iterator< ESM::Armor >::type mArmor
Definition: containerstore.hpp:269
MWWorld::CellRefList< ESM::Light > lights
Definition: containerstore.hpp:79
Iterator< ESM::Light >::type mLight
Definition: containerstore.hpp:273
ContainerStoreIterator unstack(const Ptr &ptr, const Ptr &container, int count=1)
Definition: containerstore.cpp:181
static const int Type_Miscellaneous
Definition: containerstore.hpp:60
virtual void itemRemoved(const ConstPtr &item, int count)
Definition: containerstore.hpp:45
int count(const std::string &id)
Definition: containerstore.cpp:151
void storeStates(const CellRefList< T > &collection, ESM::InventoryState &inventory, int &index, bool equipable=false) const
Definition: containerstore.cpp:98
float mCachedWeight
Definition: containerstore.hpp:92
static const int Type_Lockpick
Definition: containerstore.hpp:59
const ContainerStore * type
Definition: containerstore.hpp:257
virtual void readState(const ESM::InventoryState &state)
Definition: containerstore.cpp:801