xoreos  0.0.5
inventory.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
26 
27 namespace Engines {
28 
29 namespace KotOR {
30 
31 void Inventory::addItem(const Common::UString &tag, int count) {
32  if (tag.empty())
33  return;
34 
35  assert(count > 0);
36 
37  ItemMap::iterator i = _items.find(tag);
38  if (i == _items.end()) {
39  InventoryItem item;
40  item.tag = tag;
41  item.count = count;
42  _items.insert(std::pair<Common::UString, InventoryItem>(tag, item));
43  } else
44  i->second.count += count;
45 }
46 
47 void Inventory::removeItem(const Common::UString &tag, int count) {
48  if (tag.empty())
49  return;
50 
51  assert(count > 0);
52 
53  ItemMap::iterator i = _items.find(tag);
54  if (i == _items.end())
55  return;
56 
57  if (i->second.count > count)
58  i->second.count -= count;
59  else
60  _items.erase(i);
61 }
62 
64  _items.clear();
65 }
66 
67 const std::map<Common::UString, InventoryItem> &Inventory::getItems() const {
68  return _items;
69 }
70 
71 bool Inventory::hasItem(const Common::UString &tag) const {
72  return _items.find(tag) != _items.end();
73 }
74 
75 } // End of namespace KotOR
76 
77 } // End of namespace Engines
A class holding an UTF-8 string.
Definition: ustring.h:48
Collection of items.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
void addItem(const Common::UString &tag, int count=1)
Definition: inventory.cpp:31
bool hasItem(const Common::UString &tag) const
Definition: inventory.cpp:71
void removeItem(const Common::UString &tag, int count=1)
Definition: inventory.cpp:47
const std::map< Common::UString, InventoryItem > & getItems() const
Definition: inventory.cpp:67