23 typedef std::vector<T> vec_type;
24 typedef typename vec_type::const_iterator const_iterator;
25 typedef typename vec_type::const_reverse_iterator const_reverse_iterator;
49 if((a >= _a) && (b <= _b))
56 _val.resize(_b - _a,T());
63 { _val.insert(_val.begin(),_a -
a,T()); }
65 { _val.insert(_val.end(),b - _b,T()); }
91 {
return _val[i - _a]; }
93 {
return _val[i - _a]; }
94 const T& at(
int i)
const 95 {
return _val.at(i - _a); }
97 {
return _val.at(i - _a); }
100 {
return _val.begin(); }
101 const_iterator end()
const 102 {
return _val.end(); }
104 {
return _val.empty(); }
107 {
return _val.size(); }
131 cache_line_buffer_type *_buffer;
133 line_ref(cache_line_buffer_type& b):_buffer(&b)
136 {
return _buffer->
a(); }
138 {
return _buffer->b(); }
140 {
return (*_buffer)[i]; }
142 {
return (*_buffer)[i]; }
143 bool exists(
int i)
const 144 {
return (
a() <= i) && (i < b()); }
146 operator bool()
const 163 mutable CRITICAL_SECTION _critsec;
165 mutable pthread_mutex_t _mutex;
167 typedef std::map<int,cache_line_buffer_type> buffers_type;
168 buffers_type _buffers;
171 buffer_refs_type _buffer_refs;
176 InitializeCriticalSection(&_critsec);
178 int r = pthread_mutex_init(&_mutex,NULL);
180 { std::cerr <<
"ldpk::ldp_builtin::pthread_mutex_init: " << strerror(r) << std::endl; }
186 DeleteCriticalSection(&_critsec);
188 int r = pthread_mutex_destroy(&_mutex);
190 { std::cerr <<
"ldpk::ldp_builtin::pthread_mutex_destroy: " << strerror(r) << std::endl; }
196 _buffer_refs.clear();
198 bool line_exists(
int iy)
const 200 return _buffer_refs.at(iy);
206 EnterCriticalSection(&_critsec);
208 pthread_mutex_lock(&_mutex);
213 cache_line_buffer_type& buffer =
static_cast<cache_line_buffer_type&
>(_buffers[iy].resize(a,b));
216 _buffer_refs.at(iy) = &buffer;
219 LeaveCriticalSection(&_critsec);
221 pthread_mutex_unlock(&_mutex);
223 return line_ref_type(buffer);
225 catch(
const std::exception& e)
227 std::cerr <<
"ldpk::line_cache::resize_line(" << iy <<
"," << a <<
"," << b <<
") failed: " << e.what() << std::endl;
229 LeaveCriticalSection(&_critsec);
231 pthread_mutex_unlock(&_mutex);
237 std::cerr <<
"ldpk::line_cache::resize_line(" << iy <<
"," << a <<
"," << b <<
") failed: unknown exception" << std::endl;
239 LeaveCriticalSection(&_critsec);
241 pthread_mutex_unlock(&_mutex);
250 EnterCriticalSection(&_critsec);
252 pthread_mutex_lock(&_mutex);
257 cache_line_buffer_type *buffer = _buffer_refs.at(iy);
259 {
throw std::range_error(
"ldpk::line_cache::get_line"); }
261 LeaveCriticalSection(&_critsec);
263 pthread_mutex_unlock(&_mutex);
265 return line_ref_type(*buffer);
267 catch(
const std::exception& e)
269 std::cerr <<
"ldpk::line_cache::get_line(" << iy <<
") failed: " << e.what() << std::endl;
271 LeaveCriticalSection(&_critsec);
273 pthread_mutex_unlock(&_mutex);
279 std::cerr <<
"ldpk::line_cache::resize_line(" << iy <<
") failed: unknown exception" << std::endl;
281 LeaveCriticalSection(&_critsec);
283 pthread_mutex_unlock(&_mutex);
290 {
return _buffer_refs.
size(); }
291 friend std::ostream& operator<<(std::ostream& cout,
const this_type& c)
293 for(
int i = c._buffer_refs.
a();i < c._buffer_refs.b();++i)
298 cout <<
"line " << i <<
": " << l.a() <<
"," << l.b() << std::endl;
const_iterator begin() const
Iterate over the entire container, not just [a,b[.
Definition: ldpk_line_cache.h:99
void extend(int i)
After this call (*this)[i] is valid.
Definition: ldpk_line_cache.h:74
const T & operator[](int i) const
Since resize() will move elements around, the access methods are fast.
Definition: ldpk_line_cache.h:90
int size() const
Entire size, not just [a,b[.
Definition: ldpk_line_cache.h:106
In Multithreading, more than one thread may work on the same line reading and writing, however resizing is not allowed.
Definition: ldpk_line_cache.h:126
Definition: ldpk_line_cache.h:19
The namespace of (most of the) things related to the Lens Distortion Plugin Kit.
Definition: ldpk.h:180
this_type & resize(int a, int b)
After this call (*this)[i] is valid for a <= i < b. Expensive. This method will only do something if ...
Definition: ldpk_line_cache.h:43
int a() const
Minimum and maximum values ever set with resize() since object was created.
Definition: ldpk_line_cache.h:85
int size() const
Number of Buffer-Refs.
Definition: ldpk_line_cache.h:289
The class is thread-safe in the following sense:
Definition: ldpk_line_cache.h:155
line_ref_type get_line(int iy) const
Return reference to line_buffer if exists, otherwise exception.
Definition: ldpk_line_cache.h:247
Definition: ldpk_line_cache.h:110
line_ref_type resize_line(int iy, int a, int b)
Protected by mutex. By this method, line iy is created -if not exists- and initialized.
Definition: ldpk_line_cache.h:203