Home | History | Annotate | Download | only in core
      1 /* libs/graphics/sgl/SkTemplatesPriv.h
      2 **
      3 ** Copyright 2006, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 #ifndef SkTemplatesPriv_DEFINED
     19 #define SkTemplatesPriv_DEFINED
     20 
     21 #include "SkTemplates.h"
     22 
     23 ////////////////////////////////////////////////////////////////////////////////
     24 
     25 #ifdef SK_BUILD_FOR_WIN32
     26     #define SK_PLACEMENT_NEW(result, classname, storage, storageSize)   \
     27         result = SkNEW(classname)
     28 
     29     #define SK_PLACEMENT_NEW_ARGS(result, classname, storage, storageSize, args)    \
     30         result = SkNEW_ARGS(classname, args)
     31 #else
     32     #include <new>
     33     #define SK_PLACEMENT_NEW(result, classname, storage, storagesize)       \
     34     do {                                                                    \
     35         if (storagesize)                                                    \
     36         {                                                                   \
     37             SkASSERT(storageSize >= sizeof(classname));                     \
     38             result = new(storage) classname;                                \
     39         }                                                                   \
     40         else                                                                \
     41             result = SkNEW(classname);                                      \
     42     } while (0)
     43 
     44     #define SK_PLACEMENT_NEW_ARGS(result, classname, storage, storagesize, args)        \
     45     do {                                                                                \
     46         if (storagesize)                                                                \
     47         {                                                                               \
     48             SkASSERT(storageSize >= sizeof(classname));                                 \
     49             result = new(storage) classname args;                                       \
     50         }                                                                               \
     51         else                                                                            \
     52             result = SkNEW_ARGS(classname, args);                                       \
     53     } while (0)
     54 #endif
     55 
     56 ////////////////////////////////////////////////////////////////////////////////
     57 
     58 template <class T> class SkAutoTPlacementDelete {
     59 public:
     60     SkAutoTPlacementDelete(T* obj, void* storage) : fObj(obj), fStorage(storage)
     61     {
     62     }
     63     ~SkAutoTPlacementDelete()
     64     {
     65         if (fObj)
     66         {
     67             if (fObj == fStorage)
     68                 fObj->~T();
     69             else
     70                 delete fObj;
     71         }
     72     }
     73     T* detach()
     74     {
     75         T*  obj = fObj;
     76         fObj = NULL;
     77         return obj;
     78     }
     79 private:
     80     T*      fObj;
     81     void*   fStorage;
     82 };
     83 
     84 #endif
     85