Skip to content

Commit 0f95fa4

Browse files
committed
Add idiomatic C++ API for grpc::Slice construction that doesn't
require using grpc_slice
1 parent c39d4c1 commit 0f95fa4

File tree

7 files changed

+67
-24
lines changed

7 files changed

+67
-24
lines changed

include/grpc++/support/slice.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ class Slice final {
4444
/// Construct a slice from \a slice, stealing a reference.
4545
Slice(grpc_slice slice, StealRef);
4646

47+
/// Allocate a slice of specified size
48+
Slice(size_t len);
49+
50+
/// Construct a slice from a copied buffer
51+
Slice(const void* buf, size_t len);
52+
53+
/// Construct a slice from a copied string
54+
Slice(const grpc::string& str);
55+
56+
enum StaticSlice { STATIC_SLICE };
57+
58+
/// Construct a slice from a static buffer
59+
Slice(const void* buf, size_t len, StaticSlice);
60+
4761
/// Copy constructor, adds a reference.
4862
Slice(const Slice& other);
4963

src/cpp/util/slice_cc.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ Slice::Slice(grpc_slice slice, AddRef) : slice_(grpc_slice_ref(slice)) {}
2828

2929
Slice::Slice(grpc_slice slice, StealRef) : slice_(slice) {}
3030

31+
Slice::Slice(size_t len) : slice_(grpc_slice_malloc(len)) {}
32+
33+
Slice::Slice(const void* buf, size_t len)
34+
: slice_(grpc_slice_from_copied_buffer(reinterpret_cast<const char*>(buf),
35+
len)) {}
36+
37+
Slice::Slice(const grpc::string& str)
38+
: slice_(grpc_slice_from_copied_buffer(str.c_str(), str.length())) {}
39+
40+
Slice::Slice(const void* buf, size_t len, StaticSlice)
41+
: slice_(grpc_slice_from_static_buffer(reinterpret_cast<const char*>(buf),
42+
len)) {}
43+
3144
Slice::Slice(const Slice& other) : slice_(grpc_slice_ref(other.slice_)) {}
3245

3346
} // namespace grpc

