Files
platform-external-webrtc/webrtc/base/buffer.h
kwiberg@webrtc.org 67186fe00c Fix clang style warnings in webrtc/base
Mostly this consists of marking functions with override when
applicable, and moving function bodies from .h to .cc files.

Not inlining virtual functions with simple bodies such as

  { return false; }

strikes me as probably losing more in readability than we gain in
binary size and compilation time, but I guess it's just like any other
case where enabling a generally good warning forces us to write
slightly worse code in a couple of places.

BUG=163
R=kjellander@webrtc.org, tommi@webrtc.org

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

Cr-Commit-Position: refs/heads/master@{#8656}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8656 4adac7df-926f-26a2-2b94-8c16560cd09d
2015-03-09 22:24:25 +00:00

97 lines
2.6 KiB
C++

/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_BASE_BUFFER_H_
#define WEBRTC_BASE_BUFFER_H_
#include <string.h>
#include "webrtc/base/common.h"
#include "webrtc/base/scoped_ptr.h"
namespace rtc {
// Basic buffer class, can be grown and shrunk dynamically.
// Unlike std::string/vector, does not initialize data when expanding capacity.
class Buffer {
public:
Buffer();
Buffer(const void* data, size_t length);
Buffer(const void* data, size_t length, size_t capacity);
Buffer(const Buffer& buf);
~Buffer();
const char* data() const { return data_.get(); }
char* data() { return data_.get(); }
// TODO: should this be size(), like STL?
size_t length() const { return length_; }
size_t capacity() const { return capacity_; }
Buffer& operator=(const Buffer& buf) {
if (&buf != this) {
Construct(buf.data(), buf.length(), buf.length());
}
return *this;
}
bool operator==(const Buffer& buf) const {
return (length_ == buf.length() &&
memcmp(data_.get(), buf.data(), length_) == 0);
}
bool operator!=(const Buffer& buf) const {
return !operator==(buf);
}
void SetData(const void* data, size_t length) {
ASSERT(data != NULL || length == 0);
SetLength(length);
memcpy(data_.get(), data, length);
}
void AppendData(const void* data, size_t length) {
ASSERT(data != NULL || length == 0);
size_t old_length = length_;
SetLength(length_ + length);
memcpy(data_.get() + old_length, data, length);
}
void SetLength(size_t length) {
SetCapacity(length);
length_ = length;
}
void SetCapacity(size_t capacity) {
if (capacity > capacity_) {
rtc::scoped_ptr<char[]> data(new char[capacity]);
memcpy(data.get(), data_.get(), length_);
data_.swap(data);
capacity_ = capacity;
}
}
void TransferTo(Buffer* buf) {
ASSERT(buf != NULL);
buf->data_.reset(data_.release());
buf->length_ = length_;
buf->capacity_ = capacity_;
Construct(NULL, 0, 0);
}
protected:
void Construct(const void* data, size_t length, size_t capacity) {
data_.reset(new char[capacity_ = capacity]);
SetData(data, length);
}
scoped_ptr<char[]> data_;
size_t length_;
size_t capacity_;
};
} // namespace rtc
#endif // WEBRTC_BASE_BUFFER_H_