OpenTTD Source  1.11.0-beta2
mem_func.hpp
Go to the documentation of this file.
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 
10 #ifndef MEM_FUNC_HPP
11 #define MEM_FUNC_HPP
12 
13 #include "math_func.hpp"
14 
22 template <typename T>
23 static inline void MemCpyT(T *destination, const T *source, size_t num = 1)
24 {
25  memcpy(destination, source, num * sizeof(T));
26 }
27 
35 template <typename T>
36 static inline void MemMoveT(T *destination, const T *source, size_t num = 1)
37 {
38  memmove(destination, source, num * sizeof(T));
39 }
40 
48 template <typename T>
49 static inline void MemSetT(T *ptr, byte value, size_t num = 1)
50 {
51  memset(ptr, value, num * sizeof(T));
52 }
53 
62 template <typename T>
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 
76 template <typename T>
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 
93 template <typename T>
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 */
math_func.hpp
MemReverseT
static void MemReverseT(T *ptr1, T *ptr2)
Type safe memory reverse operation.
Definition: mem_func.hpp:77
MemCpyT
static void MemCpyT(T *destination, const T *source, size_t num=1)
Type-safe version of memcpy().
Definition: mem_func.hpp:23
MemSetT
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49
Swap
static void Swap(T &a, T &b)
Type safe swap operation.
Definition: math_func.hpp:215
MemCmpT
static int MemCmpT(const T *ptr1, const T *ptr2, size_t num=1)
Type-safe version of memcmp().
Definition: mem_func.hpp:63
MemMoveT
static void MemMoveT(T *destination, const T *source, size_t num=1)
Type-safe version of memmove().
Definition: mem_func.hpp:36