rtc::Buffer improvements

  1. Constructors, SetData(), and AppendData() now accept uint8_t*,
     int8_t*, and char*. Previously, they accepted void*, meaning that
     any kind of pointer was accepted. I think requiring an explicit
     cast in cases where the input array isn't already of a byte-sized
     type is a better compromise between convenience and safety.

  2. data() can now return a uint8_t* instead of a char*, which seems
     more appropriate for a byte array, and is harder to mix up with
     zero-terminated C strings. data<int8_t>() is also available so
     that callers that want that type instead won't have to cast, as
     is data<char>() (which remains the default until all existing
     callers have been fixed).

  3. Constructors, SetData(), and AppendData() now accept arrays
     natively, not just decayed to pointers. The advantage of this is
     that callers don't have to pass the size separately.

  4. There are new constructors that allow setting size and capacity
     without initializing the array. Previously, this had to be done
     separately after construction.

  5. Instead of TransferTo(), Buffer now supports swap(), and move
     construction and assignment, and has a Pass() method that works
     just like std::move(). (The Pass method is modeled after
     scoped_ptr::Pass().)

R=jmarusic@webrtc.org, tommi@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/42989004

Cr-Commit-Position: refs/heads/master@{#9033}
diff --git a/webrtc/base/buffer.h b/webrtc/base/buffer.h
index c7fb959..5880f50 100644
--- a/webrtc/base/buffer.h
+++ b/webrtc/base/buffer.h
@@ -11,83 +11,215 @@
 #ifndef WEBRTC_BASE_BUFFER_H_
 #define WEBRTC_BASE_BUFFER_H_
 
-#include <string.h>
-
-#include "webrtc/base/common.h"
+#include <algorithm>  // std::swap (pre-C++11)
+#include <cassert>
+#include <cstring>
+#include <utility>  // std::swap (C++11 and later)
 #include "webrtc/base/scoped_ptr.h"
 
 namespace rtc {
 
+namespace internal {
+
+// (Internal; please don't use outside this file.) ByteType<T>::t is int if T
+// is uint8_t, int8_t, or char; otherwise, it's a compilation error. Use like
+// this:
+//
+//   template <typename T, typename ByteType<T>::t = 0>
+//   void foo(T* x);
+//
+// to let foo<T> be defined only for byte-sized integers.
+template <typename T>
+struct ByteType {
+ private:
+  static int F(uint8_t*);
+  static int F(int8_t*);
+  static int F(char*);
+
+ public:
+  using t = decltype(F(static_cast<T*>(nullptr)));
+};
+
+}  // namespace internal
+
 // Basic buffer class, can be grown and shrunk dynamically.
 // Unlike std::string/vector, does not initialize data when expanding capacity.
-class Buffer {
+class Buffer final {
  public:
-  Buffer();
+  Buffer();                   // An empty buffer.
+  Buffer(const Buffer& buf);  // Copy size and contents of an existing buffer.
+  Buffer(Buffer&& buf);       // Move contents from an existing buffer.
+
+  // Construct a buffer with the specified number of uninitialized bytes.
   explicit Buffer(size_t size);
-  Buffer(const void* data, size_t size);
-  Buffer(const void* data, size_t size, size_t capacity);
-  Buffer(const Buffer& buf);
+  Buffer(size_t size, size_t capacity);
+
+  // Construct a buffer and copy the specified number of bytes into it. The
+  // source array may be (const) uint8_t*, int8_t*, or char*.
+  template <typename T, typename internal::ByteType<T>::t = 0>
+  Buffer(const T* data, size_t size)
+      : Buffer(data, size, size) {}
+  template <typename T, typename internal::ByteType<T>::t = 0>
+  Buffer(const T* data, size_t size, size_t capacity)
+      : Buffer(size, capacity) {
+    std::memcpy(data_.get(), data, size);
+  }
+
+  // Construct a buffer from the contents of an array.
+  template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
+  Buffer(const T(&array)[N])
+      : Buffer(array, N) {}
+
   ~Buffer();
 
-  const char* data() const { return data_.get(); }
-  char* data() { return data_.get(); }
-  size_t size() const { return size_; }
-  size_t capacity() const { return capacity_; }
+  // Get a pointer to the data. Just .data() will give you a (const) char*,
+  // but you may also use .data<int8_t>() and .data<uint8_t>().
+  // TODO(kwiberg): Change default to uint8_t
+  template <typename T = char, typename internal::ByteType<T>::t = 0>
+  const T* data() const {
+    assert(IsConsistent());
+    return reinterpret_cast<T*>(data_.get());
+  }
+  template <typename T = char, typename internal::ByteType<T>::t = 0>
+  T* data() {
+    assert(IsConsistent());
+    return reinterpret_cast<T*>(data_.get());
+  }
+
+  size_t size() const {
+    assert(IsConsistent());
+    return size_;
+  }
+  size_t capacity() const {
+    assert(IsConsistent());
+    return capacity_;
+  }
 
   Buffer& operator=(const Buffer& buf) {
-    if (&buf != this) {
-      Construct(buf.data(), buf.size(), buf.size());
-    }
+    if (&buf != this)
+      SetData(buf.data(), buf.size());
     return *this;
   }
-  bool operator==(const Buffer& buf) const {
-    return (size_ == buf.size() && memcmp(data_.get(), buf.data(), size_) == 0);
-  }
-  bool operator!=(const Buffer& buf) const {
-    return !operator==(buf);
+  Buffer& operator=(Buffer&& buf) {
+    assert(IsConsistent());
+    assert(buf.IsConsistent());
+    size_ = buf.size_;
+    capacity_ = buf.capacity_;
+    data_ = buf.data_.Pass();
+    buf.OnMovedFrom();
+    return *this;
   }
 
-  void SetData(const void* data, size_t size) {
-    ASSERT(data != NULL || size == 0);
-    SetSize(size);
-    memcpy(data_.get(), data, size);
+  bool operator==(const Buffer& buf) const {
+    assert(IsConsistent());
+    return size_ == buf.size() && memcmp(data_.get(), buf.data(), size_) == 0;
   }
-  void AppendData(const void* data, size_t size) {
-    ASSERT(data != NULL || size == 0);
-    size_t old_size = size_;
-    SetSize(size_ + size);
-    memcpy(data_.get() + old_size, data, size);
+
+  bool operator!=(const Buffer& buf) const { return !(*this == buf); }
+
+  // Replace the contents of the buffer. Accepts the same types as the
+  // constructors.
+  template <typename T, typename internal::ByteType<T>::t = 0>
+  void SetData(const T* data, size_t size) {
+    assert(IsConsistent());
+    size_ = 0;
+    AppendData(data, size);
   }
+  template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
+  void SetData(const T(&array)[N]) {
+    SetData(array, N);
+  }
+  void SetData(const Buffer& buf) { SetData(buf.data(), buf.size()); }
+
+  // Append data to the buffer. Accepts the same types as the constructors.
+  template <typename T, typename internal::ByteType<T>::t = 0>
+  void AppendData(const T* data, size_t size) {
+    assert(IsConsistent());
+    const size_t new_size = size_ + size;
+    EnsureCapacity(new_size);
+    std::memcpy(data_.get() + size_, data, size);
+    size_ = new_size;
+    assert(IsConsistent());
+  }
+  template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
+  void AppendData(const T(&array)[N]) {
+    AppendData(array, N);
+  }
+  void AppendData(const Buffer& buf) { AppendData(buf.data(), buf.size()); }
+
+  // Sets the size of the buffer. If the new size is smaller than the old, the
+  // buffer contents will be kept but truncated; if the new size is greater,
+  // the existing contents will be kept and the new space will be
+  // uninitialized.
   void SetSize(size_t size) {
-    SetCapacity(size);
+    EnsureCapacity(size);
     size_ = size;
   }
-  void SetCapacity(size_t capacity) {
-    if (capacity > capacity_) {
-      rtc::scoped_ptr<char[]> data(new char[capacity]);
-      memcpy(data.get(), data_.get(), size_);
-      data_.swap(data);
-      capacity_ = capacity;
-    }
+
+  // Ensure that the buffer size can be increased to at least capacity without
+  // further reallocation. (Of course, this operation might need to reallocate
+  // the buffer.)
+  void EnsureCapacity(size_t capacity) {
+    assert(IsConsistent());
+    if (capacity <= capacity_)
+      return;
+    scoped_ptr<uint8_t[]> new_data(new uint8_t[capacity]);
+    std::memcpy(new_data.get(), data_.get(), size_);
+    data_ = new_data.Pass();
+    capacity_ = capacity;
+    assert(IsConsistent());
   }
 
-  void TransferTo(Buffer* buf) {
-    ASSERT(buf != NULL);
-    buf->data_.reset(data_.release());
-    buf->size_ = size_;
-    buf->capacity_ = capacity_;
-    Construct(NULL, 0, 0);
+  // We can't call std::move(b), so call b.Pass() instead to do the same job.
+  Buffer&& Pass() {
+    assert(IsConsistent());
+    return static_cast<Buffer&&>(*this);
   }
 
- protected:
-  void Construct(const void* data, size_t size, size_t capacity) {
-    data_.reset(new char[capacity_ = capacity]);
-    SetData(data, size);
+  // Resets the buffer to zero size and capacity. Works even if the buffer has
+  // been moved from.
+  void Clear() {
+    data_.reset();
+    size_ = 0;
+    capacity_ = 0;
+    assert(IsConsistent());
   }
 
-  scoped_ptr<char[]> data_;
+  // Swaps two buffers. Also works for buffers that have been moved from.
+  friend void swap(Buffer& a, Buffer& b) {
+    using std::swap;
+    swap(a.size_, b.size_);
+    swap(a.capacity_, b.capacity_);
+    swap(a.data_, b.data_);
+  }
+
+ private:
+  // Precondition for all methods except Clear and the destructor.
+  // Postcondition for all methods except move construction and move
+  // assignment, which leave the moved-from object in a possibly inconsistent
+  // state.
+  bool IsConsistent() const {
+    return (data_ || capacity_ == 0) && capacity_ >= size_;
+  }
+
+  // Called when *this has been moved from. Conceptually it's a no-op, but we
+  // can mutate the state slightly to help subsequent sanity checks catch bugs.
+  void OnMovedFrom() {
+#ifdef NDEBUG
+    // Make *this consistent and empty. Shouldn't be necessary, but better safe
+    // than sorry.
+    size_ = 0;
+    capacity_ = 0;
+#else
+    // Ensure that *this is always inconsistent, to provoke bugs.
+    size_ = 1;
+    capacity_ = 0;
+#endif
+  }
+
   size_t size_;
   size_t capacity_;
+  scoped_ptr<uint8_t[]> data_;
 };
 
 }  // namespace rtc