OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkData_DEFINED | 8 #ifndef SkData_DEFINED |
9 #define SkData_DEFINED | 9 #define SkData_DEFINED |
10 | 10 |
11 #include <stdio.h> | 11 #include <stdio.h> |
12 | 12 |
13 #include "SkRefCnt.h" | 13 #include "SkRefCnt.h" |
14 | 14 |
15 class SkStream; | 15 class SkStream; |
16 | 16 |
| 17 #define SK_SUPPORT_LEGACY_DATA_FACTORIES |
| 18 |
17 /** | 19 /** |
18 * SkData holds an immutable data buffer. Not only is the data immutable, | 20 * SkData holds an immutable data buffer. Not only is the data immutable, |
19 * but the actual ptr that is returned (by data() or bytes()) is guaranteed | 21 * but the actual ptr that is returned (by data() or bytes()) is guaranteed |
20 * to always be the same for the life of this instance. | 22 * to always be the same for the life of this instance. |
21 */ | 23 */ |
22 class SK_API SkData : public SkRefCnt { | 24 class SK_API SkData : public SkRefCnt { |
23 public: | 25 public: |
24 /** | 26 /** |
25 * Returns the number of bytes stored. | 27 * Returns the number of bytes stored. |
26 */ | 28 */ |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 * length to the size of the data. If buffer is NULL, it is ignored, and | 62 * length to the size of the data. If buffer is NULL, it is ignored, and |
61 * only the computed number of bytes is returned. | 63 * only the computed number of bytes is returned. |
62 */ | 64 */ |
63 size_t copyRange(size_t offset, size_t length, void* buffer) const; | 65 size_t copyRange(size_t offset, size_t length, void* buffer) const; |
64 | 66 |
65 /** | 67 /** |
66 * Returns true if these two objects have the same length and contents, | 68 * Returns true if these two objects have the same length and contents, |
67 * effectively returning 0 == memcmp(...) | 69 * effectively returning 0 == memcmp(...) |
68 */ | 70 */ |
69 bool equals(const SkData* other) const; | 71 bool equals(const SkData* other) const; |
| 72 bool equals(sk_sp<const SkData>& other) const { return this->equals(other.ge
t()); } |
70 | 73 |
71 /** | 74 /** |
72 * Function that, if provided, will be called when the SkData goes out | 75 * Function that, if provided, will be called when the SkData goes out |
73 * of scope, allowing for custom allocation/freeing of the data's contents. | 76 * of scope, allowing for custom allocation/freeing of the data's contents. |
74 */ | 77 */ |
75 typedef void (*ReleaseProc)(const void* ptr, void* context); | 78 typedef void (*ReleaseProc)(const void* ptr, void* context); |
76 | 79 |
77 /** | 80 /** |
78 * Create a new dataref by copying the specified data | 81 * Create a new dataref by copying the specified data |
79 */ | 82 */ |
80 static SkData* NewWithCopy(const void* data, size_t length); | 83 static sk_sp<SkData> MakeWithCopy(const void* data, size_t length); |
81 | 84 |
| 85 |
82 /** | 86 /** |
83 * Create a new data with uninitialized contents. The caller should call wr
itable_data() | 87 * Create a new data with uninitialized contents. The caller should call wr
itable_data() |
84 * to write into the buffer, but this must be done before another ref() is
made. | 88 * to write into the buffer, but this must be done before another ref() is
made. |
85 */ | 89 */ |
86 static SkData* NewUninitialized(size_t length); | 90 static sk_sp<SkData> MakeUninitialized(size_t length); |
87 | 91 |
88 /** | 92 /** |
89 * Create a new dataref by copying the specified c-string | 93 * Create a new dataref by copying the specified c-string |
90 * (a null-terminated array of bytes). The returned SkData will have size() | 94 * (a null-terminated array of bytes). The returned SkData will have size() |
91 * equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same | 95 * equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same |
92 * as "". | 96 * as "". |
93 */ | 97 */ |
94 static SkData* NewWithCString(const char cstr[]); | 98 static sk_sp<SkData> MakeWithCString(const char cstr[]); |
95 | 99 |
96 /** | 100 /** |
97 * Create a new dataref, taking the ptr as is, and using the | 101 * Create a new dataref, taking the ptr as is, and using the |
98 * releaseproc to free it. The proc may be NULL. | 102 * releaseproc to free it. The proc may be NULL. |
99 */ | 103 */ |
100 static SkData* NewWithProc(const void* ptr, size_t length, ReleaseProc proc,
void* context); | 104 static sk_sp<SkData> MakeWithProc(const void* ptr, size_t length, ReleasePro
c proc, void* ctx); |
101 | 105 |
102 /** | 106 /** |
103 * Call this when the data parameter is already const and will outlive the
lifetime of the | 107 * Call this when the data parameter is already const and will outlive the
lifetime of the |
104 * SkData. Suitable for with const globals. | 108 * SkData. Suitable for with const globals. |
105 */ | 109 */ |
106 static SkData* NewWithoutCopy(const void* data, size_t length) { | 110 static sk_sp<SkData> MakeWithoutCopy(const void* data, size_t length) { |
107 return NewWithProc(data, length, DummyReleaseProc, NULL); | 111 return MakeWithProc(data, length, DummyReleaseProc, nullptr); |
108 } | 112 } |
109 | 113 |
110 /** | 114 /** |
111 * Create a new dataref from a pointer allocated by malloc. The Data object | 115 * Create a new dataref from a pointer allocated by malloc. The Data object |
112 * takes ownership of that allocation, and will handling calling sk_free. | 116 * takes ownership of that allocation, and will handling calling sk_free. |
113 */ | 117 */ |
114 static SkData* NewFromMalloc(const void* data, size_t length); | 118 static sk_sp<SkData> MakeFromMalloc(const void* data, size_t length); |
115 | 119 |
116 /** | 120 /** |
117 * Create a new dataref the file with the specified path. | 121 * Create a new dataref the file with the specified path. |
118 * If the file cannot be opened, this returns NULL. | 122 * If the file cannot be opened, this returns NULL. |
119 */ | 123 */ |
120 static SkData* NewFromFileName(const char path[]); | 124 static sk_sp<SkData> MakeFromFileName(const char path[]); |
121 | 125 |
122 /** | 126 /** |
123 * Create a new dataref from a stdio FILE. | 127 * Create a new dataref from a stdio FILE. |
124 * This does not take ownership of the FILE, nor close it. | 128 * This does not take ownership of the FILE, nor close it. |
125 * The caller is free to close the FILE at its convenience. | 129 * The caller is free to close the FILE at its convenience. |
126 * The FILE must be open for reading only. | 130 * The FILE must be open for reading only. |
127 * Returns NULL on failure. | 131 * Returns NULL on failure. |
128 */ | 132 */ |
129 static SkData* NewFromFILE(FILE* f); | 133 static sk_sp<SkData> MakeFromFILE(FILE* f); |
130 | 134 |
131 /** | 135 /** |
132 * Create a new dataref from a file descriptor. | 136 * Create a new dataref from a file descriptor. |
133 * This does not take ownership of the file descriptor, nor close it. | 137 * This does not take ownership of the file descriptor, nor close it. |
134 * The caller is free to close the file descriptor at its convenience. | 138 * The caller is free to close the file descriptor at its convenience. |
135 * The file descriptor must be open for reading only. | 139 * The file descriptor must be open for reading only. |
136 * Returns NULL on failure. | 140 * Returns NULL on failure. |
137 */ | 141 */ |
138 static SkData* NewFromFD(int fd); | 142 static sk_sp<SkData> MakeFromFD(int fd); |
139 | 143 |
140 /** | 144 /** |
141 * Attempt to read size bytes into a SkData. If the read succeeds, return t
he data, | 145 * Attempt to read size bytes into a SkData. If the read succeeds, return t
he data, |
142 * else return NULL. Either way the stream's cursor may have been changed a
s a result | 146 * else return NULL. Either way the stream's cursor may have been changed a
s a result |
143 * of calling read(). | 147 * of calling read(). |
144 */ | 148 */ |
145 static SkData* NewFromStream(SkStream*, size_t size); | 149 static sk_sp<SkData> MakeFromStream(SkStream*, size_t size); |
146 | 150 |
147 /** | 151 /** |
148 * Create a new dataref using a subset of the data in the specified | 152 * Create a new dataref using a subset of the data in the specified |
149 * src dataref. | 153 * src dataref. |
150 */ | 154 */ |
151 static SkData* NewSubset(const SkData* src, size_t offset, size_t length); | 155 static sk_sp<SkData> MakeSubset(const SkData* src, size_t offset, size_t len
gth); |
152 | 156 |
153 /** | 157 /** |
154 * Returns a new empty dataref (or a reference to a shared empty dataref). | 158 * Returns a new empty dataref (or a reference to a shared empty dataref). |
155 * New or shared, the caller must see that unref() is eventually called. | 159 * New or shared, the caller must see that unref() is eventually called. |
156 */ | 160 */ |
157 static SkData* NewEmpty(); | 161 static sk_sp<SkData> MakeEmpty(); |
| 162 |
| 163 #ifdef SK_SUPPORT_LEGACY_DATA_FACTORIES |
| 164 static SkData* NewWithCopy(const void* data, size_t length) { |
| 165 return MakeWithCopy(data, length).release(); |
| 166 } |
| 167 static SkData* NewUninitialized(size_t length) { |
| 168 return MakeUninitialized(length).release(); |
| 169 } |
| 170 static SkData* NewWithCString(const char cstr[]) { |
| 171 return MakeWithCString(cstr).release(); |
| 172 } |
| 173 static SkData* NewWithProc(const void* ptr, size_t length, ReleaseProc proc,
void* context) { |
| 174 return MakeWithProc(ptr, length, proc, context).release(); |
| 175 } |
| 176 static SkData* NewWithoutCopy(const void* data, size_t length) { |
| 177 return MakeWithoutCopy(data, length).release(); |
| 178 } |
| 179 static SkData* NewFromMalloc(const void* data, size_t length) { |
| 180 return MakeFromMalloc(data, length).release(); |
| 181 } |
| 182 static SkData* NewFromFileName(const char path[]) { return MakeFromFileName(
path).release(); } |
| 183 static SkData* NewFromFILE(FILE* f) { return MakeFromFILE(f).release(); } |
| 184 static SkData* NewFromFD(int fd) { return MakeFromFD(fd).release(); } |
| 185 static SkData* NewFromStream(SkStream* stream, size_t size) { |
| 186 return MakeFromStream(stream, size).release(); |
| 187 } |
| 188 static SkData* NewSubset(const SkData* src, size_t offset, size_t length) { |
| 189 return MakeSubset(src, offset, length).release(); |
| 190 } |
| 191 static SkData* NewEmpty() { return MakeEmpty().release(); } |
| 192 #endif |
158 | 193 |
159 private: | 194 private: |
160 ReleaseProc fReleaseProc; | 195 ReleaseProc fReleaseProc; |
161 void* fReleaseProcContext; | 196 void* fReleaseProcContext; |
162 void* fPtr; | 197 void* fPtr; |
163 size_t fSize; | 198 size_t fSize; |
164 | 199 |
165 SkData(const void* ptr, size_t size, ReleaseProc, void* context); | 200 SkData(const void* ptr, size_t size, ReleaseProc, void* context); |
166 explicit SkData(size_t size); // inplace new/delete | 201 explicit SkData(size_t size); // inplace new/delete |
167 virtual ~SkData(); | 202 virtual ~SkData(); |
168 | 203 |
169 | 204 |
170 // Objects of this type are sometimes created in a custom fashion using sk_m
alloc_throw and | 205 // Objects of this type are sometimes created in a custom fashion using sk_m
alloc_throw and |
171 // therefore must be sk_freed. We overload new to also call sk_malloc_throw
so that memory | 206 // therefore must be sk_freed. We overload new to also call sk_malloc_throw
so that memory |
172 // can be unconditionally released using sk_free in an overloaded delete. Ov
erloading regular | 207 // can be unconditionally released using sk_free in an overloaded delete. Ov
erloading regular |
173 // new means we must also overload placement new. | 208 // new means we must also overload placement new. |
174 void* operator new(size_t size) { return sk_malloc_throw(size); } | 209 void* operator new(size_t size) { return sk_malloc_throw(size); } |
175 void* operator new(size_t, void* p) { return p; } | 210 void* operator new(size_t, void* p) { return p; } |
176 void operator delete(void* p) { sk_free(p); } | 211 void operator delete(void* p) { sk_free(p); } |
177 | 212 |
178 // Called the first time someone calls NewEmpty to initialize the singleton. | 213 // Called the first time someone calls NewEmpty to initialize the singleton. |
179 friend SkData* sk_new_empty_data(); | 214 friend SkData* sk_new_empty_data(); |
180 | 215 |
181 // shared internal factory | 216 // shared internal factory |
182 static SkData* PrivateNewWithCopy(const void* srcOrNull, size_t length); | 217 static sk_sp<SkData> PrivateNewWithCopy(const void* srcOrNull, size_t length
); |
183 | 218 |
184 static void DummyReleaseProc(const void*, void*) {} | 219 static void DummyReleaseProc(const void*, void*) {} |
185 | 220 |
186 typedef SkRefCnt INHERITED; | 221 typedef SkRefCnt INHERITED; |
187 }; | 222 }; |
188 | 223 |
| 224 #ifdef SK_SUPPORT_LEGACY_DATA_FACTORIES |
189 /** Typedef of SkAutoTUnref<SkData> for automatically unref-ing a SkData. */ | 225 /** Typedef of SkAutoTUnref<SkData> for automatically unref-ing a SkData. */ |
190 typedef SkAutoTUnref<SkData> SkAutoDataUnref; | 226 typedef SkAutoTUnref<SkData> SkAutoDataUnref; |
| 227 #endif |
191 | 228 |
192 #endif | 229 #endif |
OLD | NEW |