Add move semantics to idList and idStr

* idListArrayResize uses std::move
* idStr implements move constructor
* and move operator
* mpMap_t also implements a move operator
This commit is contained in:
Admer 2022-09-26 20:54:27 +02:00
parent 868fd1dce3
commit 93b8564b6e
3 changed files with 71 additions and 1 deletions

View file

@ -167,6 +167,7 @@ struct MemInfo_t
struct mpMap_t
{
mpMap_t& operator=( mpMap_t&& src ) = default;
void operator=( const mpMap_t& src )
{

View file

@ -130,6 +130,7 @@ class idStr
public:
idStr();
idStr( idStr&& text ) noexcept; // Admer: added move constructor
idStr( const idStr& text );
idStr( const idStr& text, int start, int end );
idStr( const char* text );
@ -149,6 +150,7 @@ public:
char operator[]( int index ) const;
char& operator[]( int index );
void operator=( idStr&& text ) noexcept; // Admer: added move operator
void operator=( const idStr& text );
void operator=( const char* text );
@ -477,6 +479,12 @@ ID_INLINE idStr::idStr()
Construct();
}
ID_INLINE idStr::idStr( idStr&& text ) noexcept
{
Construct();
*this = std::move( text );
}
ID_INLINE idStr::idStr( const idStr& text )
{
Construct();
@ -677,6 +685,28 @@ ID_INLINE char& idStr::operator[]( int index )
return data[ index ];
}
ID_INLINE void idStr::operator=( idStr&& text ) noexcept
{
Clear();
len = text.len;
allocedAndFlag = text.allocedAndFlag;
memcpy( baseBuffer, text.baseBuffer, sizeof( baseBuffer ) );
if ( text.data == text.baseBuffer )
{
data = baseBuffer;
}
else
{
data = text.data;
}
text.len = 0;
text.allocedAndFlag = 0;
text.data = nullptr;
}
ID_INLINE void idStr::operator=( const idStr& text )
{
int l;

View file

@ -98,7 +98,8 @@ ID_INLINE void* idListArrayResize( void* voldptr, int oldNum, int newNum, bool z
int overlap = Min( oldNum, newNum );
for( int i = 0; i < overlap; i++ )
{
newptr[i] = oldptr[i];
//newptr[i] = oldptr[i];
newptr[i] = std::move( oldptr[i] );
}
}
idListArrayDelete<_type_>( voldptr, oldNum );
@ -125,6 +126,7 @@ public:
typedef _type_ new_t();
idList( int newgranularity = 16 );
idList( idList&& other );
idList( const idList& other );
idList( std::initializer_list<_type_> initializerList );
~idList();
@ -139,6 +141,7 @@ public:
size_t Size() const; // returns total size of allocated memory including size of list _type_
size_t MemoryUsed() const; // returns size of the used elements in the list
idList<_type_, _tag_>& operator=( idList<_type_, _tag_>&& other );
idList<_type_, _tag_>& operator=( const idList<_type_, _tag_>& other );
const _type_& operator[]( int index ) const;
_type_& operator[]( int index );
@ -303,6 +306,18 @@ ID_INLINE idList<_type_, _tag_>::idList( int newgranularity )
Clear();
}
/*
================
idList<_type_,_tag_>::idList( idList< _type_, _tag_ >&& other )
================
*/
template< typename _type_, memTag_t _tag_ >
ID_INLINE idList<_type_, _tag_>::idList( idList&& other )
{
list = NULL;
*this = std::move( other );
}
/*
================
idList<_type_,_tag_>::idList( const idList< _type_, _tag_ > &other )
@ -700,6 +715,30 @@ ID_INLINE void idList<_type_, _tag_>::AssureSizeAlloc( int newSize, new_t* alloc
================
idList<_type_,_tag_>::operator=
Moves the contents and size attributes of another list, effectively emptying the other list.
================
*/
template< typename _type_, memTag_t _tag_ >
ID_INLINE idList<_type_, _tag_>& idList<_type_, _tag_>::operator=( idList<_type_, _tag_>&& other )
{
Clear();
num = other.num;
size = other.size;
granularity = other.granularity;
memTag = other.memTag;
list = other.list;
other.list = nullptr;
other.Clear();
return *this;
}
/*
================
idList<_type_,_tag_>::operator=
Copies the contents and size attributes of another list.
================
*/