(svn r27950) -Merge: Documentation updates from 1.7 branch
[openttd.git] / src / core / mem_func.hpp
blob6878711645e089211dc9b3e8c0bdf01382764c3b
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file mem_func.hpp Functions related to memory operations. */
12 #ifndef MEM_FUNC_HPP
13 #define MEM_FUNC_HPP
15 #include "math_func.hpp"
17 /**
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!)
24 template <typename T>
25 static inline void MemCpyT(T *destination, const T *source, size_t num = 1)
27 memcpy(destination, source, num * sizeof(T));
30 /**
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!)
37 template <typename T>
38 static inline void MemMoveT(T *destination, const T *source, size_t num = 1)
40 memmove(destination, source, num * sizeof(T));
43 /**
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!)
50 template <typename T>
51 static inline void MemSetT(T *ptr, byte value, size_t num = 1)
53 memset(ptr, value, num * sizeof(T));
56 /**
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
64 template <typename T>
65 static inline int MemCmpT(const T *ptr1, const T *ptr2, size_t num = 1)
67 return memcmp(ptr1, ptr2, num * sizeof(T));
70 /**
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.
78 template <typename T>
79 static inline void MemReverseT(T *ptr1, T *ptr2)
81 assert(ptr1 != NULL && ptr2 != NULL);
82 assert(ptr1 < ptr2);
84 do {
85 Swap(*ptr1, *ptr2);
86 } while (++ptr1 < --ptr2);
89 /**
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.
95 template <typename T>
96 static inline void MemReverseT(T *ptr, size_t num)
98 assert(ptr != NULL);
100 MemReverseT(ptr, ptr + (num - 1));
103 #endif /* MEM_FUNC_HPP */