Crossfire Server, Trunk
AssetsCollection.h
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2020 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #ifndef ASSETSCOLLECTION_H
14 #define ASSETSCOLLECTION_H
15 
16 #include <string>
17 #include <map>
18 #include <set>
19 #include <algorithm>
20 #include <functional>
21 
38 template<class T, class Key = std::string>
40 
41  public:
42 
46  void clear() {
47  for (auto asset : m_assets) {
48  destroy(asset.second);
49  }
50  m_assets.empty();
51  }
52 
57  size_t count() const {
58  return m_assets.size();
59  }
60 
66  T *get(const Key& name) {
67  auto asset = m_assets.find(name);
68  if (asset != m_assets.end()) {
69  return asset->second;
70  }
71 
72  T *add = create(name);
73  m_assets[name] = add;
74  m_undefined.insert(name);
75  added(add);
76  return add;
77  }
78 
85  T *find(const Key& name) {
86  auto asset = m_assets.find(name);
87  return asset != m_assets.end() ? asset->second : nullptr;
88  }
89 
97  T *define(const Key& name, T *asset) {
98  m_undefined.erase(name);
99  auto existing = m_assets.find(name);
100  if (existing != m_assets.end()) {
101  replace(existing->second, asset);
102  return existing->second;
103  }
104 
105  m_assets[name] = asset;
106  added(asset);
107  return asset;
108  }
109 
116  T *next(T *current) {
117  if (m_assets.empty()) {
118  return nullptr;
119  }
120  if (current == nullptr) {
121  return m_assets.begin()->second;
122  }
123 
124  auto c = std::find_if(m_assets.begin(), m_assets.end(), [&current](auto& item) { return item.second == current; });
125  if (c == m_assets.end() || (++c) == m_assets.end()) {
126  return nullptr;
127  }
128  return c->second;
129  }
130 
135  void each(std::function<void(T *)> op) {
136  std::for_each(m_assets.begin(), m_assets.end(), [&op] (auto item) { op(item.second); });
137  }
138 
144  T *first(std::function<bool(const T *)> op) {
145  auto found = std::find_if(m_assets.begin(), m_assets.end(), [&op] (auto item) { return op(item.second); });
146  return found == m_assets.end() ? nullptr : found->second;
147  }
148 
154  const std::set<Key>& undefined() const { return m_undefined; }
155 
156  protected:
157  std::map<Key, T*> m_assets;
158  std::set<Key> m_undefined;
165  virtual T *create(const Key& name) = 0;
170  virtual void destroy(T *item) = 0;
176  virtual void replace(T *existing, T *update) = 0;
177 
181  virtual void added(T *) { }
182 };
183 
184 #endif /* ASSETSCOLLECTION_H */
AssetsCollection::count
size_t count() const
Definition: AssetsCollection.h:57
AssetsCollection::undefined
const std::set< Key > & undefined() const
Definition: AssetsCollection.h:154
AssetsCollection::first
T * first(std::function< bool(const T *)> op)
Definition: AssetsCollection.h:144
c
static event_registration c
Definition: citylife.cpp:425
AssetsCollection::added
virtual void added(T *)
Definition: AssetsCollection.h:181
AssetsCollection::find
T * find(const Key &name)
Definition: AssetsCollection.h:85
AssetsCollection::replace
virtual void replace(T *existing, T *update)=0
AssetsCollection::m_assets
std::map< Key, T * > m_assets
Definition: AssetsCollection.h:157
AssetsCollection
Definition: AssetsCollection.h:39
AssetsCollection::define
T * define(const Key &name, T *asset)
Definition: AssetsCollection.h:97
AssetsCollection::m_undefined
std::set< Key > m_undefined
Definition: AssetsCollection.h:158
AssetsCollection::get
T * get(const Key &name)
Definition: AssetsCollection.h:66
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:135
AssetsCollection::next
T * next(T *current)
Definition: AssetsCollection.h:116
is_valid_types_gen.found
found
Definition: is_valid_types_gen.py:39
item
Definition: item.py:1
give.op
op
Definition: give.py:33
AssetsCollection::destroy
virtual void destroy(T *item)=0
AssetsCollection::clear
void clear()
Definition: AssetsCollection.h:46
replace.current
current
Definition: replace.py:64
AssetsCollection::create
virtual T * create(const Key &name)=0
give.name
name
Definition: give.py:27