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 #include <memory>
     29 
     30 using std::pair;
     31 
     32 namespace WTF {
     33 
     34     class AtomicString;
     35 
     36     template<bool isPod, typename T>
     37     struct VectorTraitsBase;
     38 
     39     template<typename T>
     40     struct VectorTraitsBase<false, T>
     41     {
     42         static const bool needsDestruction = true;
     43         static const bool needsInitialization = true;
     44         static const bool canInitializeWithMemset = false;
     45         static const bool canMoveWithMemcpy = false;
     46         static const bool canCopyWithMemcpy = false;
     47         static const bool canFillWithMemset = false;
     48         static const bool canCompareWithMemcmp = false;
     49     };
     50 
     51     template<typename T>
     52     struct VectorTraitsBase<true, T>
     53     {
     54         static const bool needsDestruction = false;
     55         static const bool needsInitialization = false;
     56         static const bool canInitializeWithMemset = false;
     57         static const bool canMoveWithMemcpy = true;
     58         static const bool canCopyWithMemcpy = true;
     59         static const bool canFillWithMemset = sizeof(T) == sizeof(char);
     60         static const bool canCompareWithMemcmp = true;
     61     };
     62 
     63     template<typename T>
     64     struct VectorTraits : VectorTraitsBase<IsPod<T>::value, T> { };
     65 
     66     struct SimpleClassVectorTraits : VectorTraitsBase<false, void>
     67     {
     68         static const bool canInitializeWithMemset = true;
     69         static const bool canMoveWithMemcpy = true;
     70         static const bool canCompareWithMemcmp = true;
     71     };
     72 
     73     // we know OwnPtr and RefPtr are simple enough that initializing to 0 and moving with memcpy
     74     // (and then not destructing the original) will totally work
     75     template<typename P>
     76     struct VectorTraits<RefPtr<P> > : SimpleClassVectorTraits { };
     77 
     78     template<typename P>
     79     struct VectorTraits<OwnPtr<P> > : SimpleClassVectorTraits { };
     80 
     81     template<>
     82     struct VectorTraits<AtomicString> : SimpleClassVectorTraits { };
     83 
     84     template<typename First, typename Second>
     85     struct VectorTraits<pair<First, Second> >
     86     {
     87         typedef VectorTraits<First> FirstTraits;
     88         typedef VectorTraits<Second> SecondTraits;
     89 
     90         static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;
     91         static const bool needsInitialization = FirstTraits::needsInitialization || SecondTraits::needsInitialization;
     92         static const bool canInitializeWithMemset = FirstTraits::canInitializeWithMemset && SecondTraits::canInitializeWithMemset;
     93         static const bool canMoveWithMemcpy = FirstTraits::canMoveWithMemcpy && SecondTraits::canMoveWithMemcpy;
     94         static const bool canCopyWithMemcpy = FirstTraits::canCopyWithMemcpy && SecondTraits::canCopyWithMemcpy;
     95         static const bool canFillWithMemset = false;
     96         static const bool canCompareWithMemcmp = FirstTraits::canCompareWithMemcmp && SecondTraits::canCompareWithMemcmp;
     97     };
     98 
     99 } // namespace WTF
    100 
    101 using WTF::VectorTraits;
    102 using WTF::SimpleClassVectorTraits;
    103 
    104 #endif // WTF_VectorTraits_h
    105