OpenBarnyard
 
Loading...
Searching...
No Matches
Helpers.h
Go to the documentation of this file.
1#pragma once
2#include "Math/TMathInline.h"
3#include <limits>
4
5// Swaps values of two variables with a help of moving semantic
6template <typename T>
7TFORCEINLINE void TSwapValues( T& a, T& b )
8{
9 T temp = std::move( a );
10 a = std::move( b );
11 b = std::move( temp );
12}
13
14// Returns TUINT32 value that consists of the specified 4 characters
15TFORCEINLINE constexpr TUINT32 TFourCC( const TCHAR str[ 4 ] )
16{
17 return ( ( str[ 3 ] << 24 ) | ( str[ 2 ] << 16 ) | ( str[ 1 ] << 8 ) | str[ 0 ] );
18}
19
20// Returns TUINT32 value (always Low Endian) that consists of the specified 4 characters
22{
23 return PARSEDWORD( TFourCC( str ) );
24}
25
26// Returns TUINT32 value (always Big Endian) that consists of the specified 4 characters
28{
29 return PARSEDWORD_BIG( TFourCC( str ) );
30}
31
32// Returns TTRUE if the pointer is aligned to the specified alignment value
33// For example, TTRUE is returned if pointer is 0x8 and alignment is 4
34// TFALSE is returned if pointer is 0x8 and alignment is 7
35TFORCEINLINE TBOOL TIsPointerAligned( const void* a_pPointer, TSIZE a_uiAlignment = sizeof( void* ) )
36{
37 return 0 == ( TUINTPTR( a_pPointer ) % a_uiAlignment );
38}
39
40template <class T>
41TFORCEINLINE constexpr T* TAlignPointerUp( T* a_pMem, TSIZE a_uiAlignment = sizeof( T* ) )
42{
43 return TREINTERPRETCAST(
44 T*,
45 ( TUINTPTR( a_pMem ) + ( a_uiAlignment - 1 ) ) & ( ~( a_uiAlignment - 1U ) )
46 );
47}
48
49template <class T>
50TFORCEINLINE constexpr T* TAlignPointerDown( T* a_pMem, TSIZE a_uiAlignment = sizeof( T* ) )
51{
52 return TREINTERPRETCAST(
53 T*,
54 ( TUINTPTR( a_pMem ) ) & ( ~( a_uiAlignment - 1U ) )
55 );
56}
57
58template <class T>
59TFORCEINLINE constexpr T TAlignNumDown( T a_iValue, TSIZE a_uiAlignment = 4 )
60{
61 TSTATICASSERT( std::is_arithmetic<T>::value );
62 return a_iValue & (T)( ~( a_uiAlignment - 1U ) );
63}
64
65template <class T>
66TFORCEINLINE constexpr T TAlignNumUp( T a_iValue, TSIZE a_uiAlignment = 4 )
67{
68 TSTATICASSERT( std::is_arithmetic<T>::value );
69 return ( a_iValue + ( a_uiAlignment - 1U ) ) & (T)( ~( a_uiAlignment - 1U ) );
70}
#define TREINTERPRETCAST(TYPE, VALUE)
Definition Defines.h:68
#define TFORCEINLINE
Definition Defines.h:74
#define TSTATICASSERT(...)
Definition Defines.h:67
TFORCEINLINE constexpr T * TAlignPointerUp(T *a_pMem, TSIZE a_uiAlignment=sizeof(T *))
Definition Helpers.h:41
TFORCEINLINE TUINT32 TFourCCBE(const TCHAR str[4])
Definition Helpers.h:27
TFORCEINLINE void TSwapValues(T &a, T &b)
Definition Helpers.h:7
TFORCEINLINE TUINT32 TFourCCLE(const TCHAR str[4])
Definition Helpers.h:21
TFORCEINLINE constexpr T TAlignNumUp(T a_iValue, TSIZE a_uiAlignment=4)
Definition Helpers.h:66
TFORCEINLINE constexpr T * TAlignPointerDown(T *a_pMem, TSIZE a_uiAlignment=sizeof(T *))
Definition Helpers.h:50
TFORCEINLINE constexpr TUINT32 TFourCC(const TCHAR str[4])
Definition Helpers.h:15
TFORCEINLINE constexpr T TAlignNumDown(T a_iValue, TSIZE a_uiAlignment=4)
Definition Helpers.h:59
TFORCEINLINE TBOOL TIsPointerAligned(const void *a_pPointer, TSIZE a_uiAlignment=sizeof(void *))
Definition Helpers.h:35
uintptr_t TUINTPTR
Definition Typedefs.h:18
size_t TSIZE
Definition Typedefs.h:9
char TCHAR
Definition Typedefs.h:20
uint32_t TUINT32
Definition Typedefs.h:13
bool TBOOL
Definition Typedefs.h:6