μHAL (v2.6.5)
Part of the IPbus software repository
ValMem.cpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Andrew Rose, Imperial College, London
25  email: awr01 <AT> imperial.ac.uk
26 
27  Marc Magrans de Abril, CERN
28  email: marc.magrans.de.abril <AT> cern.ch
29 
30 ---------------------------------------------------------------------------
31 */
32 
33 #include "uhal/ValMem.hpp"
34 
35 
36 #include "uhal/log/log.hpp"
37 #include "uhal/utilities/bits.hpp"
38 
39 
40 namespace uhal
41 {
42 
43  _ValHeader_::_ValHeader_ ( const bool& aValid ) :
44  valid ( aValid )
45  {
46  }
47 
48 
49  template< typename T >
50 
51  _ValWord_<T>::_ValWord_ ( const T& aValue , const bool& aValid , const uint32_t aMask ) :
52  _ValHeader_ ( aValid ) ,
53  value ( aValue ) ,
54  mask ( aMask )
55  {
56  }
57 
58 
59 
60  template< typename T >
61 
62  _ValVector_<T>::_ValVector_ ( const std::vector<T>& aValue , const bool& aValid ) :
63  _ValHeader_ ( aValid ),
64  value ( aValue )
65  {
66  }
67 
68 
69 
70 
71 
72 
74  mMembers ( new _ValHeader_ ( false ) )
75  {
76  }
77 
78 
80  {
81  return mMembers->valid;
82  }
83 
84  void ValHeader::valid ( bool aValid )
85  {
86  mMembers->valid = aValid;
87  }
88 
89 
90  // const std::deque<uint32_t>& ValHeader::returnedHeaders()
91  // {
92  // try
93  // {
94  // if ( mMembers->valid )
95  // {
96  // return mMembers->IPbusHeaders ;
97  // }
98  // else
99  // {
100  // log ( Error() , "Access attempted on non-validated memory" );
101  // throw exception::// NonValidatedMemory();
102  // }
103  // }
104  // catch ( uhal::exception& aExc )
105  // {
106  // aExc.throw r;
107  // }
108  // catch ( const std::exception& aExc )
109  // {
110  // throw // StdException ( aExc );
111  // }
112  // }
113 
114 
115 
116 
117  template< typename T >
118 
119  ValWord< T >::ValWord ( const T& aValue , const uint32_t& aMask ) :
120  mMembers ( new _ValWord_<T> ( aValue , false , aMask ) )
121  {
122  }
123 
124 
125  template< typename T >
126 
128  mMembers ( aVal.mMembers )
129  {
130  }
131 
132  template< typename T >
133 
135  mMembers ( new _ValWord_<T> ( T() , false , 0xFFFFFFFF ) )
136  {
137  }
138 
139  template< typename T >
141  {
142  return mMembers->valid;
143  }
144 
145  template< typename T >
146  void ValWord< T >::valid ( bool aValid )
147  {
148  mMembers->valid = aValid;
149  }
150 
151  template< typename T >
153  {
154  mMembers->value = aValue ;
155  return *this;
156  }
157 
158  // template< typename T >
159  // ValWord< T >::operator const T&()
160  // {
161  // return value();
162  // }
163 
164  // template< typename T >
165  // const T& ValWord< T >::value() const
166  // {
167  // if ( mMembers->valid )
168  // {
169  // return mMembers->value;
170  // }
171  // else
172  // {
173  //
174  // NonValidatedMemory().throwFrom( ThisLocation() );
175  // }
176  // }
177 
178  template< typename T >
180  {
181  return value();
182  }
183 
184  template< typename T >
186  {
187  if ( mMembers->valid )
188  {
189  return ( mMembers->value & mMembers->mask ) >> utilities::TrailingRightBits ( mMembers->mask ) ;
190  }
191  else
192  {
193  exception::NonValidatedMemory lExc;
194  log ( lExc , "Access attempted on non-validated memory" );
195  throw lExc;
196  }
197  }
198 
199  template< typename T >
200  void ValWord< T >::value ( const T& aValue )
201  {
202  if ( !mMembers->valid )
203  {
204  mMembers->value = aValue;
205  }
206  else
207  {
208  exception::ValMemImutabilityViolation lExc;
209  log ( lExc , "Attempted to modify validated memory" );
210  throw lExc;
211  }
212  }
213 
214  template< typename T >
215  const uint32_t& ValWord< T >::mask() const
216  {
217  return mMembers->mask;
218  }
219 
220  template< typename T >
221  void ValWord< T >::mask ( const uint32_t& aMask )
222  {
223  mMembers->mask = aMask ;
224  }
225 
226 
227  // template< typename T >
228  // const std::deque<uint32_t>& ValWord< T >::returnedHeaders()
229  // {
230  // try
231  // {
232  // if ( mMembers->valid )
233  // {
234  // return mMembers->IPbusHeaders ;
235  // }
236  // else
237  // {
238  // log ( Error() , "Access attempted on non-validated memory" );
239  // throw exception::// NonValidatedMemory();
240  // }
241  // }
242  // catch ( uhal::exception& aExc )
243  // {
244  // aExc.throw r;
245  // }
246  // catch ( const std::exception& aExc )
247  // {
248  // throw // StdException ( aExc );
249  // }
250  // }
251 
252 
253 
254 
255 
256 
257  template< typename T >
258 
259  ValVector< T >::ValVector ( const std::vector<T>& aValues ) :
260  mMembers ( new _ValVector_<T> ( aValues , false ) )
261  {
262  }
263 
264 
265  template< typename T >
266 
268  mMembers ( aValues.mMembers )
269  {
270  }
271 
272 
273  template< typename T >
274 
275  ValVector< T >::ValVector ( const uint32_t& aSize ) :
276  mMembers ( new _ValVector_<T> ( std::vector<T> ( aSize , T() ) , false ) )
277  {
278  }
279 
280 
281  template< typename T >
282 
284  mMembers ( new _ValVector_<T> ( std::vector<T>() , false ) )
285  {
286  }
287 
288 
289  template< typename T >
291  {
292  return mMembers->valid;
293  }
294 
295  template< typename T >
296  void ValVector< T >::valid ( bool aValid )
297  {
298  mMembers->valid = aValid;
299  }
300 
301 
302 
303  template< typename T >
304  void ValVector< T >::push_back ( const T& aValue )
305  {
306  if ( !mMembers->valid )
307  {
308  mMembers->value.push_back ( aValue );
309  }
310  else
311  {
312  exception::ValMemImutabilityViolation lExc;
313  log ( lExc , "Attempted to modify validated memory" );
314  throw lExc;
315  }
316  }
317 
318  template< typename T >
319  const T& ValVector< T >::operator[] ( std::size_t aIndex ) const
320  {
321  if ( mMembers->valid )
322  {
323  return ( mMembers->value ) [aIndex];
324  }
325  else
326  {
327  exception::NonValidatedMemory lExc;
328  log ( lExc , "Access attempted on non-validated memory" );
329  throw lExc;
330  }
331  }
332 
333  template< typename T >
334  const T& ValVector< T >::at ( std::size_t aIndex ) const
335  {
336  if ( mMembers->valid )
337  {
338  return mMembers->value.at ( aIndex );
339  }
340  else
341  {
342  exception::NonValidatedMemory lExc;
343  log ( lExc , "Access attempted on non-validated memory" );
344  throw lExc;
345  }
346  }
347 
348  template< typename T >
349  std::size_t ValVector< T >::size() const
350  {
351  return mMembers->value.size();
352  /*
353  if ( mMembers->valid )
354  {
355  return mMembers->value.size();
356  }
357  else
358  {
359  log ( Error() , "Access attempted on non-validated memory" );
360 
361  NonValidatedMemory().throwFrom( ThisLocation() );
362  }
363  */
364  }
365 
366  template< typename T >
368  {
369  mMembers->valid = false;
370  mMembers->value.clear();
371  }
372 
373  template< typename T >
375  {
376  if ( mMembers->valid )
377  {
378  return mMembers->value.begin();
379  }
380  else
381  {
382  exception::NonValidatedMemory lExc;
383  log ( lExc , "Access attempted on non-validated memory" );
384  throw lExc;
385  }
386  }
387 
388  template< typename T >
390  {
391  if ( mMembers->valid )
392  {
393  return mMembers->value.end();
394  }
395  else
396  {
397  exception::NonValidatedMemory lExc;
398  log ( lExc , "Access attempted on non-validated memory" );
399  throw lExc;
400  }
401  }
402 
403  template< typename T >
405  {
406  if ( mMembers->valid )
407  {
408  return mMembers->value.rbegin();
409  }
410  else
411  {
412  exception::NonValidatedMemory lExc;
413  log ( lExc , "Access attempted on non-validated memory" );
414  throw lExc;
415  }
416  }
417 
418  template< typename T >
420  {
421  if ( mMembers->valid )
422  {
423  return mMembers->value.rend();
424  }
425  else
426  {
427  exception::NonValidatedMemory lExc;
428  log ( lExc , "Access attempted on non-validated memory" );
429  throw lExc;
430  }
431  }
432 
433 
434  template< typename T >
435  std::vector<T> ValVector< T >::value() const
436  {
437  if ( mMembers->valid )
438  {
439  return mMembers->value;
440  }
441  else
442  {
443  exception::NonValidatedMemory lExc;
444  log ( lExc , "Access attempted on non-validated memory" );
445  throw lExc;
446  }
447  }
448 
449  template< typename T >
450  void ValVector< T >::value ( const std::vector<T>& aValue )
451  {
452  if ( !mMembers->valid )
453  {
454  mMembers->value = aValue;
455  }
456  else
457  {
458  exception::ValMemImutabilityViolation lExc;
459  log ( lExc , "Attempted to modify validated memory" );
460  throw lExc;
461  }
462  }
463 
464  /*
465  template< typename T >
466  typename ValVector< T >::iterator ValVector< T >::begin()
467  {
468  try
469  {
470  if ( !mMembers->valid )
471  {
472  return mMembers->value.begin();
473  }
474  else
475  {
476  log ( Error() , "Attempted to modify validated memory. If you do not intend to modify the memory, please use a const_iterator." );
477 
478  ValMemImutabilityViolation().throwFrom( ThisLocation() );
479  }
480  }
481  catch ( uhal::exception& aExc )
482  {
483  aExc.rethrowFrom( ThisLocation() );
484  }
485  catch ( const std::exception& aExc )
486  {
487  log ( Error() , "Exception " , Quote( aExc.what() ) , " caught at " , ThisLocation() );
488  StdException ( aExc ).throwFrom( ThisLocation() );
489  }
490  }
491 
492  template< typename T >
493  typename ValVector< T >::iterator ValVector< T >::end()
494  {
495  try
496  {
497  if ( !mMembers->valid )
498  {
499  return mMembers->value.end();
500  }
501  else
502  {
503  log ( Error() , "Attempted to modify validated memory. If you do not intend to modify the memory, please use a const_iterator." );
504 
505  ValMemImutabilityViolation().throwFrom( ThisLocation() );
506  }
507  }
508  catch ( uhal::exception& aExc )
509  {
510  aExc.rethrowFrom( ThisLocation() );
511  }
512  catch ( const std::exception& aExc )
513  {
514  log ( Error() , "Exception " , Quote( aExc.what() ) , " caught at " , ThisLocation() );
515  StdException ( aExc ).throwFrom( ThisLocation() );
516  }
517  }
518 
519  template< typename T >
520  typename ValVector< T >::reverse_iterator ValVector< T >::rbegin()
521  {
522  try
523  {
524  if ( !mMembers->valid )
525  {
526  return mMembers->value.rbegin();
527  }
528  else
529  {
530  log ( Error() , "Attempted to modify validated memory. If you do not intend to modify the memory, please use a const_reverse_iterator." );
531 
532  ValMemImutabilityViolation().throwFrom( ThisLocation() );
533  }
534  }
535  catch ( uhal::exception& aExc )
536  {
537  aExc.rethrowFrom( ThisLocation() );
538  }
539  catch ( const std::exception& aExc )
540  {
541  log ( Error() , "Exception " , Quote( aExc.what() ) , " caught at " , ThisLocation() );
542  StdException ( aExc ).throwFrom( ThisLocation() );
543  }
544  }
545 
546  template< typename T >
547  typename ValVector< T >::reverse_iterator ValVector< T >::rend()
548  {
549  try
550  {
551  if ( !mMembers->valid )
552  {
553  return mMembers->value.rend();
554  }
555  else
556  {
557  log ( Error() , "Attempted to modify validated memory. If you do not intend to modify the memory, please use a const_iterator." );
558 
559  ValMemImutabilityViolation().throwFrom( ThisLocation() );
560  }
561  }
562  catch ( uhal::exception& aExc )
563  {
564  aExc.rethrowFrom( ThisLocation() );
565  }
566  catch ( const std::exception& aExc )
567  {
568  log ( Error() , "Exception " , Quote( aExc.what() ) , " caught at " , ThisLocation() );
569  StdException ( aExc ).throwFrom( ThisLocation() );
570  }
571  }
572  */
573 
574 
575  // template< typename T >
576  // const std::deque<uint32_t>& ValVector< T >::returnedHeaders()
577  // {
578  // try
579  // {
580  // if ( mMembers->valid )
581  // {
582  // return mMembers->IPbusHeaders ;
583  // }
584  // else
585  // {
586  // log ( Error() , "Access attempted on non-validated memory" );
587  // throw exception::// NonValidatedMemory();
588  // }
589  // }
590  // catch ( uhal::exception& aExc )
591  // {
592  // aExc.throw r;
593  // }
594  // catch ( const std::exception& aExc )
595  // {
596  // throw // StdException ( aExc );
597  // }
598  // }
599 
600 
601  template class ValWord< uint8_t >;
602  template class ValWord< uint32_t >;
603 
604  template class ValVector< uint8_t >;
605  template class ValVector< uint32_t >;
606 
607 
608 }
boost::shared_ptr< _ValHeader_ > mMembers
Return a std::deque containing all the IPbus headers returned during the transaction.
Definition: ValMem.hpp:202
A class which wraps a single word of data and marks whether or not it is valid.
Definition: ValMem.hpp:74
std::vector< T >::const_iterator const_iterator
typedef iterator to be that of the underlying storage type
Definition: ValMem.hpp:305
std::vector< T >::const_reverse_iterator const_reverse_iterator
typedef iterator to be that of the underlying storage type
Definition: ValMem.hpp:309
T value() const
Return the value of the validated memory with check on validity.
Definition: ValMem.cpp:185
A class which wraps a block of data and marks whether or not it is valid.
Definition: ValMem.hpp:76
ValHeader()
Default constructor.
Definition: ValMem.cpp:73
A Template helper struct wrapping an IPbus header, a register for storing a single word of data...
Definition: ValMem.hpp:103
const uint32_t & mask() const
Return the mask used by this validated memory.
Definition: ValMem.cpp:215
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:79
A helper struct wrapping an IPbus header and a valid flag.
Definition: ValMem.hpp:80
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:290
T value
A register for storing data.
Definition: ValMem.hpp:107
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:140
std::vector< T > value() const
Return the value of the validated memory with check on validity.
Definition: ValMem.cpp:435
_ValWord_(const T &aValue, const bool &aValid, const uint32_t aMask)
Constructor Private, since this struct should only be used by the ValWord.
Definition: ValMem.cpp:51
lDepths push_back(N_200MB)
_ValVector_(const std::vector< T > &aValue, const bool &aValid)
Constructor Private, since this struct should only be used by the ValVector.
Definition: ValMem.cpp:62
unsigned int TrailingRightBits(uint32_t aValue)
Helper function to calculate the number of zero-bits at the righthand end of a 32-bit number...
Definition: bits.cpp:44
ValWord()
Default constructor.
Definition: ValMem.cpp:134
A Template helper struct wrapping a block of IPbus header, a register for storing a block of data and...
Definition: ValMem.hpp:131
_ValHeader_(const bool &aValid)
Constructor Private, since this struct should only be used by the ValHeader.
Definition: ValMem.cpp:43