1 2 /* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #ifndef SkTemplatesPriv_DEFINED 11 #define SkTemplatesPriv_DEFINED 12 13 #include "SkTemplates.h" 14 15 //////////////////////////////////////////////////////////////////////////////// 16 17 #ifdef SK_BUILD_FOR_WIN32 18 #define SK_PLACEMENT_NEW(result, classname, storage, storageSize) \ 19 result = SkNEW(classname) 20 21 #define SK_PLACEMENT_NEW_ARGS(result, classname, storage, storageSize, args) \ 22 result = SkNEW_ARGS(classname, args) 23 #else 24 #include <new> 25 #define SK_PLACEMENT_NEW(result, classname, storage, storagesize) \ 26 do { \ 27 if (storagesize) \ 28 { \ 29 SkASSERT(storageSize >= sizeof(classname)); \ 30 result = new(storage) classname; \ 31 } \ 32 else \ 33 result = SkNEW(classname); \ 34 } while (0) 35 36 #define SK_PLACEMENT_NEW_ARGS(result, classname, storage, storagesize, args) \ 37 do { \ 38 if (storagesize) \ 39 { \ 40 SkASSERT(storageSize >= sizeof(classname)); \ 41 result = new(storage) classname args; \ 42 } \ 43 else \ 44 result = SkNEW_ARGS(classname, args); \ 45 } while (0) 46 #endif 47 48 //////////////////////////////////////////////////////////////////////////////// 49 50 template <class T> class SkAutoTPlacementDelete { 51 public: SkAutoTPlacementDelete(T * obj,void * storage)52 SkAutoTPlacementDelete(T* obj, void* storage) : fObj(obj), fStorage(storage) 53 { 54 } ~SkAutoTPlacementDelete()55 ~SkAutoTPlacementDelete() 56 { 57 if (fObj) 58 { 59 if (fObj == fStorage) 60 fObj->~T(); 61 else 62 delete fObj; 63 } 64 } detach()65 T* detach() 66 { 67 T* obj = fObj; 68 fObj = NULL; 69 return obj; 70 } 71 private: 72 T* fObj; 73 void* fStorage; 74 }; 75 76 #endif 77