2 // This holds all the non-specialized templates for Cache.hpp
4 #include "BAREErrors.hpp"
8 template <typename S, typename T> T* Cache<S, T>::findItem(S& key)
10 // Check if we have the item in the m_data map
12 auto iter = m_data.find(key);
14 if(iter == m_data.end())
23 template <typename S, typename T> T* Cache<S, T>::operator[](S& key)
28 template <typename S, typename T> bool Cache<S, T>::addItem(S& key, T* item)
36 template <typename S, typename T> bool Cache<S, T>::setItem(S& key, T* item)
46 template <typename S, typename T> T* Cache<S, T>::createItem(S& key, std::function<T*()> creationFunc)
48 // Creates an item, or overwrites the existing item if it already exists.
50 // Check if it already exists
51 T* object = findItem(key);
53 // nullptr if object doesn't already exist
56 throwFatalError(BAREError::CACHE_FAILURE, "Item was created twice.");
60 // Now we know the object doesn't exist, so we need to create one!
61 object = creationFunc();
63 m_data.insert(std::make_pair(key, object));
68 template <typename S, typename T> void Cache<S, T>::clear()
70 // Deletes all in m_data, then clears m_data.
72 for(std::pair<S, T*> i : m_data)
81 template <typename S, typename T> unsigned int Cache<S, T>::getCount()
86 template <typename S, typename T> typename std::unordered_map<S, T*>::iterator Cache<S, T>::begin()
88 return m_data.begin();
91 template <typename S, typename T> typename std::unordered_map<S, T*>::iterator Cache<S, T>::end()
96 template <typename S, typename T> bool Cache<S, T>::operator!=(Cache& other)
98 return m_data != other.m_data;
101 } // namespace BARE2D