test/cpp/qps/client.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ class ClientRequestCreator<ByteBuffer> {
8888
if (payload_config.has_bytebuf_params()) {
8989
std::unique_ptr<char[]> buf(
9090
new char[payload_config.bytebuf_params().req_size()]);
91-
grpc_slice s = grpc_slice_from_copied_buffer(
92-
buf.get(), payload_config.bytebuf_params().req_size());
93-
Slice slice(s, Slice::STEAL_REF);
91+
Slice slice(buf.get(), payload_config.bytebuf_params().req_size());
9492
*req = ByteBuffer(&slice, 1);
9593
} else {
9694
GPR_ASSERT(false); // not appropriate for this specialization

test/cpp/util/byte_buffer_proto_helper.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ std::unique_ptr<ByteBuffer> SerializeToByteBuffer(
3636
grpc::protobuf::Message* message) {
3737
grpc::string buf;
3838
message->SerializeToString(&buf);
39-
grpc_slice s = grpc_slice_from_copied_string(buf.c_str());
40-
Slice slice(s, Slice::STEAL_REF);
39+
Slice slice(buf);
4140
return std::unique_ptr<ByteBuffer>(new ByteBuffer(&slice, 1));
4241
}
4342

test/cpp/util/byte_buffer_test.cc

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,30 @@ const char* kContent2 = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy world";
3434
class ByteBufferTest : public ::testing::Test {};
3535

3636
TEST_F(ByteBufferTest, CreateFromSingleSlice) {
37-
grpc_slice hello = grpc_slice_from_copied_string(kContent1);
38-
Slice s(hello, Slice::STEAL_REF);
37+
Slice s(kContent1);
3938
ByteBuffer buffer(&s, 1);
39+
EXPECT_EQ(strlen(kContent1), buffer.Length());
4040
}
4141

4242
TEST_F(ByteBufferTest, CreateFromVector) {
43-
grpc_slice hello = grpc_slice_from_copied_string(kContent1);
44-
grpc_slice world = grpc_slice_from_copied_string(kContent2);
4543
std::vector<Slice> slices;
46-
slices.push_back(Slice(hello, Slice::STEAL_REF));
47-
slices.push_back(Slice(world, Slice::STEAL_REF));
44+
slices.emplace_back(kContent1);
45+
slices.emplace_back(kContent2);
4846
ByteBuffer buffer(&slices[0], 2);
47+
EXPECT_EQ(strlen(kContent1) + strlen(kContent2), buffer.Length());
4948
}
5049

5150
TEST_F(ByteBufferTest, Clear) {
52-
grpc_slice hello = grpc_slice_from_copied_string(kContent1);
53-
Slice s(hello, Slice::STEAL_REF);
51+
Slice s(kContent1);
5452
ByteBuffer buffer(&s, 1);
5553
buffer.Clear();
54+
EXPECT_EQ(static_cast<size_t>(0), buffer.Length());
5655
}
5756

5857
TEST_F(ByteBufferTest, Length) {
59-
grpc_slice hello = grpc_slice_from_copied_string(kContent1);
60-
grpc_slice world = grpc_slice_from_copied_string(kContent2);
6158
std::vector<Slice> slices;
62-
slices.push_back(Slice(hello, Slice::STEAL_REF));
63-
slices.push_back(Slice(world, Slice::STEAL_REF));
59+
slices.emplace_back(kContent1);
60+
slices.emplace_back(kContent2);
6461
ByteBuffer buffer(&slices[0], 2);
6562
EXPECT_EQ(strlen(kContent1) + strlen(kContent2), buffer.Length());
6663
}

test/cpp/util/cli_call.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ void CliCall::WritesDone() {
119119
}
120120

121121
void CliCall::WriteAndWait(const grpc::string& request) {
122-
grpc_slice s = grpc_slice_from_copied_string(request.c_str());
123-
grpc::Slice req_slice(s, grpc::Slice::STEAL_REF);
122+
grpc::Slice req_slice(request);
124123
grpc::ByteBuffer send_buffer(&req_slice, 1);
125124

126125
gpr_mu_lock(&write_mu_);

test/cpp/util/slice_test.cc

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,41 @@ const char* kContent = "hello xxxxxxxxxxxxxxxxxxxx world";
2828

2929
class SliceTest : public ::testing::Test {
3030
protected:
31+
void CheckSliceSize(const Slice& s, const grpc::string& content) {
32+
EXPECT_EQ(content.size(), s.size());
33+
}
3134
void CheckSlice(const Slice& s, const grpc::string& content) {
3235
EXPECT_EQ(content.size(), s.size());
3336
EXPECT_EQ(content,
3437
grpc::string(reinterpret_cast<const char*>(s.begin()), s.size()));
3538
}
3639
};
3740

41+
TEST_F(SliceTest, Empty) {
42+
Slice empty_slice;
43+
CheckSlice(empty_slice, "");
44+
}
45+
46+
TEST_F(SliceTest, Sized) {
47+
Slice sized_slice(strlen(kContent));
48+
CheckSliceSize(sized_slice, kContent);
49+
}
50+
51+
TEST_F(SliceTest, String) {
52+
Slice spp(kContent);
53+
CheckSlice(spp, kContent);
54+
}
55+
56+
TEST_F(SliceTest, Buf) {
57+
Slice spp(kContent, strlen(kContent));
58+
CheckSlice(spp, kContent);
59+
}
60+
61+
TEST_F(SliceTest, StaticBuf) {
62+
Slice spp(kContent, strlen(kContent), Slice::STATIC_SLICE);
63+
CheckSlice(spp, kContent);
64+
}
65+
3866
TEST_F(SliceTest, Steal) {
3967
grpc_slice s = grpc_slice_from_copied_string(kContent);
4068
Slice spp(s, Slice::STEAL_REF);
@@ -48,11 +76,6 @@ TEST_F(SliceTest, Add) {
4876
CheckSlice(spp, kContent);
4977
}
5078

51-
TEST_F(SliceTest, Empty) {
52-
Slice empty_slice;
53-
CheckSlice(empty_slice, "");
54-
}
55-
5679
TEST_F(SliceTest, Cslice) {
5780
grpc_slice s = grpc_slice_from_copied_string(kContent);
5881
Slice spp(s, Slice::STEAL_REF);

0 commit comments

Comments
 (0)