forked from HeliumProject/Platform
/
Utility.h
163 lines (132 loc) · 6.42 KB
/
Utility.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#pragma once
#include "Platform/API.h"
#include "Platform/Types.h"
#include "Platform/Assert.h"
#include "Platform/System.h"
#if HELIUM_CC_CL
# pragma warning( push )
# pragma warning( disable : 4530 ) // C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
# if _MSC_VER < 1600 // VC2008
# pragma warning( disable : 4985 ) // 'symbol name': attributes not present on previous declaration : see http://connect.microsoft.com/VisualStudio/feedback/details/381422/warning-of-attributes-not-present-on-previous-declaration-on-ceil-using-both-math-h-and-intrin-h
# endif
#endif
#if HELIUM_CC_CL
#pragma warning( pop )
#endif
#include <type_traits>
#include <cstdarg>
#include <string.h>
/// @defgroup utilitymacros General Utility Macros
//@{
/// Specify a variable as purposefully unreferenced.
///
/// @param[in] VAR Variable that may be unreferenced across its scoped.
#define HELIUM_UNREF( VAR ) ( void )( VAR )
/// Get the number of elements in a static array.
///
/// @param[in] ARRAY Static array.
///
/// @return Number of elements in the array.
#define HELIUM_ARRAY_COUNT( ARRAY ) ( sizeof( ARRAY ) / sizeof( ARRAY[ 0 ] ) )
/// Get the byte offset of a member of a struct or class.
///
/// @param[in] TYPE Struct or class type name.
/// @param[in] MEMBER Member name.
///
/// @return Byte offset of the specified member.
#define HELIUM_OFFSET_OF( TYPE, MEMBER ) ( reinterpret_cast< size_t >( &static_cast< TYPE* >( NULL )->MEMBER ) )
/// Get the bitfield with only the most significant bit set.
///
/// @param[in] TYPE Any type (should be numeric).
///
/// @return Constant number of the highest bit set, all other bits zero.
#define HELIUM_HIGH_BIT( TYPE ) ( 1 << ( sizeof( TYPE ) * 8 ) )
//@}
namespace Helium
{
/// @defgroup utilitymem Memory Utility Functions
//@{
inline void MemoryCopy( void* pDest, const void* pSource, size_t size );
inline void MemoryMove( void* pDest, const void* pSource, size_t size );
inline void MemorySet( void* pDest, int value, size_t size );
inline void MemoryZero( void* pDest, size_t size );
inline int MemoryCompare( const void* pMemory0, const void* pMemory1, size_t size );
template< typename T > void InPlaceConstruct( void* pMemory );
template< typename T > void InPlaceDestruct( void* pMemory );
template< typename T > void ArrayCopy( T* pDest, const T* pSource, size_t count );
template< typename T > void ArrayMove( T* pDest, const T* pSource, size_t count );
template< typename T > void ArraySet( T* pDest, const T& rValue, size_t count );
template< typename T > T* ArrayInPlaceConstruct( void* pMemory, size_t count );
template< typename T > void ArrayInPlaceDestruct( T* pMemory, size_t count );
template< typename T > void ArrayUninitializedCopy( T* pDest, const T* pSource, size_t count );
template< typename T > void ArrayUninitializedFill( T* pDest, const T& rValue, size_t count );
template< typename T > T Align( const T& rValue, size_t alignment );
template< typename T > void Swap( T& rValue0, T& rValue1 );
//@}
/// @defgroup utilitystring String Utility Functions
//@{
template< typename T > uint32_t StringHash( const T* pString );
template< typename T > size_t StringLength( const T* pString );
template< class T, size_t N >
inline void CopyString( T (&dest)[N], const T* src, size_t count = 0 );
template< class T >
inline void CopyString( T* dest, size_t destCount, const T* src, size_t count = 0 );
template< class T, size_t N >
inline void AppendString( T (&dest)[N], const T* src, size_t count = 0 );
template< class T >
inline void AppendString( T* dest, size_t destCount, const T* src, size_t count = 0 );
template < class T >
inline int CompareString( const T* a, const T* b, size_t count = 0 );
template < class T >
inline int CaseInsensitiveCompareString( const T* a, const T* b, size_t count = 0 );
template< class T >
inline const T* FindCharacter( const T* data, const T value, size_t count = 0 );
template< class T >
inline const T* FindNextToken( const T* data, const T delim, size_t count = 0 );
//@}
/// @defgroup utilityindex Index Utility Functions
//@{
template< typename IndexType > IndexType Invalid();
template< typename IndexType > bool IsValid( IndexType index );
template< typename IndexType > bool IsInvalid( IndexType index );
template< typename IndexType > void SetInvalid( IndexType& rIndex );
template< typename DestIndexType, typename SourceIndexType > DestIndexType CastIndex( SourceIndexType index );
//@}
/// @defgroup utilitybit Bit Manipulation Utility Functions
//@{
template< typename ElementType > void GetBitElementAndMaskIndex( size_t bitIndex, size_t& rElementIndex, size_t& rMaskIndex );
template< typename ElementType > bool GetBit( const ElementType& rElement, size_t maskIndex );
template< typename ElementType > void SetBit( ElementType& rElement, size_t maskIndex );
template< typename ElementType > void ClearBit( ElementType& rElement, size_t maskIndex );
template< typename ElementType > void ToggleBit( ElementType& rElement, size_t maskIndex );
template< typename ElementType > void SetBitRange( ElementType* pElements, size_t bitStart, size_t bitCount );
template< typename ElementType > void ClearBitRange( ElementType* pElements, size_t bitStart, size_t bitCount );
template< typename ElementType > void ToggleBitRange( ElementType* pElements, size_t bitStart, size_t bitCount );
//@}
/// @defgroup utilitydatareading Data Reading & Byte-swapping Utility Functions
/// Note that all of these functions can perform in-place byte swapping (source and destination arguments are the same).
//@{
template< typename T > const void* LoadValue( T& rDest, const void* pSource );
template< typename T > const void* LoadValueSwapped( T& rDest, const void* pSource );
inline void ReverseByteOrder( void* pDest, const void* pSource, size_t size );
//@}
/// Non-copyable base class.
class HELIUM_PLATFORM_API NonCopyable
{
public:
/// @name Construction/Destruction
//@{
inline NonCopyable();
//@}
private:
/// @name Construction/Destruction, Private
//@{
NonCopyable( const NonCopyable& ); // Not implemented.
//@}
/// @name Overloaded Operators
//@{
NonCopyable& operator=( const NonCopyable& ); // Not implemented.
//@}
};
}
#include "Platform/Utility.inl"