Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename _type, size_t capacity>
- class StaticFIFO
- {
- size_t m_allocated = capacity;
- size_t m_size = 0;
- size_t m_head = 0;
- size_t m_tail = 0; // must point to free cell
- _type* m_data = nullptr;
- public:
- StaticFIFO()
- {
- if (!m_allocated)
- m_allocated = 1;
- m_data = new _type[m_allocated];
- }
- ~StaticFIFO()
- {
- if (m_data)
- delete[] m_data;
- }
- bool Add(const _type& object)
- {
- if (m_size == m_allocated)
- return false;
- m_data[m_tail] = object;
- ++m_size;
- ++m_tail;
- if (m_tail == m_allocated)
- m_tail = 0;
- return true;
- }
- bool Get(_type& object)
- {
- if (!m_size)
- return false;
- object = m_data[m_head];
- --m_size;
- ++m_head;
- if (m_head == m_allocated)
- m_head = 0;
- return true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement