4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file mem_func.hpp Functions related to memory operations. */
15 #include "math_func.hpp"
18 * Type-safe version of memcpy().
20 * @param destination Pointer to the destination buffer
21 * @param source Pointer to the source buffer
22 * @param num number of items to be copied. (!not number of bytes!)
25 static inline void MemCpyT(T
*destination
, const T
*source
, size_t num
= 1)
27 memcpy(destination
, source
, num
* sizeof(T
));
31 * Type-safe version of memmove().
33 * @param destination Pointer to the destination buffer
34 * @param source Pointer to the source buffer
35 * @param num number of items to be copied. (!not number of bytes!)
38 static inline void MemMoveT(T
*destination
, const T
*source
, size_t num
= 1)
40 memmove(destination
, source
, num
* sizeof(T
));
44 * Type-safe version of memset().
46 * @param ptr Pointer to the destination buffer
47 * @param value Value to be set
48 * @param num number of items to be set (!not number of bytes!)
51 static inline void MemSetT(T
*ptr
, byte value
, size_t num
= 1)
53 memset(ptr
, value
, num
* sizeof(T
));
57 * Type-safe version of memcmp().
59 * @param ptr1 Pointer to the first buffer
60 * @param ptr2 Pointer to the second buffer
61 * @param num Number of items to compare. (!not number of bytes!)
62 * @return an int value indicating the relationship between the content of the two buffers
65 static inline int MemCmpT(const T
*ptr1
, const T
*ptr2
, size_t num
= 1)
67 return memcmp(ptr1
, ptr2
, num
* sizeof(T
));
71 * Type safe memory reverse operation.
72 * Reverse a block of memory in steps given by the
73 * type of the pointers.
75 * @param ptr1 Start-pointer to the block of memory.
76 * @param ptr2 End-pointer to the block of memory.
79 static inline void MemReverseT(T
*ptr1
, T
*ptr2
)
81 assert(ptr1
!= NULL
&& ptr2
!= NULL
);
86 } while (++ptr1
< --ptr2
);
90 * Type safe memory reverse operation (overloaded)
92 * @param ptr Pointer to the block of memory.
93 * @param num The number of items we want to reverse.
96 static inline void MemReverseT(T
*ptr
, size_t num
)
100 MemReverseT(ptr
, ptr
+ (num
- 1));
103 #endif /* MEM_FUNC_HPP */