Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Unified Diff: include/core/SkTemplates.h

Issue 1153123003: refactor bitmapshader to use a controller (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: include/core/SkTemplates.h
diff --git a/include/core/SkTemplates.h b/include/core/SkTemplates.h
index 0488a29651ad044df90d3822b166e0f7c3f5f954..2ddc5be22525b893a3818059eb450cbb96bfd692 100644
--- a/include/core/SkTemplates.h
+++ b/include/core/SkTemplates.h
@@ -480,14 +480,47 @@ private:
};
};
+//////////////////////////////////////////////////////////////////////////////////////////////////
+
+/**
+ * Pass the object and the storage that was offered during SkInPlaceNewCheck, and this will
+ * safely destroy (and free if it was dynamically allocated) the object.
+ */
+template <typename T> void SkInPlaceDeleteCheck(T* obj, void* storage) {
+ if (storage == obj) {
+ obj->~T();
+ } else {
+ SkDELETE(obj);
+ }
+}
+
+/**
+ * Allocates T, using storage if it is large enough, and allocating on the heap (via new) if
+ * storage is not large enough.
+ *
+ * obj = SkInPlaceNewCheck<Type>(storage, size);
+ * ...
+ * SkInPlaceDeleteCheck(obj, storage);
+ */
+template <typename T> T* SkInPlaceNewCheck(void* storage, size_t size) {
+ return (sizeof(T) <= size) ? new (storage) T : SkNEW(T);
+}
+
+template <typename T, typename A1, typename A2, typename A3>
+T* SkInPlaceNewCheck(void* storage, size_t size, const A1& a1, const A2& a2, const A3& a3) {
+ return (sizeof(T) <= size) ? new (storage) T(a1, a2, a3) : SkNEW_ARGS(T, (a1, a2, a3));
+}
+
/**
* Reserves memory that is aligned on double and pointer boundaries.
* Hopefully this is sufficient for all practical purposes.
*/
template <size_t N> class SkAlignedSStorage : SkNoncopyable {
public:
+ size_t size() const { return N; }
void* get() { return fData; }
const void* get() const { return fData; }
+
private:
union {
void* fPtr;
« no previous file with comments | « gyp/core.gypi ('k') | src/core/SkBitmapController.h » ('j') | src/core/SkBitmapProcState.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698