Home | History | Annotate | Download | only in include
      1 /*
      2     Copyright 2010 Google Inc.
      3 
      4     Licensed under the Apache License, Version 2.0 (the "License");
      5     you may not use this file except in compliance with the License.
      6     You may obtain a copy of the License at
      7 
      8          http://www.apache.org/licenses/LICENSE-2.0
      9 
     10     Unless required by applicable law or agreed to in writing, software
     11     distributed under the License is distributed on an "AS IS" BASIS,
     12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13     See the License for the specific language governing permissions and
     14     limitations under the License.
     15  */
     16 
     17 #ifndef GrTemplates_DEFINED
     18 #define GrTemplates_DEFINED
     19 
     20 #include "GrNoncopyable.h"
     21 
     22 /**
     23  *  Use to cast a ptr to a different type, and maintain strict-aliasing
     24  */
     25 template <typename Dst, typename Src> Dst GrTCast(Src src) {
     26     union {
     27         Src src;
     28         Dst dst;
     29     } data;
     30     data.src = src;
     31     return data.dst;
     32 }
     33 
     34 /**
     35  * Reserves memory that is aligned on double and pointer boundaries.
     36  * Hopefully this is sufficient for all practical purposes.
     37  */
     38 template <size_t N> class GrAlignedSStorage : GrNoncopyable {
     39 public:
     40     void* get() { return fData; }
     41 private:
     42     union {
     43         void*   fPtr;
     44         double  fDouble;
     45         char    fData[N];
     46     };
     47 };
     48 
     49 /**
     50  * Reserves memory that is aligned on double and pointer boundaries.
     51  * Hopefully this is sufficient for all practical purposes. Otherwise,
     52  * we have to do some arcane trickery to determine alignment of non-POD
     53  * types. Lifetime of the memory is the lifetime of the object.
     54  */
     55 template <int N, typename T> class GrAlignedSTStorage : GrNoncopyable {
     56 public:
     57     /**
     58      * Returns void* because this object does not initialize the
     59      * memory. Use placement new for types that require a cons.
     60      */
     61     void* get() { return fStorage.get(); }
     62 private:
     63     GrAlignedSStorage<sizeof(T)*N> fStorage;
     64 };
     65 
     66 /**
     67  * saves value of T* in and restores in destructor
     68  * e.g.:
     69  * {
     70  *      GrAutoTPtrValueRestore<int*> autoCountRestore;
     71  *      if (useExtra) {
     72  *          autoCountRestore.save(&fCount);
     73  *          fCount += fExtraCount;
     74  *      }
     75  *      ...
     76  * }  // fCount is restored
     77  */
     78 template <typename T> class GrAutoTPtrValueRestore : public GrNoncopyable {
     79 public:
     80     GrAutoTPtrValueRestore() : fPtr(NULL), fVal() {}
     81 
     82     GrAutoTPtrValueRestore(T* ptr) {
     83         fPtr = ptr;
     84         if (NULL != ptr) {
     85             fVal = *ptr;
     86         }
     87     }
     88 
     89     ~GrAutoTPtrValueRestore() {
     90         if (NULL != fPtr) {
     91             *fPtr = fVal;
     92         }
     93     }
     94 
     95     // restores previously saved value (if any) and saves value for passed T*
     96     void save(T* ptr) {
     97         if (NULL != fPtr) {
     98             *fPtr = fVal;
     99         }
    100         fPtr = ptr;
    101         fVal = *ptr;
    102     }
    103 private:
    104     T* fPtr;
    105     T  fVal;
    106 };
    107 
    108 #endif
    109