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