diff options
author | joth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-21 09:51:40 +0000 |
---|---|---|
committer | joth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-21 09:51:40 +0000 |
commit | 289f5a7870a3f1e5ed7b465556125515a6e474b0 (patch) | |
tree | 6c2569a80bd165d905f7c84cc04798dda9fb0f60 /base | |
parent | dc20613ba3e16cf90dc862cdd27c989a1388a6b0 (diff) | |
download | chromium_src-289f5a7870a3f1e5ed7b465556125515a6e474b0.zip chromium_src-289f5a7870a3f1e5ed7b465556125515a6e474b0.tar.gz chromium_src-289f5a7870a3f1e5ed7b465556125515a6e474b0.tar.bz2 |
Revert 93365 - it broke on Chrome OS
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.
BUG=None
TEST=All existing tests pass. Extended ref_counted_memory_unittests
Review URL: http://codereview.chromium.org/7397021
TBR=joth@chromium.org
Review URL: http://codereview.chromium.org/7471033
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@93367 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 1 | ||||
-rw-r--r-- | base/base64.cc | 4 | ||||
-rw-r--r-- | base/base64.h | 5 | ||||
-rw-r--r-- | base/memory/ref_counted_memory.cc | 34 | ||||
-rw-r--r-- | base/memory/ref_counted_memory.h | 51 | ||||
-rw-r--r-- | base/memory/ref_counted_memory_unittest.cc | 45 |
6 files changed, 16 insertions, 124 deletions
diff --git a/base/base.gyp b/base/base.gyp index d952161..6a7cfd4 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -144,7 +144,6 @@ 'md5_unittest.cc', 'memory/linked_ptr_unittest.cc', 'memory/mru_cache_unittest.cc', - 'memory/ref_counted_memory_unittest.cc', 'memory/ref_counted_unittest.cc', 'memory/scoped_ptr_unittest.cc', 'memory/scoped_vector_unittest.cc', diff --git a/base/base64.cc b/base/base64.cc index 62bd12d..56a577d 100644 --- a/base/base64.cc +++ b/base/base64.cc @@ -8,7 +8,7 @@ namespace base { -bool Base64Encode(const StringPiece& input, std::string* output) { +bool Base64Encode(const std::string& input, std::string* output) { std::string temp; temp.resize(modp_b64_encode_len(input.size())); // makes room for null byte @@ -23,7 +23,7 @@ bool Base64Encode(const StringPiece& input, std::string* output) { return true; } -bool Base64Decode(const StringPiece& input, std::string* output) { +bool Base64Decode(const std::string& input, std::string* output) { std::string temp; temp.resize(modp_b64_decode_len(input.size())); diff --git a/base/base64.h b/base/base64.h index e966ea7..294fb83 100644 --- a/base/base64.h +++ b/base/base64.h @@ -9,17 +9,16 @@ #include <string> #include "base/base_api.h" -#include "base/string_piece.h" namespace base { // Encodes the input string in base64. Returns true if successful and false // otherwise. The output string is only modified if successful. -BASE_API bool Base64Encode(const StringPiece& input, std::string* output); +BASE_API bool Base64Encode(const std::string& input, std::string* output); // Decodes the base64 input string. Returns true if successful and false // otherwise. The output string is only modified if successful. -BASE_API bool Base64Decode(const StringPiece& input, std::string* output); +BASE_API bool Base64Decode(const std::string& input, std::string* output); } // namespace base diff --git a/base/memory/ref_counted_memory.cc b/base/memory/ref_counted_memory.cc index 7e034f9..aa16031 100644 --- a/base/memory/ref_counted_memory.cc +++ b/base/memory/ref_counted_memory.cc @@ -4,8 +4,6 @@ #include "base/memory/ref_counted_memory.h" -#include "base/logging.h" - RefCountedMemory::RefCountedMemory() { } @@ -24,49 +22,25 @@ 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 0d48f64..1a0f51ee 100644 --- a/base/memory/ref_counted_memory.h +++ b/base/memory/ref_counted_memory.h @@ -6,11 +6,9 @@ #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 @@ -42,9 +40,9 @@ class BASE_API RefCountedStaticMemory : public RefCountedMemory { RefCountedStaticMemory() : data_(NULL), length_(0) {} RefCountedStaticMemory(const unsigned char* data, size_t length) - : data_(length ? data : NULL), length_(length) {} + : data_(data), length_(length) {} - // Overridden from RefCountedMemory: + // Overriden from RefCountedMemory: virtual const unsigned char* front() const; virtual size_t size() const; @@ -69,51 +67,18 @@ class BASE_API RefCountedBytes : public RefCountedMemory { // vector.) static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy); - // Overridden from RefCountedMemory: - virtual const unsigned char* front() const OVERRIDE; - virtual size_t size() const OVERRIDE; + // Overriden from RefCountedMemory: + virtual const unsigned char* front() const; + virtual size_t size() const; - const std::vector<unsigned char>& data() const { return data_; } - std::vector<unsigned char>& data() { return data_; } + std::vector<unsigned char> data; - private: + protected: friend class base::RefCountedThreadSafe<RefCountedBytes>; virtual ~RefCountedBytes(); - 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); + DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); }; -} // 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 deleted file mode 100644 index 1936040..0000000 --- a/base/memory/ref_counted_memory_unittest.cc +++ /dev/null @@ -1,45 +0,0 @@ -// 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 |