fifo_cache.h
Go to the documentation of this file.
1 #ifndef __fifo_cache__
2 #define __fifo_cache__
3 
4 // STL
5 #include <cassert>
6 #include <map>
7 #include <vector>
8 
9 // Boost
10 #include <boost/noncopyable.hpp>
11 #include <boost/cstdint.hpp>
12 
13 template<class T> class fifo_cache: public boost::noncopyable
14 {
15  public:
16 
17  fifo_cache(std::size_t max_size);
18 
19  ~fifo_cache();
20 
21  const T* data(boost::uint64_t id) const;
22 
23  bool is_full() const;
24 
25  T* pop();
26 
27  void push(boost::uint64_t id, T* data);
28 
29  private:
30 
31  std::map<boost::uint64_t, T*> m_fifos;
32  std::vector<boost::uint64_t> m_fifo;
33  std::size_t m_size;
34  std::size_t m_max_size;
35  std::size_t m_fifo_idx;
36 };
37 
38 template<class T> inline fifo_cache<T>::fifo_cache(std::size_t max_size)
39  : m_size(0),
40  m_max_size(max_size),
41  m_fifo_idx(0)
42 {
43  m_fifo.resize(max_size, 0);
44 }
45 
46 template<class T> inline fifo_cache<T>::~fifo_cache()
47 {
48 // TODO: apagar
49 }
50 
51 template<class T> inline const T* fifo_cache<T>::data(boost::uint64_t id) const
52 {
53  std::map<boost::uint64_t, T*>::const_iterator it = m_fifos.find(id);
54 
55  return (it != m_fifos.end()) ? it->second : 0;
56 }
57 
58 template<class T> inline bool fifo_cache<T>::is_full() const
59 {
60  return m_size == m_max_size;
61 }
62 
63 template<class T> inline T* fifo_cache<T>::pop()
64 {
65  if(m_size == 0)
66  throw std::runtime_error("fifo cache is empty!");
67 
68  assert(m_fifos.find(m_fifo[m_fifo_idx]) != m_fifos.end());
69 
70  T* d = m_fifos[m_fifo[m_fifo_idx] ];
71 
72  --m_size;
73 
74  return d;
75 }
76 
77 template<class T> inline void fifo_cache<T>::push(boost::uint64_t id, T* data)
78 {
79  if(is_full())
80  throw std::runtime_error("fifo cache is full!");
81 
82  std::map<boost::uint64_t, T*>::const_iterator it = m_fifos.find(id);
83 
84  if(it != m_fifos.end())
85  throw std::runtime_error("fifo cache already has an entry for this fifo!");
86 
87  m_fifos[id] = data;
88 
89  ++m_size;
90 
91  m_fifo[m_fifo_idx] = id;
92 
94 }
95 
96 #endif // __fifo_cache__
97 
std::size_t m_fifo_idx
Definition: fifo_cache.h:35
T * pop()
Definition: fifo_cache.h:63
std::map< boost::uint64_t, T * > m_fifos
Definition: fifo_cache.h:31
bool is_full() const
Definition: fifo_cache.h:58
fifo_cache(std::size_t max_size)
Definition: fifo_cache.h:38
std::size_t m_max_size
Definition: fifo_cache.h:34
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
const T * data(boost::uint64_t id) const
Definition: fifo_cache.h:51
std::vector< boost::uint64_t > m_fifo
Definition: fifo_cache.h:32
std::size_t m_size
Definition: fifo_cache.h:33
void push(boost::uint64_t id, T *data)
Definition: fifo_cache.h:77