Home | History | Annotate | Download | only in wtf
      1 /*
      2  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  *
     19  */
     20 
     21 #ifndef WTF_VectorTraits_h
     22 #define WTF_VectorTraits_h
     23 
     24 #include "wtf/OwnPtr.h"
     25 #include "wtf/RefPtr.h"
     26 #include "wtf/TypeTraits.h"
     27 #include <utility>
     28 
     29 using std::pair;
     30 
     31 namespace WTF {
     32 
     33     class AtomicString;
     34 
     35     template<typename T>
     36     struct VectorTraitsBase
     37     {
     38         static const bool needsDestruction = !IsPod<T>::value;
     39         static const bool canInitializeWithMemset = IsPod<T>::value;
     40         static const bool canMoveWithMemcpy = IsPod<T>::value;
     41         static const bool canCopyWithMemcpy = IsPod<T>::value;
     42         static const bool canFillWithMemset = IsPod<T>::value && (sizeof(T) == sizeof(char));
     43         static const bool canCompareWithMemcmp = IsPod<T>::value;
     44         template<typename U = void>
     45         struct NeedsTracingLazily {
     46             static const bool value = NeedsTracing<T>::value;
     47         };
     48         static const WeakHandlingFlag weakHandlingFlag = NoWeakHandlingInCollections; // We don't support weak handling in vectors.
     49     };
     50 
     51     template<typename T>
     52     struct VectorTraits : VectorTraitsBase<T> { };
     53 
     54     // Classes marked with SimpleVectorTraits will use memmov, memcpy, memcmp
     55     // instead of constructors, copy operators, etc for initialization, move
     56     // and comparison.
     57     template<typename T>
     58     struct SimpleClassVectorTraits : VectorTraitsBase<T>
     59     {
     60         static const bool canInitializeWithMemset = true;
     61         static const bool canMoveWithMemcpy = true;
     62         static const bool canCompareWithMemcmp = true;
     63     };
     64 
     65     // We know OwnPtr and RefPtr are simple enough that initializing to 0 and
     66     // moving with memcpy (and then not destructing the original) will totally
     67     // work.
     68     template<typename P>
     69     struct VectorTraits<RefPtr<P> > : SimpleClassVectorTraits<RefPtr<P> > { };
     70 
     71     template<typename P>
     72     struct VectorTraits<OwnPtr<P> > : SimpleClassVectorTraits<OwnPtr<P> > { };
     73 
     74     template<typename First, typename Second>
     75     struct VectorTraits<pair<First, Second> >
     76     {
     77         typedef VectorTraits<First> FirstTraits;
     78         typedef VectorTraits<Second> SecondTraits;
     79 
     80         static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;
     81         static const bool canInitializeWithMemset = FirstTraits::canInitializeWithMemset && SecondTraits::canInitializeWithMemset;
     82         static const bool canMoveWithMemcpy = FirstTraits::canMoveWithMemcpy && SecondTraits::canMoveWithMemcpy;
     83         static const bool canCopyWithMemcpy = FirstTraits::canCopyWithMemcpy && SecondTraits::canCopyWithMemcpy;
     84         static const bool canFillWithMemset = false;
     85         static const bool canCompareWithMemcmp = FirstTraits::canCompareWithMemcmp && SecondTraits::canCompareWithMemcmp;
     86         template <typename U = void>
     87         struct NeedsTracingLazily {
     88             static const bool value = ShouldBeTraced<FirstTraits>::value || ShouldBeTraced<SecondTraits>::value;
     89         };
     90         static const WeakHandlingFlag weakHandlingFlag = NoWeakHandlingInCollections; // We don't support weak handling in vectors.
     91     };
     92 
     93 } // namespace WTF
     94 
     95 #define WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(ClassName) \
     96 namespace WTF { \
     97     template<> \
     98     struct VectorTraits<ClassName> : SimpleClassVectorTraits<ClassName> { }; \
     99 }
    100 
    101 #define WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(ClassName) \
    102 namespace WTF { \
    103     template<> \
    104     struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> \
    105     { \
    106         static const bool canInitializeWithMemset = true; \
    107         static const bool canMoveWithMemcpy = true; \
    108     }; \
    109 }
    110 
    111 #define WTF_ALLOW_INIT_WITH_MEM_FUNCTIONS(ClassName) \
    112 namespace WTF { \
    113     template<> \
    114     struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> \
    115     { \
    116         static const bool canInitializeWithMemset = true; \
    117     }; \
    118 }
    119 
    120 using WTF::VectorTraits;
    121 using WTF::SimpleClassVectorTraits;
    122 
    123 #endif // WTF_VectorTraits_h
    124