Improved Construct<T> function

P4:6127553
This commit is contained in:
Fletcher Dunn
2020-10-06 15:58:18 -07:00
parent 430b81aac0
commit ca57199db2
3 changed files with 11 additions and 48 deletions
+3 -40
View File
@@ -99,48 +99,11 @@ inline T* ValueInitializeConstruct( T* pMemory )
return ::new( pMemory ) T{};
}
template <class T>
inline void CopyConstruct( T* pMemory, T const& src )
template <class T, typename... ConstructorArgs>
inline T* Construct( T* pMemory, ConstructorArgs&&... args )
{
HINT( pMemory != 0 );
::new( pMemory ) T(src);
}
#ifdef VALVE_RVALUE_REFS
template <class T>
inline void CopyConstruct( T* pMemory, T&& src )
{
HINT( pMemory != 0 );
::new(pMemory)T( std::forward<T>(src) );
}
#endif
template <class T, class P>
inline void ConstructOneArg( T* pMemory, P const& arg)
{
HINT( pMemory != 0 );
::new( pMemory ) T(arg);
}
template <class T, class P, class P2 >
inline void ConstructTwoArg( T* pMemory, P const& arg1, P2 const &arg2 )
{
HINT( pMemory != 0 );
::new( pMemory ) T(arg1, arg2);
}
template <class T, class P, class P2 >
inline void ConstructTwoArgNoConst( T* pMemory, P& arg1, P2& arg2 )
{
HINT( pMemory != 0 );
::new( pMemory ) T( arg1, arg2 );
}
template <class T, class P, class P2, class P3, class P4, class P5, class P6, class P7 >
inline void ConstructSevenArg( T* pMemory, P const& arg1, P2 const &arg2, P3 const &arg3, P4 const &arg4, P5 const &arg5, P6 const &arg6, P7 const &arg7 )
{
HINT( pMemory != 0 );
::new( pMemory ) T(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
return ::new( pMemory ) T( std::forward<ConstructorArgs>(args)... );
}
template <class T>
+4 -4
View File
@@ -476,7 +476,7 @@ inline int CUtlHashMap<K,T,L,H>::InsertUnconstructed( KeyType_universal_ref &&ke
// make an item
int iNewNode = AllocNode();
m_memNodes[iNewNode].m_iNextNode = kInvalidIndex;
CopyConstruct( &m_memNodes[iNewNode].m_key, std::forward<KeyType_universal_ref>( key ) );
Construct( &m_memNodes[iNewNode].m_key, std::forward<KeyType_universal_ref>( key ) );
// Note: m_elem remains intentionally unconstructed here
// Note: key may have been moved depending on which constructor was called.
@@ -534,11 +534,11 @@ inline int CUtlHashMap<K,T,L,H>::FindOrInsert_Internal( pf_key &&key, pf_elem &&
if ( bReplace && iNodeExisting != kInvalidIndex )
{
Destruct( &m_memNodes[ iNodeExisting ].m_elem );
CopyConstruct( &m_memNodes[ iNodeExisting ].m_elem, std::forward<pf_elem>( elem ) );
Construct( &m_memNodes[ iNodeExisting ].m_elem, std::forward<pf_elem>( elem ) );
}
else if ( iNodeInserted != kInvalidIndex )
{
CopyConstruct( &m_memNodes[ iNodeInserted ].m_elem, std::forward<pf_elem>( elem ) );
Construct( &m_memNodes[ iNodeInserted ].m_elem, std::forward<pf_elem>( elem ) );
return iNodeInserted;
}
return iNodeExisting;
@@ -554,7 +554,7 @@ inline int CUtlHashMap<K,T,L,H>::InsertWithDupes_Internal( pf_key &&key, pf_elem
int iNodeInserted = InsertUnconstructed( std::forward<pf_key>( key ), NULL, true /*duplicates allowed!*/ ); // copies key
if ( iNodeInserted != kInvalidIndex )
{
CopyConstruct( &m_memNodes[ iNodeInserted ].m_elem, std::forward<pf_elem>( insert ) );
Construct( &m_memNodes[ iNodeInserted ].m_elem, std::forward<pf_elem>( insert ) );
}
return iNodeInserted;
}
+4 -4
View File
@@ -596,7 +596,7 @@ inline int CUtlVector<T, A>::SortedInsert( const T& src, bool (__cdecl *pfnLessF
int pos = SortedFindLessOrEqual( src, pfnLessFunc, pLessContext ) + 1;
GrowVector();
ShiftElementsRight(pos);
CopyConstruct<T>( &Element(pos), src );
Construct<T>( &Element(pos), src );
return pos;
}
@@ -746,7 +746,7 @@ inline int CUtlVector<T, A>::SortedInsert( const T& src, bool (__cdecl *pfnLessF
int pos = SortedFindLessOrEqual( src, pfnLessFunc ) + 1;
GrowVector();
ShiftElementsRight(pos);
CopyConstruct<T>( &Element(pos), src );
Construct<T>( &Element(pos), src );
return pos;
}
@@ -1046,7 +1046,7 @@ inline int CUtlVector<T, A>::InsertBefore( int elem, const T& src )
GrowVector();
ShiftElementsRight(elem);
CopyConstruct( &Element(elem), src );
Construct( &Element(elem), src );
return elem;
}
@@ -1059,7 +1059,7 @@ inline int CUtlVector<T, A>::AddToTail( T&& src )
Assert( (&src < Base()) || (&src >= (Base() + Count())) );
int elem = m_Size;
GrowVector();
CopyConstruct( &Element( elem ), std::forward<T>( src ) );
Construct( &Element( elem ), std::forward<T>( src ) );
return elem;
}
#endif