src/core/mem_func.hpp
author Tero Marttila <terom@fixme.fi>
Fri, 18 Jul 2008 22:41:08 +0300
changeset 11177 6d9a43c48924
parent 11049 f8bbc9635251
permissions -rw-r--r--
set the GRFConfig's next ptr to NULL
10962
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
     1
/* $Id$ */
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
     2
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
     3
/** @file mem_func.hpp Functions related to memory operations. */
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
     4
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
     5
#ifndef MEM_FUNC_HPP
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
     6
#define MEM_FUNC_HPP
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
     7
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
     8
#include <string.h>
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
     9
#include "math_func.hpp"
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    10
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    11
/**
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    12
 * Type-safe version of memcpy().
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    13
 *
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    14
 * @param destination Pointer to the destination buffer
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    15
 * @param source Pointer to the source buffer
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    16
 * @param num number of items to be copied. (!not number of bytes!)
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    17
 */
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    18
template <typename T>
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10963
diff changeset
    19
static FORCEINLINE void MemCpyT(T *destination, const T *source, uint num = 1)
10962
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    20
{
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    21
	memcpy(destination, source, num * sizeof(T));
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    22
}
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    23
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    24
/**
10963
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    25
 * Type-safe version of memmove().
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    26
 *
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    27
 * @param destination Pointer to the destination buffer
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    28
 * @param source Pointer to the source buffer
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    29
 * @param num number of items to be copied. (!not number of bytes!)
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    30
 */
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    31
template <typename T>
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10963
diff changeset
    32
static FORCEINLINE void MemMoveT(T *destination, const T *source, uint num = 1)
10963
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    33
{
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    34
	memmove(destination, source, num * sizeof(T));
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    35
}
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    36
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    37
/**
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    38
 * Type-safe version of memset().
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    39
 *
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    40
 * @param ptr Pointer to the destination buffer
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    41
 * @param value Value to be set
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    42
 * @param num number of items to be set (!not number of bytes!)
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    43
 */
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    44
template <typename T>
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10963
diff changeset
    45
static FORCEINLINE void MemSetT(T *ptr, int value, uint num = 1)
10963
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    46
{
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    47
	memset(ptr, value, num * sizeof(T));
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    48
}
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    49
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    50
/**
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    51
 * Type-safe version of memcmp().
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    52
 *
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    53
 * @param ptr1 Pointer to the first buffer
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    54
 * @param ptr2 Pointer to the second buffer
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    55
 * @param num Number of items to compare. (!not number of bytes!)
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    56
 * @return an int value indicating the relationship between the content of the two buffers
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    57
 */
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    58
template <typename T>
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10963
diff changeset
    59
static FORCEINLINE int MemCmpT(const T *ptr1, const T *ptr2, uint num = 1)
10963
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    60
{
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    61
	return memcmp(ptr1, ptr2, num * sizeof(T));
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    62
}
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    63
e4ff3aea4acf (svn r13517) -Add: remaining used typesafe versions of the mem* functions from <string.h>
skidd13
parents: 10962
diff changeset
    64
/**
10962
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    65
 * Type safe memory reverse operation.
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    66
 *  Reverse a block of memory in steps given by the
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    67
 *  type of the pointers.
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    68
 *
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    69
 * @param ptr1 Start-pointer to the block of memory.
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    70
 * @param ptr2 End-pointer to the block of memory.
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    71
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10963
diff changeset
    72
template <typename T>
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10963
diff changeset
    73
static FORCEINLINE void MemReverseT(T *ptr1, T *ptr2)
10962
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    74
{
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    75
	assert(ptr1 != NULL && ptr2 != NULL);
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    76
	assert(ptr1 < ptr2);
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    77
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    78
	do {
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    79
		Swap(*ptr1, *ptr2);
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    80
	} while (++ptr1 < --ptr2);
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    81
}
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    82
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    83
/**
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    84
 * Type safe memory reverse operation (overloaded)
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    85
 *
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    86
 * @param ptr Pointer to the block of memory.
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    87
 * @param num The number of items we want to reverse.
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    88
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10963
diff changeset
    89
template <typename T>
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10963
diff changeset
    90
static FORCEINLINE void MemReverseT(T *ptr, uint num)
10962
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    91
{
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    92
	assert(ptr != NULL);
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    93
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    94
	MemReverseT(ptr, ptr + (num - 1));
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    95
}
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    96
2649d7a316af (svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13
parents:
diff changeset
    97
#endif /* MEM_FUNC_HPP */