BARE2D
Cache.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 // We use unordered maps for more efficient single-item access + appendation.
4 #include <functional>
5 #include <string>
6 #include <unordered_map>
7 
8 namespace BARE2D {
9 
10 /**
11  * @class Cache
12  * @brief This is a skeleton cache class. This can ***only*** be used by the ResourceManager or other classes who
13  * actually maintain proper pointer ownership.
14  */
15 template <typename S, typename T> class Cache
16 {
17  public:
19  {}
20 
21  /**
22  * @brief Finds an item based on a key
23  * @param key The key to find the item by.
24  * @return A pointer to the data, or nullptr if no such data exists
25  */
26  T* findItem(S& key);
27 
28  /**
29  * @brief An operator overload that returns the object stored at key. Analagous to findItem();
30  * @param key
31  */
32  T* operator[](S& key);
33 
34  /**
35  * @brief Simply adds an item
36  * @param key The key for the item - how it is stored
37  * @param item The item itself
38  * @return True if successful, false otherwise.
39  */
40  bool addItem(S& key, T* item);
41 
42  /**
43  * @brief Adds or overwrites an item
44  * @param key The key for the item - how it is stored
45  * @param item The item itself
46  * @return True if successful, false otherwise.
47  */
48  bool setItem(S& key, T* item);
49 
50  /**
51  * @brief Creates an item with a given key and uses the constructor to fill out the rest. If it already exists,
52  * overwrite it and return that pointer.
53  * @param key The key to give the item.
54  * @param creationFunc A function that allocates and returns a new object. Defaults, but it can slice data if you're
55  * using inheritance.
56  * @return A pointer to that item.
57  */
58  T* createItem(
59  S& key,
60  std::function<T*()> creationFunc = []() -> T* { return new T(); });
61 
62  /**
63  * @brief Clears the cache. Destroys all data, calling their respective destructors, then delete.
64  */
65  void clear();
66 
67  /**
68  * @brief Returns the number of items in the cache.
69  */
70  unsigned int getCount();
71 
72  // For iterating
73  typename std::unordered_map<S, T*>::iterator begin();
74  typename std::unordered_map<S, T*>::iterator end();
75  bool operator!=(Cache& other);
76 
77  private:
78  std::unordered_map<S, T*> m_data;
79 };
80 
81 } // namespace BARE2D
82 
83 #include "Cache_impl.tcc"
Cache_impl.tcc
BARE2D
Definition: App.cpp:13
BARE2D::Cache::begin
std::unordered_map< S, T * >::iterator begin()
BARE2D::Cache::findItem
T * findItem(S &key)
Finds an item based on a key.
BARE2D::Cache::getCount
unsigned int getCount()
Returns the number of items in the cache.
BARE2D::Cache::addItem
bool addItem(S &key, T *item)
Simply adds an item.
BARE2D::Cache::createItem
T * createItem(S &key, std::function< T *()> creationFunc=[]() -> T *{ return new T();})
Creates an item with a given key and uses the constructor to fill out the rest. If it already exists,...
BARE2D::Cache::m_data
std::unordered_map< S, T * > m_data
Definition: Cache.hpp:78
BARE2D::Cache::setItem
bool setItem(S &key, T *item)
Adds or overwrites an item.
BARE2D::Cache::clear
void clear()
Clears the cache. Destroys all data, calling their respective destructors, then delete.
BARE2D::Cache::operator!=
bool operator!=(Cache &other)
BARE2D::Cache
This is a skeleton cache class. This can only be used by the ResourceManager or other classes who act...
Definition: Cache.hpp:15
BARE2D::Cache::Cache
Cache()
Definition: Cache.hpp:18
BARE2D::Cache::end
std::unordered_map< S, T * >::iterator end()
BARE2D::Cache::operator[]
T * operator[](S &key)
An operator overload that returns the object stored at key. Analagous to findItem();.