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 
26 template<class T>
27 extern void asset_destroy(T *);
28 
34 template <class T, class Key>
35 extern T *asset_create(const Key &);
36 
53 template<class T, class Key = std::string>
55 
56  public:
57 
61  virtual ~AssetsCollection() {
62  clear();
63  }
64 
68  void clear() {
69  for (auto asset : m_assets) {
70  asset_destroy(asset.second);
71  }
72  m_assets.clear();
73  }
74 
79  size_t count() const {
80  return m_assets.size();
81  }
82 
88  T *get(const Key& name) {
89  auto asset = m_assets.find(name);
90  if (asset != m_assets.end()) {
91  return asset->second;
92  }
93 
94  T *add = asset_create<T>(name);
95  m_assets[name] = add;
96  m_undefined.insert(name);
97  added(add);
98  return add;
99  }
100 
107  T *find(const Key& name) {
108  auto asset = m_assets.find(name);
109  return asset != m_assets.end() ? asset->second : nullptr;
110  }
111 
119  T *define(const Key& name, T *asset) {
120  m_undefined.erase(name);
121  auto existing = m_assets.find(name);
122  if (existing != m_assets.end()) {
123  replace(existing->second, asset);
124  return existing->second;
125  }
126 
127  m_assets[name] = asset;
128  added(asset);
129  return asset;
130  }
131 
138  T *next(T *current) {
139  if (m_assets.empty()) {
140  return nullptr;
141  }
142  if (current == nullptr) {
143  return m_assets.begin()->second;
144  }
145 
146  auto c = std::find_if(m_assets.begin(), m_assets.end(), [&current](auto& item) { return item.second == current; });
147  if (c == m_assets.end() || (++c) == m_assets.end()) {
148  return nullptr;
149  }
150  return c->second;
151  }
152 
157  void each(std::function<void(T *)> op) {
158  std::for_each(m_assets.begin(), m_assets.end(), [&op] (auto item) { op(item.second); });
159  }
160 
166  T *first(std::function<bool(const T *)> op) {
167  auto found = std::find_if(m_assets.begin(), m_assets.end(), [&op] (auto item) { return op(item.second); });
168  return found == m_assets.end() ? nullptr : found->second;
169  }
170 
176  const std::set<Key>& undefined() const { return m_undefined; }
177 
178  protected:
179  std::map<Key, T*> m_assets;
180  std::set<Key> m_undefined;
187  virtual void replace(T *existing, T *update) = 0;
188 
192  virtual void added(T *) { }
193 };
194 
195 #endif /* ASSETSCOLLECTION_H */
AssetsCollection::count
size_t count() const
Definition: AssetsCollection.h:79
AssetsCollection::undefined
const std::set< Key > & undefined() const
Definition: AssetsCollection.h:176
AssetsCollection::first
T * first(std::function< bool(const T *)> op)
Definition: AssetsCollection.h:166
c
static event_registration c
Definition: citylife.cpp:425
AssetsCollection::added
virtual void added(T *)
Definition: AssetsCollection.h:192
AssetsCollection::find
T * find(const Key &name)
Definition: AssetsCollection.h:107
AssetsCollection::replace
virtual void replace(T *existing, T *update)=0
AssetsCollection::m_assets
std::map< Key, T * > m_assets
Definition: AssetsCollection.h:179
AssetsCollection
Definition: AssetsCollection.h:54
AssetsCollection::define
T * define(const Key &name, T *asset)
Definition: AssetsCollection.h:119
AssetsCollection::m_undefined
std::set< Key > m_undefined
Definition: AssetsCollection.h:180
AssetsCollection::get
T * get(const Key &name)
Definition: AssetsCollection.h:88
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:157
AssetsCollection::next
T * next(T *current)
Definition: AssetsCollection.h:138
AssetsCollection::~AssetsCollection
virtual ~AssetsCollection()
Definition: AssetsCollection.h:61
asset_create
T * asset_create(const Key &)
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
asset_destroy
void asset_destroy(T *)
AssetsCollection::clear
void clear()
Definition: AssetsCollection.h:68
replace.current
current
Definition: replace.py:64
give.name
name
Definition: give.py:27