• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "RefPtr.h"
25 #include <utility>
26 #include <memory>
27 
28 using std::pair;
29 
30 namespace WTF {
31 
32     template <typename T> struct IsPod           { static const bool value = false; };
33     template <> struct IsPod<bool>               { static const bool value = true; };
34     template <> struct IsPod<char>               { static const bool value = true; };
35     template <> struct IsPod<signed char>        { static const bool value = true; };
36     template <> struct IsPod<unsigned char>      { static const bool value = true; };
37     template <> struct IsPod<short>              { static const bool value = true; };
38     template <> struct IsPod<unsigned short>     { static const bool value = true; };
39     template <> struct IsPod<int>                { static const bool value = true; };
40     template <> struct IsPod<unsigned int>       { static const bool value = true; };
41     template <> struct IsPod<long>               { static const bool value = true; };
42     template <> struct IsPod<unsigned long>      { static const bool value = true; };
43     template <> struct IsPod<long long>          { static const bool value = true; };
44     template <> struct IsPod<unsigned long long> { static const bool value = true; };
45     template <> struct IsPod<float>              { static const bool value = true; };
46     template <> struct IsPod<double>             { static const bool value = true; };
47     template <> struct IsPod<long double>        { static const bool value = true; };
48     template <typename P> struct IsPod<P *>      { static const bool value = true; };
49 
50     template<bool isPod, typename T>
51     class VectorTraitsBase;
52 
53     template<typename T>
54     struct VectorTraitsBase<false, T>
55     {
56         static const bool needsDestruction = true;
57         static const bool needsInitialization = true;
58         static const bool canInitializeWithMemset = false;
59         static const bool canMoveWithMemcpy = false;
60         static const bool canCopyWithMemcpy = false;
61         static const bool canFillWithMemset = false;
62         static const bool canCompareWithMemcmp = false;
63     };
64 
65     template<typename T>
66     struct VectorTraitsBase<true, T>
67     {
68         static const bool needsDestruction = false;
69         static const bool needsInitialization = false;
70         static const bool canInitializeWithMemset = false;
71         static const bool canMoveWithMemcpy = true;
72         static const bool canCopyWithMemcpy = true;
73         static const bool canFillWithMemset = sizeof(T) == sizeof(char);
74         static const bool canCompareWithMemcmp = true;
75     };
76 
77     template<typename T>
78     struct VectorTraits : VectorTraitsBase<IsPod<T>::value, T> { };
79 
80     struct SimpleClassVectorTraits
81     {
82         static const bool needsDestruction = true;
83         static const bool needsInitialization = true;
84         static const bool canInitializeWithMemset = true;
85         static const bool canMoveWithMemcpy = true;
86         static const bool canCopyWithMemcpy = false;
87         static const bool canFillWithMemset = false;
88         static const bool canCompareWithMemcmp = true;
89     };
90 
91     // we know RefPtr is simple enough that initializing to 0 and moving with memcpy
92     // (and then not destructing the original) will totally work
93     template<typename P>
94     struct VectorTraits<RefPtr<P> > : SimpleClassVectorTraits { };
95 
96     template<typename P>
97     struct VectorTraits<std::auto_ptr<P> > : SimpleClassVectorTraits { };
98 
99     template<typename First, typename Second>
100     struct VectorTraits<pair<First, Second> >
101     {
102         typedef VectorTraits<First> FirstTraits;
103         typedef VectorTraits<Second> SecondTraits;
104 
105         static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;
106         static const bool needsInitialization = FirstTraits::needsInitialization || SecondTraits::needsInitialization;
107         static const bool canInitializeWithMemset = FirstTraits::canInitializeWithMemset && SecondTraits::canInitializeWithMemset;
108         static const bool canMoveWithMemcpy = FirstTraits::canMoveWithMemcpy && SecondTraits::canMoveWithMemcpy;
109         static const bool canCopyWithMemcpy = FirstTraits::canCopyWithMemcpy && SecondTraits::canCopyWithMemcpy;
110         static const bool canFillWithMemset = false;
111         static const bool canCompareWithMemcmp = FirstTraits::canCompareWithMemcmp && SecondTraits::canCompareWithMemcmp;
112     };
113 
114 } // namespace WTF
115 
116 using WTF::VectorTraits;
117 using WTF::SimpleClassVectorTraits;
118 
119 #endif // WTF_VectorTraits_h
120