Skip to content

Commit 6365d17

Browse files
Abseil TeamJon Cohen
Abseil Team
authored and
Jon Cohen
committed
Changes imported from Abseil "staging" branch:
- d1a8e5ddbf9f96a4ca020260b21e03820c5ff966 Remove references to the non-existing StringPrintf API to... by Abseil Team <[email protected]> - 64a422bc1620b29247a81fab2b08a7f23dfc1461 Add a copy and move constructor to FixedArray. This does... by Jon Cohen <[email protected]> GitOrigin-RevId: d1a8e5ddbf9f96a4ca020260b21e03820c5ff966 Change-Id: I4388bdf1260702f2847307abbac4bf265e8cf90f
1 parent eb5bbdd commit 6365d17

File tree

5 files changed

+53
-22
lines changed

5 files changed

+53
-22
lines changed

absl/container/BUILD.bazel

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ cc_library(
3333
"//absl/base:core_headers",
3434
"//absl/base:dynamic_annotations",
3535
"//absl/base:throw_delegate",
36+
"//absl/memory",
3637
],
3738
)
3839

@@ -42,7 +43,6 @@ cc_test(
4243
copts = ABSL_TEST_COPTS + ["-fexceptions"],
4344
deps = [
4445
":fixed_array",
45-
"//absl/base:core_headers",
4646
"//absl/base:exception_testing",
4747
"//absl/memory",
4848
"@com_google_googletest//:gtest_main",
@@ -55,7 +55,6 @@ cc_test(
5555
copts = ABSL_TEST_COPTS,
5656
deps = [
5757
":fixed_array",
58-
"//absl/base:core_headers",
5958
"//absl/base:exception_testing",
6059
"//absl/memory",
6160
"@com_google_googletest//:gtest_main",

absl/container/fixed_array.h

+13-5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "absl/base/macros.h"
4848
#include "absl/base/optimization.h"
4949
#include "absl/base/port.h"
50+
#include "absl/memory/memory.h"
5051

5152
namespace absl {
5253

@@ -107,6 +108,15 @@ class FixedArray {
107108
? kInlineBytesDefault / sizeof(value_type)
108109
: inlined;
109110

111+
FixedArray(const FixedArray& other) : rep_(other.begin(), other.end()) {}
112+
FixedArray(FixedArray&& other) noexcept(
113+
// clang-format off
114+
absl::allocator_is_nothrow<std::allocator<value_type>>::value &&
115+
// clang-format on
116+
std::is_nothrow_move_constructible<value_type>::value)
117+
: rep_(std::make_move_iterator(other.begin()),
118+
std::make_move_iterator(other.end())) {}
119+
110120
// Creates an array object that can store `n` elements.
111121
// Note that trivially constructible elements will be uninitialized.
112122
explicit FixedArray(size_type n) : rep_(n) {}
@@ -126,11 +136,9 @@ class FixedArray {
126136

127137
~FixedArray() {}
128138

129-
// Copy and move construction and assignment are deleted because (1) you can't
130-
// copy or move an array, (2) assignment breaks the invariant that the size of
131-
// a `FixedArray` never changes, and (3) there's no clear answer as to what
132-
// should happen to a moved-from `FixedArray`.
133-
FixedArray(const FixedArray&) = delete;
139+
// Assignments are deleted because they break the invariant that the size of a
140+
// `FixedArray` never changes.
141+
void operator=(FixedArray&&) = delete;
134142
void operator=(const FixedArray&) = delete;
135143

136144
// FixedArray::size()

absl/container/fixed_array_test.cc

+37
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "absl/base/internal/exception_testing.h"
2828
#include "absl/memory/memory.h"
2929

30+
using ::testing::ElementsAreArray;
31+
3032
namespace {
3133

3234
// Helper routine to determine if a absl::FixedArray used stack allocation.
@@ -89,6 +91,41 @@ class ThreeInts {
8991

9092
int ThreeInts::counter = 0;
9193

94+
TEST(FixedArrayTest, CopyCtor) {
95+
absl::FixedArray<int, 10> on_stack(5);
96+
std::iota(on_stack.begin(), on_stack.end(), 0);
97+
absl::FixedArray<int, 10> stack_copy = on_stack;
98+
EXPECT_THAT(stack_copy, ElementsAreArray(on_stack));
99+
EXPECT_TRUE(IsOnStack(stack_copy));
100+
101+
absl::FixedArray<int, 10> allocated(15);
102+
std::iota(allocated.begin(), allocated.end(), 0);
103+
absl::FixedArray<int, 10> alloced_copy = allocated;
104+
EXPECT_THAT(alloced_copy, ElementsAreArray(allocated));
105+
EXPECT_FALSE(IsOnStack(alloced_copy));
106+
}
107+
108+
TEST(FixedArrayTest, MoveCtor) {
109+
absl::FixedArray<std::unique_ptr<int>, 10> on_stack(5);
110+
for (int i = 0; i < 5; ++i) {
111+
on_stack[i] = absl::make_unique<int>(i);
112+
}
113+
114+
absl::FixedArray<std::unique_ptr<int>, 10> stack_copy = std::move(on_stack);
115+
for (int i = 0; i < 5; ++i) EXPECT_EQ(*(stack_copy[i]), i);
116+
EXPECT_EQ(stack_copy.size(), on_stack.size());
117+
118+
absl::FixedArray<std::unique_ptr<int>, 10> allocated(15);
119+
for (int i = 0; i < 15; ++i) {
120+
allocated[i] = absl::make_unique<int>(i);
121+
}
122+
123+
absl::FixedArray<std::unique_ptr<int>, 10> alloced_copy =
124+
std::move(allocated);
125+
for (int i = 0; i < 15; ++i) EXPECT_EQ(*(alloced_copy[i]), i);
126+
EXPECT_EQ(allocated.size(), alloced_copy.size());
127+
}
128+
92129
TEST(FixedArrayTest, SmallObjects) {
93130
// Small object arrays
94131
{

absl/strings/str_cat.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
// You can convert to hexadecimal output rather than decimal output using the
4747
// `Hex` type contained here. To do so, pass `Hex(my_int)` as a parameter to
4848
// `StrCat()` or `StrAppend()`. You may specify a minimum hex field width using
49-
// a `PadSpec` enum, so the equivalent of `StringPrintf("%04x", my_int)` is
50-
// `absl::StrCat(absl::Hex(my_int, absl::kZeroPad4))`.
49+
// a `PadSpec` enum.
5150
//
5251
// -----------------------------------------------------------------------------
5352

absl/strings/substitute.h

+1-13
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,6 @@
4545
// SubstituteAndAppend(&s, "My name is $0 and I am $1 years old.", "Bob", 5);
4646
// EXPECT_EQ("Hi. My name is Bob and I am 5 years old.", s);
4747
//
48-
// Differences from `StringPrintf()`:
49-
// * The format std::string does not identify the types of arguments. Instead, the
50-
// arguments are implicitly converted to strings. See below for a list of
51-
// accepted types.
52-
// * Substitutions in the format std::string are identified by a '$' followed by a
53-
// single digit. You can use arguments out-of-order and use the same
54-
// argument multiple times.
55-
// * A '$$' sequence in the format std::string means output a literal '$'
56-
// character.
57-
// * `Substitute()` is significantly faster than `StringPrintf()`. For very
58-
// large strings, it may be orders of magnitude faster.
5948
//
6049
// Supported types:
6150
// * absl::string_view, std::string, const char* (null is equivalent to "")
@@ -157,8 +146,7 @@ class Arg {
157146
Arg(bool value) // NOLINT(runtime/explicit)
158147
: piece_(value ? "true" : "false") {}
159148
// `void*` values, with the exception of `char*`, are printed as
160-
// `StringPrintf()` with format "%p": e.g. ("0x<hex value>").
161-
// However, in the case of `nullptr`, "NULL" is printed.
149+
// "0x<hex value>". However, in the case of `nullptr`, "NULL" is printed.
162150
Arg(const void* value); // NOLINT(runtime/explicit)
163151

164152
Arg(const Arg&) = delete;

0 commit comments

Comments
 (0)