|
1 /* $Id$ */ |
|
2 |
|
3 /** @file mem_func.hpp Functions related to memory operations. */ |
|
4 |
|
5 #ifndef MEM_FUNC_HPP |
|
6 #define MEM_FUNC_HPP |
|
7 |
|
8 #include <string.h> |
|
9 #include "math_func.hpp" |
|
10 |
|
11 /** |
|
12 * Type-safe version of memcpy(). |
|
13 * |
|
14 * @param destination Pointer to the destination buffer |
|
15 * @param source Pointer to the source buffer |
|
16 * @param num number of items to be copied. (!not number of bytes!) |
|
17 */ |
|
18 template <typename T> |
|
19 FORCEINLINE void MemCpyT(T *destination, const T *source, uint num = 1) |
|
20 { |
|
21 memcpy(destination, source, num * sizeof(T)); |
|
22 } |
|
23 |
|
24 /** |
|
25 * Type-safe version of memmove(). |
|
26 * |
|
27 * @param destination Pointer to the destination buffer |
|
28 * @param source Pointer to the source buffer |
|
29 * @param num number of items to be copied. (!not number of bytes!) |
|
30 */ |
|
31 template <typename T> |
|
32 FORCEINLINE void MemMoveT(T *destination, const T *source, uint num = 1) |
|
33 { |
|
34 memmove(destination, source, num * sizeof(T)); |
|
35 } |
|
36 |
|
37 /** |
|
38 * Type-safe version of memset(). |
|
39 * |
|
40 * @param ptr Pointer to the destination buffer |
|
41 * @param value Value to be set |
|
42 * @param num number of items to be set (!not number of bytes!) |
|
43 */ |
|
44 template <typename T> |
|
45 FORCEINLINE void MemSetT(T *ptr, int value, uint num = 1) |
|
46 { |
|
47 memset(ptr, value, num * sizeof(T)); |
|
48 } |
|
49 |
|
50 /** |
|
51 * Type-safe version of memcmp(). |
|
52 * |
|
53 * @param ptr1 Pointer to the first buffer |
|
54 * @param ptr2 Pointer to the second buffer |
|
55 * @param num Number of items to compare. (!not number of bytes!) |
|
56 * @return an int value indicating the relationship between the content of the two buffers |
|
57 */ |
|
58 template <typename T> |
|
59 FORCEINLINE int MemCmpT(const T *ptr1, const T *ptr2, uint num = 1) |
|
60 { |
|
61 return memcmp(ptr1, ptr2, num * sizeof(T)); |
|
62 } |
|
63 |
|
64 /** |
|
65 * Type safe memory reverse operation. |
|
66 * Reverse a block of memory in steps given by the |
|
67 * type of the pointers. |
|
68 * |
|
69 * @param ptr1 Start-pointer to the block of memory. |
|
70 * @param ptr2 End-pointer to the block of memory. |
|
71 */ |
|
72 template<typename T> |
|
73 FORCEINLINE void MemReverseT(T *ptr1, T *ptr2) |
|
74 { |
|
75 assert(ptr1 != NULL && ptr2 != NULL); |
|
76 assert(ptr1 < ptr2); |
|
77 |
|
78 do { |
|
79 Swap(*ptr1, *ptr2); |
|
80 } while (++ptr1 < --ptr2); |
|
81 } |
|
82 |
|
83 /** |
|
84 * Type safe memory reverse operation (overloaded) |
|
85 * |
|
86 * @param ptr Pointer to the block of memory. |
|
87 * @param num The number of items we want to reverse. |
|
88 */ |
|
89 template<typename T> |
|
90 FORCEINLINE void MemReverseT(T *ptr, uint num) |
|
91 { |
|
92 assert(ptr != NULL); |
|
93 |
|
94 MemReverseT(ptr, ptr + (num - 1)); |
|
95 } |
|
96 |
|
97 #endif /* MEM_FUNC_HPP */ |