Update .DEPS.git
[chromium-blink-merge.git] / base / memory / ref_counted_memory.h
blob9edcd6fdc94d7f358596dcc2a39153a38d7e0b0f
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_
6 #define BASE_MEMORY_REF_COUNTED_MEMORY_H_
8 #include <string>
9 #include <vector>
11 #include "base/base_export.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
15 namespace base {
17 // A generic interface to memory. This object is reference counted because one
18 // of its two subclasses own the data they carry, and we need to have
19 // heterogeneous containers of these two types of memory.
20 class BASE_EXPORT RefCountedMemory
21 : public base::RefCountedThreadSafe<RefCountedMemory> {
22 public:
23 // Retrieves a pointer to the beginning of the data we point to. If the data
24 // is empty, this will return NULL.
25 virtual const unsigned char* front() const = 0;
27 // Size of the memory pointed to.
28 virtual size_t size() const = 0;
30 protected:
31 friend class base::RefCountedThreadSafe<RefCountedMemory>;
32 RefCountedMemory();
33 virtual ~RefCountedMemory();
36 // An implementation of RefCountedMemory, where the ref counting does not
37 // matter.
38 class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory {
39 public:
40 RefCountedStaticMemory()
41 : data_(NULL), length_(0) {}
42 RefCountedStaticMemory(const unsigned char* data, size_t length)
43 : data_(length ? data : NULL), length_(length) {}
45 // Overridden from RefCountedMemory:
46 virtual const unsigned char* front() const OVERRIDE;
47 virtual size_t size() const OVERRIDE;
49 private:
50 virtual ~RefCountedStaticMemory();
52 const unsigned char* data_;
53 size_t length_;
55 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
58 // An implementation of RefCountedMemory, where we own our the data in a
59 // vector.
60 class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
61 public:
62 RefCountedBytes();
64 // Constructs a RefCountedBytes object by _copying_ from |initializer|.
65 RefCountedBytes(const std::vector<unsigned char>& initializer);
67 // Constructs a RefCountedBytes object by performing a swap. (To non
68 // destructively build a RefCountedBytes, use the constructor that takes a
69 // vector.)
70 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy);
72 // Overridden from RefCountedMemory:
73 virtual const unsigned char* front() const OVERRIDE;
74 virtual size_t size() const OVERRIDE;
76 const std::vector<unsigned char>& data() const { return data_; }
77 std::vector<unsigned char>& data() { return data_; }
79 private:
80 virtual ~RefCountedBytes();
82 std::vector<unsigned char> data_;
84 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
87 // An implementation of RefCountedMemory, where the bytes are stored in an STL
88 // string. Use this if your data naturally arrives in that format.
89 class BASE_EXPORT RefCountedString : public RefCountedMemory {
90 public:
91 RefCountedString();
93 // Constructs a RefCountedString object by performing a swap. (To non
94 // destructively build a RefCountedString, use the default constructor and
95 // copy into object->data()).
96 static RefCountedString* TakeString(std::string* to_destroy);
98 // Overridden from RefCountedMemory:
99 virtual const unsigned char* front() const OVERRIDE;
100 virtual size_t size() const OVERRIDE;
102 const std::string& data() const { return data_; }
103 std::string& data() { return data_; }
105 private:
106 virtual ~RefCountedString();
108 std::string data_;
110 DISALLOW_COPY_AND_ASSIGN(RefCountedString);
113 } // namespace base
115 #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_