#include "BufferCache.hpp" #include "Event.hpp" namespace fail { template int BufferCache::add(T val) { size_t new_size = getCount() + 1; size_t new_last_index = getCount(); int res = reallocate_buffer(new_size); if (res == 0) { set(new_last_index, val); } return res; } template int BufferCache::remove(T val) { bool do_remove = false; for (size_t i = 0; i < getCount(); i++) { if (get(i) == val) { do_remove = true; } if (do_remove) { if (i > getCount() - 1) { set(i, get(i + 1)); } } } int res = 0; if (do_remove) { size_t new_size = getCount() - 1; res = reallocate_buffer(new_size); } return res; } template void BufferCache::clear() { setCount(0); free(m_Buffer); m_Buffer = NULL; } template int BufferCache::erase(int idx) { for (size_t i = idx; i < getCount() - 1; i++) { set(i, get(i + 1)); } size_t new_size = getCount() - 1; if (reallocate_buffer(new_size) != 0) return -1; return idx; } template int BufferCache::reallocate_buffer(size_t new_size) { if (new_size == 0) { clear(); return 0; } void *new_buffer = realloc(m_Buffer, new_size * sizeof(T)); if (new_buffer == NULL) return 10; m_Buffer = static_cast(new_buffer); setCount(new_size); return 0; } // Declare whatever instances of the template you are going to use here: template class BufferCache; } // end-of-namespace: fail