src/core/mem_func.hpp
changeset 10962 2649d7a316af
child 10963 e4ff3aea4acf
equal deleted inserted replaced
10961:7c4f84421dc5 10962:2649d7a316af
       
     1 /* $Id$ */
       
     2 
       
     3 /** @file mem_func.hpp Functions related to memory operations. */
       
     4 
       
     5 #ifndef MEM_FUNC_HPP
       
     6 #define MEM_FUNC_HPP
       
     7 
       
     8 #include <string.h>
       
     9 #include "math_func.hpp"
       
    10 
       
    11 /**
       
    12  * Type-safe version of memcpy().
       
    13  *
       
    14  * @param destination Pointer to the destination buffer
       
    15  * @param source Pointer to the source buffer
       
    16  * @param num number of items to be copied. (!not number of bytes!)
       
    17  */
       
    18 template <typename T>
       
    19 FORCEINLINE void MemCpyT(T *destination, const T *source, uint num = 1)
       
    20 {
       
    21 	memcpy(destination, source, num * sizeof(T));
       
    22 }
       
    23 
       
    24 /**
       
    25  * Type safe memory reverse operation.
       
    26  *  Reverse a block of memory in steps given by the
       
    27  *  type of the pointers.
       
    28  *
       
    29  * @param ptr1 Start-pointer to the block of memory.
       
    30  * @param ptr2 End-pointer to the block of memory.
       
    31  */
       
    32 template<typename T>
       
    33 FORCEINLINE void MemReverseT(T *ptr1, T *ptr2)
       
    34 {
       
    35 	assert(ptr1 != NULL && ptr2 != NULL);
       
    36 	assert(ptr1 < ptr2);
       
    37 
       
    38 	do {
       
    39 		Swap(*ptr1, *ptr2);
       
    40 	} while (++ptr1 < --ptr2);
       
    41 }
       
    42 
       
    43 /**
       
    44  * Type safe memory reverse operation (overloaded)
       
    45  *
       
    46  * @param ptr Pointer to the block of memory.
       
    47  * @param num The number of items we want to reverse.
       
    48  */
       
    49 template<typename T>
       
    50 FORCEINLINE void MemReverseT(T *ptr, uint num)
       
    51 {
       
    52 	assert(ptr != NULL);
       
    53 
       
    54 	MemReverseT(ptr, ptr + (num - 1));
       
    55 }
       
    56 
       
    57 #endif /* MEM_FUNC_HPP */