media/mtransport/databuffer.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/mtransport/databuffer.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,47 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// Original author: ekr@rtfm.com
    1.11 +
    1.12 +#ifndef databuffer_h__
    1.13 +#define databuffer_h__
    1.14 +#include <algorithm>
    1.15 +#include <mozilla/Scoped.h>
    1.16 +#include <m_cpp_utils.h>
    1.17 +#include <nsISupportsImpl.h>
    1.18 +
    1.19 +namespace mozilla {
    1.20 +
    1.21 +class DataBuffer {
    1.22 + public:
    1.23 +  DataBuffer() : data_(nullptr), len_(0) {}
    1.24 +  DataBuffer(const uint8_t *data, size_t len) {
    1.25 +    Assign(data, len);
    1.26 +  }
    1.27 +
    1.28 +  void Assign(const uint8_t *data, size_t len) {
    1.29 +    data_ = new unsigned char[ len ? len : 1];  // Don't depend on new [0].
    1.30 +    memcpy(static_cast<void *>(data_.get()),
    1.31 +           static_cast<const void *>(data), len);
    1.32 +    len_ = len;
    1.33 +  }
    1.34 +
    1.35 +  const uint8_t *data() const { return data_; }
    1.36 +  size_t len() const { return len_; }
    1.37 +  const bool empty() const { return len_ != 0; }
    1.38 +
    1.39 +  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DataBuffer)
    1.40 +
    1.41 +private:
    1.42 +  ScopedDeleteArray<uint8_t> data_;
    1.43 +  size_t len_;
    1.44 +
    1.45 +  DISALLOW_COPY_ASSIGN(DataBuffer);
    1.46 +};
    1.47 +
    1.48 +}
    1.49 +
    1.50 +#endif

mercurial