μHAL (v2.7.9)
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 
72  mMembers ( new _ValHeader_ ( false ) )
73  {
74  }
75 
76 
78  {
79  return mMembers->valid;
80  }
81 
82  void ValHeader::valid ( bool aValid )
83  {
84  mMembers->valid = aValid;
85  }
86 
87 
88 
89 
90  template< typename T >
91  ValWord< T >::ValWord ( const T& aValue , const uint32_t& aMask ) :
92  mMembers ( new _ValWord_<T> ( aValue , false , aMask ) )
93  {
94  }
95 
96 
97  template< typename T >
99  mMembers ( aVal.mMembers )
100  {
101  }
102 
103 
104  template< typename T >
106  mMembers ( new _ValWord_<T> ( T() , false , 0xFFFFFFFF ) )
107  {
108  }
109 
110 
111  template< typename T >
113  {
114  return mMembers->valid;
115  }
116 
117 
118  template< typename T >
119  void ValWord< T >::valid ( bool aValid )
120  {
121  mMembers->valid = aValid;
122  }
123 
124 
125  template< typename T >
127  {
128  mMembers->value = aValue ;
129  return *this;
130  }
131 
132 
133  template< typename T >
135  {
136  return value();
137  }
138 
139 
140  template< typename T >
142  {
143  if ( mMembers->valid )
144  {
145  return ( mMembers->value & mMembers->mask ) >> utilities::TrailingRightBits ( mMembers->mask ) ;
146  }
147  else
148  {
149  exception::NonValidatedMemory lExc;
150  log ( lExc , "Access attempted on non-validated memory" );
151  throw lExc;
152  }
153  }
154 
155 
156  template< typename T >
157  void ValWord< T >::value ( const T& aValue )
158  {
159  if ( !mMembers->valid )
160  {
161  mMembers->value = aValue;
162  }
163  else
164  {
165  exception::ValMemImutabilityViolation lExc;
166  log ( lExc , "Attempted to modify validated memory" );
167  throw lExc;
168  }
169  }
170 
171 
172  template< typename T >
173  const uint32_t& ValWord< T >::mask() const
174  {
175  return mMembers->mask;
176  }
177 
178 
179  template< typename T >
180  void ValWord< T >::mask ( const uint32_t& aMask )
181  {
182  mMembers->mask = aMask ;
183  }
184 
185 
186 
187 
188  template< typename T >
189  ValVector< T >::ValVector ( const std::vector<T>& aValues ) :
190  mMembers ( new _ValVector_<T> ( aValues , false ) )
191  {
192  }
193 
194 
195  template< typename T >
197  mMembers ( aValues.mMembers )
198  {
199  }
200 
201 
202  template< typename T >
203  ValVector< T >::ValVector ( const uint32_t& aSize ) :
204  mMembers ( new _ValVector_<T> ( std::vector<T> ( aSize , T() ) , false ) )
205  {
206  }
207 
208 
209  template< typename T >
211  mMembers ( new _ValVector_<T> ( std::vector<T>() , false ) )
212  {
213  }
214 
215 
216  template< typename T >
218  {
219  return mMembers->valid;
220  }
221 
222 
223  template< typename T >
224  void ValVector< T >::valid ( bool aValid )
225  {
226  mMembers->valid = aValid;
227  }
228 
229 
230 
231  template< typename T >
232  void ValVector< T >::push_back ( const T& aValue )
233  {
234  if ( !mMembers->valid )
235  {
236  mMembers->value.push_back ( aValue );
237  }
238  else
239  {
240  exception::ValMemImutabilityViolation lExc;
241  log ( lExc , "Attempted to modify validated memory" );
242  throw lExc;
243  }
244  }
245 
246 
247  template< typename T >
248  const T& ValVector< T >::operator[] ( std::size_t aIndex ) const
249  {
250  if ( mMembers->valid )
251  {
252  return ( mMembers->value ) [aIndex];
253  }
254  else
255  {
256  exception::NonValidatedMemory lExc;
257  log ( lExc , "Access attempted on non-validated memory" );
258  throw lExc;
259  }
260  }
261 
262 
263  template< typename T >
264  const T& ValVector< T >::at ( std::size_t aIndex ) const
265  {
266  if ( mMembers->valid )
267  {
268  return mMembers->value.at ( aIndex );
269  }
270  else
271  {
272  exception::NonValidatedMemory lExc;
273  log ( lExc , "Access attempted on non-validated memory" );
274  throw lExc;
275  }
276  }
277 
278 
279  template< typename T >
280  std::size_t ValVector< T >::size() const
281  {
282  return mMembers->value.size();
283  }
284 
285 
286  template< typename T >
288  {
289  mMembers->valid = false;
290  mMembers->value.clear();
291  }
292 
293 
294  template< typename T >
296  {
297  if ( mMembers->valid )
298  {
299  return mMembers->value.begin();
300  }
301  else
302  {
303  exception::NonValidatedMemory lExc;
304  log ( lExc , "Access attempted on non-validated memory" );
305  throw lExc;
306  }
307  }
308 
309 
310  template< typename T >
312  {
313  if ( mMembers->valid )
314  {
315  return mMembers->value.end();
316  }
317  else
318  {
319  exception::NonValidatedMemory lExc;
320  log ( lExc , "Access attempted on non-validated memory" );
321  throw lExc;
322  }
323  }
324 
325 
326  template< typename T >
328  {
329  if ( mMembers->valid )
330  {
331  return mMembers->value.rbegin();
332  }
333  else
334  {
335  exception::NonValidatedMemory lExc;
336  log ( lExc , "Access attempted on non-validated memory" );
337  throw lExc;
338  }
339  }
340 
341 
342  template< typename T >
344  {
345  if ( mMembers->valid )
346  {
347  return mMembers->value.rend();
348  }
349  else
350  {
351  exception::NonValidatedMemory lExc;
352  log ( lExc , "Access attempted on non-validated memory" );
353  throw lExc;
354  }
355  }
356 
357 
358  template< typename T >
359  std::vector<T> ValVector< T >::value() const
360  {
361  if ( mMembers->valid )
362  {
363  return mMembers->value;
364  }
365  else
366  {
367  exception::NonValidatedMemory lExc;
368  log ( lExc , "Access attempted on non-validated memory" );
369  throw lExc;
370  }
371  }
372 
373  template< typename T >
374  void ValVector< T >::value ( const std::vector<T>& aValue )
375  {
376  if ( !mMembers->valid )
377  {
378  mMembers->value = aValue;
379  }
380  else
381  {
382  exception::ValMemImutabilityViolation lExc;
383  log ( lExc , "Attempted to modify validated memory" );
384  throw lExc;
385  }
386  }
387 
388 
389  template class ValWord< uint8_t >;
390  template class ValWord< uint32_t >;
391 
392  template class ValVector< uint8_t >;
393  template class ValVector< uint32_t >;
394 
395 
396 }
uhal::ValVector
A class which wraps a block of data and marks whether or not it is valid.
Definition: ValMem.hpp:75
uhal::ValHeader::mMembers
boost::shared_ptr< _ValHeader_ > mMembers
A shared pointer to a ValWord struct, so that every copy of this ValWord points to the same underlyin...
Definition: ValMem.hpp:183
uhal::ValVector::valid
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:217
uhal::ValWord::value
T value() const
Return the value of the validated memory with check on validity.
Definition: ValMem.cpp:141
uhal::_ValVector_::_ValVector_
_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
uhal
Definition: HttpResponseGrammar.hpp:49
uhal::_ValWord_::_ValWord_
_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
uhal::ValWord::mask
const uint32_t & mask() const
Return the mask used by this validated memory.
Definition: ValMem.cpp:173
uhal::log
void log(FatalLevel &aFatal, const T0 &aArg0)
Function to add a log entry at Fatal level.
Definition: log.hxx:20
uhal::ValVector::const_reverse_iterator
std::vector< T >::const_reverse_iterator const_reverse_iterator
typedef iterator to be that of the underlying storage type
Definition: ValMem.hpp:289
uhal::_ValHeader_::_ValHeader_
_ValHeader_(const bool &aValid)
Constructor Private, since this struct should only be used by the ValHeader.
Definition: ValMem.cpp:43
uhal::ValVector::const_iterator
std::vector< T >::const_iterator const_iterator
typedef iterator to be that of the underlying storage type
Definition: ValMem.hpp:285
uhal::ValVector::value
std::vector< T > value() const
Return the value of the validated memory with check on validity.
Definition: ValMem.cpp:359
uhal::ValWord::operator=
ValWord & operator=(const T &aValue)
Assignment operator - no check on whether the data has previously been marked as valid.
Definition: ValMem.cpp:126
uhal::ValHeader::ValHeader
ValHeader()
Default constructor.
Definition: ValMem.cpp:71
uhal::ValVector::ValVector
ValVector()
Default constructor.
Definition: ValMem.cpp:210
uhal::_ValHeader_
A helper struct wrapping an IPbus header and a valid flag.
Definition: ValMem.hpp:80
log.hpp
uhal::ValWord
A class which wraps a single word of data and marks whether or not it is valid.
Definition: ValMem.hpp:73
ValMem.hpp
uhal::ValWord::valid
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:112
bits.hpp
uhal::utilities::TrailingRightBits
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
uhal::_ValVector_
A Template helper struct wrapping a block of IPbus header, a register for storing a block of data and...
Definition: ValMem.hpp:127
uhal::ValHeader::valid
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:77
uhal::_ValWord_
A Template helper struct wrapping an IPbus header, a register for storing a single word of data,...
Definition: ValMem.hpp:103
uhal::ValWord::ValWord
ValWord()
Default constructor.
Definition: ValMem.cpp:105