Utilities.hh
Go to the documentation of this file.
1 #ifndef dune_artdaq_Overlays_Utilities_hh
2 #define dune_artdaq_Overlays_Utilities_hh
3 
4 #include "cetlib_except/exception.h"
5 
6 #include <string>
7 
8 namespace dune {
9 
10 // JCF, Jul-21-2015
11 
12 // display_bits() will display the entire bit-by-bit contents of
13 // nybtes of memory beginning at memstart; the optional last argument,
14 // "sourcename", is the label you want associated with the diagnostic
15 // message (e.g., "PennReceiver")
16 
17 void display_bits(void* memstart, size_t nbytes, std::string sourcename = "dune_display_bits");
18 
19 
20 // JCF, Jul-16-2015
21 
22 // reinterpret_cast_checked will double check that the pointer we
23 // cast to actually points to the same location in memory as the
24 // pointer we cast from. This almost certainly is the case, but it's
25 // not an ironclad guarantee in the C++ standard, unfortunately.
26 
27 template <typename S, typename T>
29 
30  S outptr = reinterpret_cast<S>( inptr );
31 
32  const void* inptr_void = static_cast<const void*>( inptr );
33  const void* outptr_void = static_cast<const void*>( outptr );
34 
35  if ( inptr_void != outptr_void ) {
36  throw cet::exception("reinterpret_cast_checked") << "Error: reinterpret_cast casted " << inptr_void << " to " << outptr_void << " => results of the cast can't be trusted";
37  }
38 
39  return outptr;
40 }
41 
42 }
43 
44 #endif
std::string string
Definition: nybbler.cc:12
Definition: 044_section.h:5
void display_bits(void *memstart, size_t nbytes, std::string sourcename)
Definition: Utilities.cc:10
S reinterpret_cast_checked(T inptr)
Definition: Utilities.hh:28
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33