CFEL - ASG Software Suite  2.5.0
CASS
cached_list.hpp
Go to the documentation of this file.
1 //Copyright (C) 2013 Lutz Foucar
2 
3 /**
4  * @file cached_list.hpp contains a list for caching results
5  *
6  * @author Lutz Foucar
7  */
8 
9 #ifndef _CACHED_LIST_H_
10 #define _CACHED_LIST_H_
11 
12 #include <vector>
13 #include <algorithm>
14 #include <stdexcept>
15 #include <functional>
16 #include <tr1/memory>
17 #include <tr1/functional>
18 
19 #include <QtCore/QMutex>
20 #include <QtCore/QMutexLocker>
21 
22 #include "result.hpp"
23 #include "cass_event.h"
24 #include "cass_exceptions.hpp"
25 
26 namespace cass
27 {
28 /** a list of results for caching
29  *
30  * @todo make item_type and id_type template parameters
31  *
32  * @author Lutz Foucar
33  */
35 {
36 public:
37  /** define the type of which the list is for */
39 
40  /** define a shared pointer of the item */
41  typedef std::tr1::shared_ptr<item_type> item_sp;
42 
43  /** define the type of the id used */
45 
46  /** bundeling the lock status and the id */
47  struct item_info { id_type id; bool locked; };
48 
49  /** define an entry in the list */
50  typedef std::pair<item_info,item_sp> entry_type;
51 
52  /** define the container of items with their ids */
53  typedef std::vector<entry_type> list_type;
54 
55  /** define an iterator for the list */
56  typedef list_type::iterator iter_type;
57 
58  /** retrieve an item with the right id
59  *
60  * @param id
61  */
62  const item_type &item(const id_type &id)
63  {
64  QMutexLocker lock(&_mutex);
65  iter_type it(findId(id));
66  if (_list.end() == it)
67  throw InvalidResultError(_latest->second->name(),id);
68  return *(it->second);
69  }
70 
71  /** retrieve the latest item
72  *
73  * @return the latest item
74  */
75  const item_type& latest() const
76  {
77  return *(_latest->second);
78  }
79 
80  /** retrieve the latest item
81  *
82  * @return the latest item
83  */
84  item_type& latest()
85  {
86  return *(_latest->second);
87  }
88 
89  /** set which one is the latest item
90  *
91  * change the lock of the item from write lock to read lock
92  *
93  * @param it pointer to the item to be set as latest
94  */
95  void latest(const iter_type it)
96  {
97  QMutexLocker lock(&_mutex);
98  _latest = it;
99  }
100 
101  /** unlock the item with id
102  *
103  * make the entry available again by setting the id to 0
104  *
105  * @param id the id of the item to be released
106  */
107  void release(const id_type &id)
108  {
109  QMutexLocker lock(&_mutex);
110  findId(id)->first.locked = false;
111  }
112 
113  /** get an item for processing
114  *
115  * find the next item in the list that has the eventid not set (e.g.: id is 0)
116  * Set the id to the event id and return the iterator pointing to the entry.
117  *
118  * Clear the item before return it
119  *
120  * @return iterator to the entry that will be allocated for the id
121  * @param id the id that the item will have in the list
122  */
123  iter_type newItem(const id_type &id)
124  {
125  QMutexLocker lock(&_mutex);
126  while(_current->first.locked || _current == _latest)
127  {
128  ++_current;
129  if (_current == _list.end())
130  _current = _list.begin();
131  }
132  _current->first.id = id;
133  _current->first.locked = true;
134  QWriteLocker wlock(&(_current->second->lock));
135  _current->second->clear();
136  return _current;
137  }
138 
139  /** create the list of items
140  *
141  * @param item the template of which copies will be placed in the container
142  * @param size the number of items that should be in the container
143  */
144  void setup(item_sp item, size_t size)
145  {
146  QMutexLocker lock(&_mutex);
147  _list.clear();
148  item_info info;
149  info.id = 0;
150  info.locked = false;
151  for (size_t i=0; i<size; ++i)
152  _list.push_back(std::make_pair(info,item->clone()));
153  _latest = _current = _list.begin();
154  }
155 
156  /** clear the items in the list
157  *
158  * lock and go through all items, lock them and clear them. After they have
159  * been cleard unlock them again.
160  */
161  void clearItems()
162  {
163  QMutexLocker lock(&_mutex);
164  iter_type it(_list.begin());
165  iter_type End(_list.end());
166  while (it != End)
167  {
168  QWriteLocker lock(&(it->second->lock));
169  it->second->clear();
170  }
171  }
172 
173 private:
174  /** get an iterator to the item for id
175  *
176  * @param id the id for which the entry should be returned
177  */
178  iter_type findId(const id_type &id)
179  {
180  using std::find_if;
181  using std::equal_to;
182  using std::tr1::bind;
183  using std::tr1::placeholders::_1;
184 
185  return find_if(_list.begin(), _list.end(),
186  std::tr1::bind(equal_to<id_type>(),id,
187  std::tr1::bind<const id_type&>(&item_info::id,
188  std::tr1::bind<const item_info&>(&entry_type::first,_1))));
189  }
190 
191 private:
192  /** the list */
193  list_type _list;
194 
195  /** mutex for locking the internal list */
197 
198  /** iterator to the latest entry */
199  iter_type _latest;
200 
201  /** iterator the currently used entry */
202  iter_type _current;
203 };
204 
205 }//end namespace cass
206 #endif
iter_type _current
iterator the currently used entry
void latest(const iter_type it)
set which one is the latest item
Definition: cached_list.hpp:95
const item_type & latest() const
retrieve the latest item
Definition: cached_list.hpp:75
file contains declaration of the CASSEvent
void clearItems()
clear the items in the list
uint64_t id_t
define the id type
Definition: cass_event.h:52
things written only at end of run H5Dump ProcessorSummary size
item_type & latest()
retrieve the latest item
Definition: cached_list.hpp:84
result classes
QMutex _mutex
mutex for locking the internal list
file contains custom exceptions used in cass
list_type _list
the list
additional info
a list of results for caching
Definition: cached_list.hpp:34
iter_type newItem(const id_type &id)
get an item for processing
iter_type findId(const id_type &id)
get an iterator to the item for id
void setup(item_sp item, size_t size)
create the list of items
void release(const id_type &id)
unlock the item with id
CASSEvent::id_t id_type
define the type of the id used
Definition: cached_list.hpp:44
iter_type _latest
iterator to the latest entry
std::pair< item_info, item_sp > entry_type
define an entry in the list
Definition: cached_list.hpp:50
std::tr1::shared_ptr< item_type > item_sp
define a shared pointer of the item
Definition: cached_list.hpp:41
list_type::iterator iter_type
define an iterator for the list
Definition: cached_list.hpp:56
std::vector< entry_type > list_type
define the container of items with their ids
Definition: cached_list.hpp:53
Exception thrown when accessing invalid histogram.
const item_type & item(const id_type &id)
retrieve an item with the right id
Definition: cached_list.hpp:62
Result< float > item_type
define the type of which the list is for
Definition: cached_list.hpp:38
bundeling the lock status and the id
Definition: cached_list.hpp:47