Initialize Tizen 2.3
[external/chromium.git] / base / memory / ref_counted_memory.h
1 // Copyright (c) 2011 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.
4
5 #ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_
6 #define BASE_MEMORY_REF_COUNTED_MEMORY_H_
7 #pragma once
8
9 #include <string>
10 #include <vector>
11
12 #include "base/base_export.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15
16 // TODO(erg): The contents of this file should be in a namespace. This would
17 // require touching >100 files in chrome/ though.
18
19 // A generic interface to memory. This object is reference counted because one
20 // of its two subclasses own the data they carry, and we need to have
21 // heterogeneous containers of these two types of memory.
22 class BASE_EXPORT RefCountedMemory
23     : public base::RefCountedThreadSafe<RefCountedMemory> {
24  public:
25   // Retrieves a pointer to the beginning of the data we point to. If the data
26   // is empty, this will return NULL.
27   virtual const unsigned char* front() const = 0;
28
29   // Size of the memory pointed to.
30   virtual size_t size() const = 0;
31
32  protected:
33   friend class base::RefCountedThreadSafe<RefCountedMemory>;
34   RefCountedMemory();
35   virtual ~RefCountedMemory();
36 };
37
38 // An implementation of RefCountedMemory, where the ref counting does not
39 // matter.
40 class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory {
41  public:
42   RefCountedStaticMemory()
43       : data_(NULL), length_(0) {}
44   RefCountedStaticMemory(const unsigned char* data, size_t length)
45       : data_(length ? data : NULL), length_(length) {}
46
47   // Overridden from RefCountedMemory:
48   virtual const unsigned char* front() const OVERRIDE;
49   virtual size_t size() const OVERRIDE;
50
51  private:
52   const unsigned char* data_;
53   size_t length_;
54
55   DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
56 };
57
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();
63
64   // Constructs a RefCountedBytes object by _copying_ from |initializer|.
65   RefCountedBytes(const std::vector<unsigned char>& initializer);
66
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);
71
72   // Overridden from RefCountedMemory:
73   virtual const unsigned char* front() const OVERRIDE;
74   virtual size_t size() const OVERRIDE;
75
76   const std::vector<unsigned char>& data() const { return data_; }
77   std::vector<unsigned char>& data() { return data_; }
78
79  private:
80   friend class base::RefCountedThreadSafe<RefCountedBytes>;
81   virtual ~RefCountedBytes();
82
83   std::vector<unsigned char> data_;
84
85   DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
86 };
87
88 namespace base {
89
90 // An implementation of RefCountedMemory, where the bytes are stored in an STL
91 // string. Use this if your data naturally arrives in that format.
92 class BASE_EXPORT RefCountedString : public RefCountedMemory {
93  public:
94   RefCountedString();
95
96   // Constructs a RefCountedString object by performing a swap. (To non
97   // destructively build a RefCountedString, use the default constructor and
98   // copy into object->data()).
99   static RefCountedString* TakeString(std::string* to_destroy);
100
101   // Overridden from RefCountedMemory:
102   virtual const unsigned char* front() const OVERRIDE;
103   virtual size_t size() const OVERRIDE;
104
105   const std::string& data() const { return data_; }
106   std::string& data() { return data_; }
107
108  private:
109   friend class base::RefCountedThreadSafe<RefCountedString>;
110   virtual ~RefCountedString();
111
112   std::string data_;
113
114   DISALLOW_COPY_AND_ASSIGN(RefCountedString);
115 };
116
117 }  // namespace base
118
119 #endif  // BASE_MEMORY_REF_COUNTED_MEMORY_H_