hard_cast.h
Go to the documentation of this file.
1 #ifndef cetlib_hard_cast_h
2 #define cetlib_hard_cast_h
3 
4 #include <cstring>
5 
6 namespace cet {
7  // For use when only a C++ {dynamic,static,reinterpret}_cast is not
8  // sufficient to the task. The only case of this known currently is
9  // when using dlopen, dlsym, etc. and a void * must be cast to a
10  // function pointer.
11  template <typename PTR_T>
12  PTR_T hard_cast(void* src);
13  template <typename PTR_T>
14  void hard_cast(void* src, PTR_T& dest);
15 }
16 
17 template <typename PTR_T>
18 inline PTR_T
19 cet::hard_cast(void* src)
20 {
21  PTR_T dest;
22  hard_cast(src, dest);
23  return dest;
24 }
25 
26 template <typename PTR_T>
27 inline void
28 cet::hard_cast(void* src, PTR_T& dest)
29 {
30  memcpy(&dest, &src, sizeof(PTR_T));
31 }
32 #endif /* cetlib_hard_cast_h */
33 
34 // Local Variables:
35 // mode: c++
36 // End:
PTR_T hard_cast(void *src)
Definition: hard_cast.h:19