# mem_func.hpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef MEM_FUNC_HPP
    00013 #define MEM_FUNC_HPP
    00014
    00015 #include <string.h>
    00016 #include "math_func.hpp"
    00017
    00025 template <typename T>
    00026 static FORCEINLINE void MemCpyT(T *destination, const T *source, size_t num = 1)
    00027 {
    00028   memcpy(destination, source, num * sizeof(T));
    00029 }
    00030
    00038 template <typename T>
    00039 static FORCEINLINE void MemMoveT(T *destination, const T *source, size_t num = 1)
    00040 {
    00041   memmove(destination, source, num * sizeof(T));
    00042 }
    00043
    00051 template <typename T>
    00052 static FORCEINLINE void MemSetT(T *ptr, byte value, size_t num = 1)
    00053 {
    00054   memset(ptr, value, num * sizeof(T));
    00055 }
    00056
    00065 template <typename T>
    00066 static FORCEINLINE int MemCmpT(const T *ptr1, const T *ptr2, size_t num = 1)
    00067 {
    00068   return memcmp(ptr1, ptr2, num * sizeof(T));
    00069 }
    00070
    00079 template <typename T>
    00080 static FORCEINLINE void MemReverseT(T *ptr1, T *ptr2)
    00081 {
    00082   assert(ptr1 != NULL && ptr2 != NULL);
    00083   assert(ptr1 < ptr2);
    00084
    00085   do {
    00086     Swap(*ptr1, *ptr2);
    00087   } while (++ptr1 < --ptr2);
    00088 }
    00089
    00096 template <typename T>
    00097 static FORCEINLINE void MemReverseT(T *ptr, size_t num)
    00098 {
    00099   assert(ptr != NULL);
    00100
    00101   MemReverseT(ptr, ptr + (num - 1));
    00102 }
    00103
    00104 #endif /* MEM_FUNC_HPP */