mirror of
https://github.com/fmtlib/fmt.git
synced 2025-01-27 06:35:37 +00:00
Implement move assignment operator for Array.
This commit is contained in:
parent
5adc272ee7
commit
26277c8ab3
@ -226,12 +226,13 @@ TEST(ArrayTest, MoveCtor) {
|
|||||||
// dynamic allocation.
|
// dynamic allocation.
|
||||||
array.push_back('a');
|
array.push_back('a');
|
||||||
CheckMoveArray("testa", array);
|
CheckMoveArray("testa", array);
|
||||||
|
const char *inline_buffer_ptr = &array[0];
|
||||||
// Adding one more character causes the content to move from the inline to
|
// Adding one more character causes the content to move from the inline to
|
||||||
// a dynamically allocated buffer.
|
// a dynamically allocated buffer.
|
||||||
array.push_back('b');
|
array.push_back('b');
|
||||||
Array<char, 5> array2(std::move(array));
|
Array<char, 5> array2(std::move(array));
|
||||||
// Move should rip the guts of the first array.
|
// Move should rip the guts of the first array.
|
||||||
EXPECT_TRUE(!&array[0]);
|
EXPECT_EQ(inline_buffer_ptr, &array[0]);
|
||||||
EXPECT_EQ("testab", std::string(&array2[0], array2.size()));
|
EXPECT_EQ("testab", std::string(&array2[0], array2.size()));
|
||||||
EXPECT_GT(array2.capacity(), 5);
|
EXPECT_GT(array2.capacity(), 5);
|
||||||
}
|
}
|
||||||
@ -335,12 +336,13 @@ TEST(WriterTest, MoveCtor) {
|
|||||||
w.Clear();
|
w.Clear();
|
||||||
w << s;
|
w << s;
|
||||||
CheckMoveWriter(s, w);
|
CheckMoveWriter(s, w);
|
||||||
|
const char *inline_buffer_ptr = w.data();
|
||||||
// Adding one more character causes the content to move from the inline to
|
// Adding one more character causes the content to move from the inline to
|
||||||
// a dynamically allocated buffer.
|
// a dynamically allocated buffer.
|
||||||
w << '*';
|
w << '*';
|
||||||
Writer w2(std::move(w));
|
Writer w2(std::move(w));
|
||||||
// Move should rip the guts of the first writer.
|
// Move should rip the guts of the first writer.
|
||||||
EXPECT_TRUE(!w.data());
|
EXPECT_EQ(inline_buffer_ptr, w.data());
|
||||||
EXPECT_EQ(s + '*', w2.str());
|
EXPECT_EQ(s + '*', w2.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,6 @@
|
|||||||
|
|
||||||
#include "format.h"
|
#include "format.h"
|
||||||
|
|
||||||
#include <cassert>
|
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <climits>
|
#include <climits>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
46
format.h
46
format.h
@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <cstddef> // for std::ptrdiff_t
|
#include <cstddef> // for std::ptrdiff_t
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@ -143,30 +144,47 @@ class Array {
|
|||||||
|
|
||||||
void Grow(std::size_t size);
|
void Grow(std::size_t size);
|
||||||
|
|
||||||
|
// Free memory allocated by the array.
|
||||||
|
void Free() {
|
||||||
|
if (ptr_ != data_) delete [] ptr_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move data from other to this array.
|
||||||
|
void Move(Array &other) {
|
||||||
|
size_ = other.size_;
|
||||||
|
capacity_ = other.capacity_;
|
||||||
|
if (other.ptr_ == other.data_) {
|
||||||
|
ptr_ = data_;
|
||||||
|
std::copy(other.data_, other.data_ + size_, CheckPtr(data_, capacity_));
|
||||||
|
} else {
|
||||||
|
ptr_ = other.ptr_;
|
||||||
|
// Set pointer to the inline array so that delete is not called
|
||||||
|
// when freeing.
|
||||||
|
other.ptr_ = other.data_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Do not implement!
|
// Do not implement!
|
||||||
Array(const Array &);
|
Array(const Array &);
|
||||||
void operator=(const Array &);
|
void operator=(const Array &);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Array() : size_(0), capacity_(SIZE), ptr_(data_) {}
|
Array() : size_(0), capacity_(SIZE), ptr_(data_) {}
|
||||||
~Array() {
|
~Array() { Free(); }
|
||||||
if (ptr_ != data_) delete [] ptr_;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if FMT_USE_RVALUE_REFERENCES
|
#if FMT_USE_RVALUE_REFERENCES
|
||||||
Array(Array &&other)
|
|
||||||
: size_(other.size_),
|
Array(Array &&other) {
|
||||||
capacity_(other.capacity_) {
|
Move(other);
|
||||||
if (other.ptr_ == other.data_) {
|
}
|
||||||
ptr_ = data_;
|
|
||||||
std::copy(other.data_, other.data_ + size_, CheckPtr(data_, capacity_));
|
Array& operator=(Array&& other) {
|
||||||
} else {
|
assert(this != &other);
|
||||||
ptr_ = other.ptr_;
|
Free();
|
||||||
other.ptr_ = 0;
|
Move(other);
|
||||||
}
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move assignment operator
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Returns the size of this array.
|
// Returns the size of this array.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user