KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
l_cpp_bits.h
Go to the documentation of this file.
1 /* $Id: l_cpp_bits.h 18278 2014-11-25 01:42:10Z ksimek $ */
2 /* {{{=========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== }}}*/
19 
20 // vim: tabstop=4 shiftwidth=4 foldmethod=marker
21 
22 #ifndef KJB_CPP_L_BITS
23 #define KJB_CPP_L_BITS
24 
25 #include <l/l_bits.h>
26 #include <l_cpp/l_exception.h>
27 
28 namespace kjb
29 {
30 
38 template <int NUM_BYTES>
39 inline void swap_bytes_dispatch(void* /* value */)
40 {
42 }
43 
45 template<>
46 inline void swap_bytes_dispatch<2>(void* value)
47 {
48  kjb_c::bswap_u16(static_cast<uint16_t*>(value));
49 }
50 
52 template<>
53 inline void swap_bytes_dispatch<4>(void* value)
54 {
55  kjb_c::bswap_u32(static_cast<uint32_t*>(value));
56 }
57 
59 template<>
60 inline void swap_bytes_dispatch<8>(void* value)
61 {
62  kjb_c::bswap_u64(static_cast<uint64_t*>(value));
63 }
64 
82 template <class T>
83 inline void swap_bytes(T* value)
84 {
85  swap_bytes_dispatch<sizeof(T)>((void*) value);
86 }
87 
88 template <class T>
89 inline void swap_bytes(T& value)
90 {
91  swap_bytes_dispatch<sizeof(T)>((void*) &value);
92 }
93 
94 
102 template <class T>
103 void swap_array_bytes(T* array, size_t N)
104 {
105  for(size_t i = 0; i < N; ++i)
106  {
107  swap_bytes(array++);
108  }
109 }
110 
111 } // namespace kjb
112 
113 #endif
void swap_bytes_dispatch< 4 >(void *value)
swap the byte order of a 32-bit value (do not call directly).
Definition: l_cpp_bits.h:53
void swap_bytes(T *value)
Definition: l_cpp_bits.h:83
#define KJB_THROW(ex)
Definition: l_exception.h:46
void swap_bytes_dispatch< 2 >(void *value)
swap the byte order of a 16-bit value (do not call directly).
Definition: l_cpp_bits.h:46
void swap_array_bytes(T *array, size_t N)
Definition: l_cpp_bits.h:103
void swap_bytes_dispatch< 8 >(void *value)
swap the byte order of a 64-bit value (do not call directly).
Definition: l_cpp_bits.h:60
void swap_bytes_dispatch(void *)
Swap the byte-order of a value.
Definition: l_cpp_bits.h:39
Object thrown when attempting to use unimplemented functionality.
Definition: l_exception.h:281
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
Support for error handling exception classes in libKJB.