Add API to get c slice from c++ Slice.

pull/7044/head
yang-g 9 years ago
parent d605b63383
commit a324c4fea6
  1. 3
      include/grpc++/support/slice.h
  2. 10
      test/cpp/util/slice_test.cc

@ -77,6 +77,9 @@ class Slice GRPC_FINAL {
/// Raw pointer to the end (one byte \em past the last element) of the slice. /// Raw pointer to the end (one byte \em past the last element) of the slice.
const uint8_t* end() const { return GPR_SLICE_END_PTR(slice_); } const uint8_t* end() const { return GPR_SLICE_END_PTR(slice_); }
/// Raw C slice. Caller needs to call gpr_slice_unref when done.
gpr_slice c_slice() const { return gpr_slice_ref(slice_); }
private: private:
friend class ByteBuffer; friend class ByteBuffer;

@ -68,6 +68,16 @@ TEST_F(SliceTest, Empty) {
CheckSlice(empty_slice, ""); CheckSlice(empty_slice, "");
} }
TEST_F(SliceTest, Cslice) {
gpr_slice s = gpr_slice_from_copied_string(kContent);
Slice spp(s, Slice::STEAL_REF);
CheckSlice(spp, kContent);
gpr_slice c_slice = spp.c_slice();
EXPECT_EQ(GPR_SLICE_START_PTR(s), GPR_SLICE_START_PTR(c_slice));
EXPECT_EQ(GPR_SLICE_END_PTR(s), GPR_SLICE_END_PTR(c_slice));
gpr_slice_unref(c_slice);
}
} // namespace } // namespace
} // namespace grpc } // namespace grpc

Loading…
Cancel
Save