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