Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
26277c8a
Commit
26277c8a
authored
Apr 25, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement move assignment operator for Array.
parent
5adc272e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
16 deletions
+35
-16
format-test.cc
format-test.cc
+4
-2
format.cc
format.cc
+0
-1
format.h
format.h
+31
-13
No files found.
format-test.cc
View file @
26277c8a
...
...
@@ -226,12 +226,13 @@ TEST(ArrayTest, MoveCtor) {
// dynamic allocation.
array
.
push_back
(
'a'
);
CheckMoveArray
(
"testa"
,
array
);
const
char
*
inline_buffer_ptr
=
&
array
[
0
];
// Adding one more character causes the content to move from the inline to
// a dynamically allocated buffer.
array
.
push_back
(
'b'
);
Array
<
char
,
5
>
array2
(
std
::
move
(
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_GT
(
array2
.
capacity
(),
5
);
}
...
...
@@ -335,12 +336,13 @@ TEST(WriterTest, MoveCtor) {
w
.
Clear
();
w
<<
s
;
CheckMoveWriter
(
s
,
w
);
const
char
*
inline_buffer_ptr
=
w
.
data
();
// Adding one more character causes the content to move from the inline to
// a dynamically allocated buffer.
w
<<
'*'
;
Writer
w2
(
std
::
move
(
w
));
// 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
());
}
...
...
format.cc
View file @
26277c8a
...
...
@@ -33,7 +33,6 @@
#include "format.h"
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
...
...
format.h
View file @
26277c8a
...
...
@@ -30,6 +30,7 @@
#include <stdint.h>
#include <cassert>
#include <cstddef> // for std::ptrdiff_t
#include <cstdio>
#include <algorithm>
...
...
@@ -143,30 +144,47 @@ class Array {
void
Grow
(
std
::
size_t
size
);
// Do not implement!
Array
(
const
Array
&
);
void
operator
=
(
const
Array
&
);
public:
Array
()
:
size_
(
0
),
capacity_
(
SIZE
),
ptr_
(
data_
)
{}
~
Array
()
{
// Free memory allocated by the array.
void
Free
()
{
if
(
ptr_
!=
data_
)
delete
[]
ptr_
;
}
#if FMT_USE_RVALUE_REFERENCES
Array
(
Array
&&
other
)
:
size_
(
other
.
size_
),
capacity_
(
other
.
capacity_
)
{
// 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_
;
other
.
ptr_
=
0
;
// Set pointer to the inline array so that delete is not called
// when freeing.
other
.
ptr_
=
other
.
data_
;
}
}
// TODO: move assignment operator
// Do not implement!
Array
(
const
Array
&
);
void
operator
=
(
const
Array
&
);
public:
Array
()
:
size_
(
0
),
capacity_
(
SIZE
),
ptr_
(
data_
)
{}
~
Array
()
{
Free
();
}
#if FMT_USE_RVALUE_REFERENCES
Array
(
Array
&&
other
)
{
Move
(
other
);
}
Array
&
operator
=
(
Array
&&
other
)
{
assert
(
this
!=
&
other
);
Free
();
Move
(
other
);
return
*
this
;
}
#endif
// Returns the size of this array.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment