1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
8 /** @file mem_func.hpp Functions related to memory operations. */
9 
10 #ifndef MEM_FUNC_HPP
11 #define MEM_FUNC_HPP
12 
13 #include "math_func.hpp"
14 
15 /**
16  * Type-safe version of memcpy().
17  *
18  * @param destination Pointer to the destination buffer
19  * @param source Pointer to the source buffer
20  * @param num number of items to be copied. (!not number of bytes!)
21  */
22 template <typename T>
MemCpyT(T * destination,const T * source,size_t num=1)23 static inline void MemCpyT(T *destination, const T *source, size_t num = 1)
24 {
25 	memcpy(destination, source, num * sizeof(T));
26 }
27 
28 /**
29  * Type-safe version of memmove().
30  *
31  * @param destination Pointer to the destination buffer
32  * @param source Pointer to the source buffer
33  * @param num number of items to be copied. (!not number of bytes!)
34  */
35 template <typename T>
MemMoveT(T * destination,const T * source,size_t num=1)36 static inline void MemMoveT(T *destination, const T *source, size_t num = 1)
37 {
38 	memmove(destination, source, num * sizeof(T));
39 }
40 
41 /**
42  * Type-safe version of memset().
43  *
44  * @param ptr Pointer to the destination buffer
45  * @param value Value to be set
46  * @param num number of items to be set (!not number of bytes!)
47  */
48 template <typename T>
MemSetT(T * ptr,byte value,size_t num=1)49 static inline void MemSetT(T *ptr, byte value, size_t num = 1)
50 {
51 	memset(ptr, value, num * sizeof(T));
52 }
53 
54 /**
55  * Type-safe version of memcmp().
56  *
57  * @param ptr1 Pointer to the first buffer
58  * @param ptr2 Pointer to the second buffer
59  * @param num Number of items to compare. (!not number of bytes!)
60  * @return an int value indicating the relationship between the content of the two buffers
61  */
62 template <typename T>
MemCmpT(const T * ptr1,const T * ptr2,size_t num=1)63 static inline int MemCmpT(const T *ptr1, const T *ptr2, size_t num = 1)
64 {
65 	return memcmp(ptr1, ptr2, num * sizeof(T));
66 }
67 
68 /**
69  * Type safe memory reverse operation.
70  *  Reverse a block of memory in steps given by the
71  *  type of the pointers.
72  *
73  * @param ptr1 Start-pointer to the block of memory.
74  * @param ptr2 End-pointer to the block of memory.
75  */
76 template <typename T>
MemReverseT(T * ptr1,T * ptr2)77 static inline void MemReverseT(T *ptr1, T *ptr2)
78 {
79 	assert(ptr1 != nullptr && ptr2 != nullptr);
80 	assert(ptr1 < ptr2);
81 
82 	do {
83 		Swap(*ptr1, *ptr2);
84 	} while (++ptr1 < --ptr2);
85 }
86 
87 /**
88  * Type safe memory reverse operation (overloaded)
89  *
90  * @param ptr Pointer to the block of memory.
91  * @param num The number of items we want to reverse.
92  */
93 template <typename T>
MemReverseT(T * ptr,size_t num)94 static inline void MemReverseT(T *ptr, size_t num)
95 {
96 	assert(ptr != nullptr);
97 
98 	MemReverseT(ptr, ptr + (num - 1));
99 }
100 
101 #endif /* MEM_FUNC_HPP */
102