diff options
author | joth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-22 13:22:23 +0000 |
---|---|---|
committer | joth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-22 13:22:23 +0000 |
commit | 1dda97773521130a156179a18528adb3bd5ae257 (patch) | |
tree | 6a4be6f874c5ad4726ae8d61fcbd14a39a1cb859 /base/memory | |
parent | efbd0286463c67eb0ed3787e37a3c1aa43c66eee (diff) | |
download | chromium_src-1dda97773521130a156179a18528adb3bd5ae257.zip chromium_src-1dda97773521130a156179a18528adb3bd5ae257.tar.gz chromium_src-1dda97773521130a156179a18528adb3bd5ae257.tar.bz2 |
Re-land r93365 - add RefCountedString
Added RefCountedString, as this is what many RefCountedMemory users seem to want
Made data member of RefCountedBytes private, as per style guide
Changed base64 APIs to accept StringPiece, as it's sometimes better and never worse than string.
Fix chromeos build; additional files updated comapred to previous patch:
chrome/browser/ui/login/login_prompt_ui.cc
chrome/browser/ui/webui/active_downloads_ui.cc
chrome/browser/ui/webui/chromeos/choose_mobile_network_ui.cc
chrome/browser/ui/webui/chromeos/enterprise_enrollment_ui.cc
chrome/browser/ui/webui/chromeos/imageburner/imageburner_ui.cc
chrome/browser/ui/webui/chromeos/keyboard_overlay_ui.cc
chrome/browser/ui/webui/chromeos/login/login_ui.cc
chrome/browser/ui/webui/chromeos/login/login_ui_helpers.cc
chrome/browser/ui/webui/chromeos/login/login_ui_helpers.h
chrome/browser/ui/webui/chromeos/login/mock_login_ui_helpers.h
chrome/browser/ui/webui/chromeos/login/oobe_ui.cc
chrome/browser/ui/webui/chromeos/mobile_setup_ui.cc
chrome/browser/ui/webui/chromeos/proxy_settings_ui.cc
chrome/browser/ui/webui/chromeos/register_page_ui.cc
chrome/browser/ui/webui/chromeos/sim_unlock_ui.cc
chrome/browser/ui/webui/chromeos/system_info_ui.cc
chrome/browser/ui/webui/collected_cookies_ui_delegate.cc
BUG=None
TEST=All existing tests pass. Extended ref_counted_memory_unittests
Review URL: http://codereview.chromium.org/7397021
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@93617 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/memory')
-rw-r--r-- | base/memory/ref_counted_memory.cc | 34 | ||||
-rw-r--r-- | base/memory/ref_counted_memory.h | 55 | ||||
-rw-r--r-- | base/memory/ref_counted_memory_unittest.cc | 45 |
3 files changed, 120 insertions, 14 deletions
diff --git a/base/memory/ref_counted_memory.cc b/base/memory/ref_counted_memory.cc index aa16031..7e034f9 100644 --- a/base/memory/ref_counted_memory.cc +++ b/base/memory/ref_counted_memory.cc @@ -4,6 +4,8 @@ #include "base/memory/ref_counted_memory.h" +#include "base/logging.h" + RefCountedMemory::RefCountedMemory() { } @@ -22,25 +24,49 @@ RefCountedBytes::RefCountedBytes() { } RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer) - : data(initializer) { + : data_(initializer) { } RefCountedBytes* RefCountedBytes::TakeVector( std::vector<unsigned char>* to_destroy) { RefCountedBytes* bytes = new RefCountedBytes; - bytes->data.swap(*to_destroy); + bytes->data_.swap(*to_destroy); return bytes; } const unsigned char* RefCountedBytes::front() const { // STL will assert if we do front() on an empty vector, but calling code // expects a NULL. - return size() ? &data.front() : NULL; + return size() ? &data_.front() : NULL; } size_t RefCountedBytes::size() const { - return data.size(); + return data_.size(); } RefCountedBytes::~RefCountedBytes() { } + +namespace base { + +RefCountedString::RefCountedString() {} + +RefCountedString::~RefCountedString() {} + +// static +RefCountedString* RefCountedString::TakeString(std::string* to_destroy) { + RefCountedString* self = new RefCountedString; + to_destroy->swap(self->data_); + return self; +} + +const unsigned char* RefCountedString::front() const { + return data_.empty() ? NULL : + reinterpret_cast<const unsigned char*>(data_.data()); +} + +size_t RefCountedString::size() const { + return data_.size(); +} + +} // namespace base diff --git a/base/memory/ref_counted_memory.h b/base/memory/ref_counted_memory.h index 1a0f51ee..7438d57 100644 --- a/base/memory/ref_counted_memory.h +++ b/base/memory/ref_counted_memory.h @@ -6,9 +6,11 @@ #define BASE_MEMORY_REF_COUNTED_MEMORY_H_ #pragma once +#include <string> #include <vector> #include "base/base_api.h" +#include "base/compiler_specific.h" #include "base/memory/ref_counted.h" // TODO(erg): The contents of this file should be in a namespace. This would @@ -40,11 +42,11 @@ class BASE_API RefCountedStaticMemory : public RefCountedMemory { RefCountedStaticMemory() : data_(NULL), length_(0) {} RefCountedStaticMemory(const unsigned char* data, size_t length) - : data_(data), length_(length) {} + : data_(length ? data : NULL), length_(length) {} - // Overriden from RefCountedMemory: - virtual const unsigned char* front() const; - virtual size_t size() const; + // Overridden from RefCountedMemory: + virtual const unsigned char* front() const OVERRIDE; + virtual size_t size() const OVERRIDE; private: const unsigned char* data_; @@ -67,18 +69,51 @@ class BASE_API RefCountedBytes : public RefCountedMemory { // vector.) static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy); - // Overriden from RefCountedMemory: - virtual const unsigned char* front() const; - virtual size_t size() const; + // Overridden from RefCountedMemory: + virtual const unsigned char* front() const OVERRIDE; + virtual size_t size() const OVERRIDE; - std::vector<unsigned char> data; + const std::vector<unsigned char>& data() const { return data_; } + std::vector<unsigned char>& data() { return data_; } - protected: + private: friend class base::RefCountedThreadSafe<RefCountedBytes>; virtual ~RefCountedBytes(); - private: + std::vector<unsigned char> data_; + DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); }; +namespace base { + +// An implementation of RefCountedMemory, where the bytes are stored in an STL +// string. Use this if your data naturally arrives in that format. +class BASE_API RefCountedString : public RefCountedMemory { + public: + RefCountedString(); + + // Constructs a RefCountedString object by performing a swap. (To non + // destructively build a RefCountedString, use the default constructor and + // copy into object->data()). + static RefCountedString* TakeString(std::string* to_destroy); + + // Overridden from RefCountedMemory: + virtual const unsigned char* front() const OVERRIDE; + virtual size_t size() const OVERRIDE; + + const std::string& data() const { return data_; } + std::string& data() { return data_; } + + private: + friend class base::RefCountedThreadSafe<RefCountedString>; + virtual ~RefCountedString(); + + std::string data_; + + DISALLOW_COPY_AND_ASSIGN(RefCountedString); +}; + +} // namespace base + #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_ diff --git a/base/memory/ref_counted_memory_unittest.cc b/base/memory/ref_counted_memory_unittest.cc new file mode 100644 index 0000000..1936040 --- /dev/null +++ b/base/memory/ref_counted_memory_unittest.cc @@ -0,0 +1,45 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/memory/ref_counted_memory.h" + +#include "testing/gtest/include/gtest/gtest.h" + +namespace base { + +TEST(RefCountedMemoryUnitTest, RefCountedStaticMemory) { + scoped_refptr<RefCountedMemory> mem = new RefCountedStaticMemory( + reinterpret_cast<const uint8*>("static mem00"), 10); + + EXPECT_EQ(10U, mem->size()); + EXPECT_EQ("static mem", + std::string(reinterpret_cast<const char*>(mem->front()), + mem->size())); +} + +TEST(RefCountedMemoryUnitTest, RefCountedBytes) { + std::vector<uint8> data; + data.push_back(45); + data.push_back(99); + scoped_refptr<RefCountedMemory> mem = RefCountedBytes::TakeVector(&data); + + EXPECT_EQ(0U, data.size()); + + EXPECT_EQ(2U, mem->size()); + EXPECT_EQ(45U, mem->front()[0]); + EXPECT_EQ(99U, mem->front()[1]); +} + +TEST(RefCountedMemoryUnitTest, RefCountedString) { + std::string s("destroy me"); + scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s); + + EXPECT_EQ(0U, s.size()); + + EXPECT_EQ(10U, mem->size()); + EXPECT_EQ('d', mem->front()[0]); + EXPECT_EQ('e', mem->front()[1]); +} + +} // namespace base |