#include "BufferCache.hpp" #include "Event.hpp" namespace fi { template int BufferCache::add(T val) { size_t new_size = get_count() + 1; size_t new_last_index = get_count(); 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 < get_count(); i++) { if (get(i) == val) { do_remove = true; } if (do_remove) { if (i > get_count() - 1) { set(i, get(i + 1)); } } } int res = 0; if (do_remove) { size_t new_size = get_count() - 1; res = reallocate_buffer(new_size); } return res; } template void BufferCache::clear() { set_count(0); free(m_Buffer); m_Buffer = NULL; } template int BufferCache::erase(int idx) { for (size_t i = idx; i < get_count() - 1; i++) { set(i, get(i + 1)); } size_t new_size = get_count() - 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); set_count(new_size); return 0; } //declare whatever instances of the template //you are going to use here: template class BufferCache; } /* namespace fi */