2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
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.
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.
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.
21 #ifndef WTF_VectorTraits_h
22 #define WTF_VectorTraits_h
24 #include "wtf/OwnPtr.h"
25 #include "wtf/RefPtr.h"
26 #include "wtf/TypeTraits.h"
36 struct VectorTraitsBase
38 static const bool needsDestruction
= !IsTriviallyDestructible
<T
>::value
;
40 static const bool canInitializeWithMemset
= IsTriviallyDefaultConstructible
<T
>::value
;
41 // true iff memset(slot, 0, size) constructs an unused slot value that is valid for
42 // Oilpan to trace and if the value needs destruction, its destructor can be invoked
43 // over. The zero'ed value representing an unused slot in the vector's backing storage;
44 // it does not have to be equal to what its constructor(s) would create, only be
45 // valid for those two uses.
46 static const bool canClearUnusedSlotsWithMemset
= IsTriviallyDefaultConstructible
<T
>::value
;
48 static const bool canMoveWithMemcpy
= IsTriviallyMoveAssignable
<T
>::value
;
49 static const bool canCopyWithMemcpy
= IsTriviallyCopyAssignable
<T
>::value
;
50 static const bool canFillWithMemset
= IsTriviallyDefaultConstructible
<T
>::value
&& (sizeof(T
) == sizeof(char));
51 static const bool canCompareWithMemcmp
= IsScalar
<T
>::value
; // Types without padding.
52 template<typename U
= void>
53 struct NeedsTracingLazily
{
54 static const bool value
= NeedsTracing
<T
>::value
;
56 static const WeakHandlingFlag weakHandlingFlag
= NoWeakHandlingInCollections
; // We don't support weak handling in vectors.
60 struct VectorTraits
: VectorTraitsBase
<T
> { };
62 // Classes marked with SimpleVectorTraits will use memmov, memcpy, memcmp
63 // instead of constructors, copy operators, etc for initialization, move
66 struct SimpleClassVectorTraits
: VectorTraitsBase
<T
>
68 static const bool canInitializeWithMemset
= true;
69 static const bool canClearUnusedSlotsWithMemset
= true;
70 static const bool canMoveWithMemcpy
= true;
71 static const bool canCompareWithMemcmp
= true;
74 // We know OwnPtr and RefPtr are simple enough that initializing to 0 and
75 // moving with memcpy (and then not destructing the original) will totally
78 struct VectorTraits
<RefPtr
<P
>> : SimpleClassVectorTraits
<RefPtr
<P
>> { };
81 struct VectorTraits
<OwnPtr
<P
>> : SimpleClassVectorTraits
<OwnPtr
<P
>> {
82 // OwnPtr -> PassOwnPtr has a very particular structure that
83 // tricks the normal type traits into thinking that the class
84 // is "trivially copyable".
85 static const bool canCopyWithMemcpy
= false;
87 static_assert(VectorTraits
<RefPtr
<int>>::canInitializeWithMemset
, "inefficient RefPtr Vector");
88 static_assert(VectorTraits
<RefPtr
<int>>::canMoveWithMemcpy
, "inefficient RefPtr Vector");
89 static_assert(VectorTraits
<RefPtr
<int>>::canCompareWithMemcmp
, "inefficient RefPtr Vector");
90 static_assert(VectorTraits
<OwnPtr
<int>>::canInitializeWithMemset
, "inefficient OwnPtr Vector");
91 static_assert(VectorTraits
<OwnPtr
<int>>::canMoveWithMemcpy
, "inefficient OwnPtr Vector");
92 static_assert(VectorTraits
<OwnPtr
<int>>::canCompareWithMemcmp
, "inefficient OwnPtr Vector");
94 template<typename First
, typename Second
>
95 struct VectorTraits
<pair
<First
, Second
>>
97 typedef VectorTraits
<First
> FirstTraits
;
98 typedef VectorTraits
<Second
> SecondTraits
;
100 static const bool needsDestruction
= FirstTraits::needsDestruction
|| SecondTraits::needsDestruction
;
101 static const bool canInitializeWithMemset
= FirstTraits::canInitializeWithMemset
&& SecondTraits::canInitializeWithMemset
;
102 static const bool canMoveWithMemcpy
= FirstTraits::canMoveWithMemcpy
&& SecondTraits::canMoveWithMemcpy
;
103 static const bool canCopyWithMemcpy
= FirstTraits::canCopyWithMemcpy
&& SecondTraits::canCopyWithMemcpy
;
104 static const bool canFillWithMemset
= false;
105 static const bool canCompareWithMemcmp
= FirstTraits::canCompareWithMemcmp
&& SecondTraits::canCompareWithMemcmp
;
106 static const bool canClearUnusedSlotsWithMemset
= FirstTraits::canClearUnusedSlotsWithMemset
&& SecondTraits::canClearUnusedSlotsWithMemset
;
107 template <typename U
= void>
108 struct NeedsTracingLazily
{
109 static const bool value
= ShouldBeTraced
<FirstTraits
>::value
|| ShouldBeTraced
<SecondTraits
>::value
;
111 static const WeakHandlingFlag weakHandlingFlag
= NoWeakHandlingInCollections
; // We don't support weak handling in vectors.
116 #define WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(ClassName) \
118 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value || !IsTriviallyMoveAssignable<ClassName>::value || !IsScalar<ClassName>::value, "macro not needed"); \
120 struct VectorTraits<ClassName> : SimpleClassVectorTraits<ClassName> { }; \
123 #define WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(ClassName) \
125 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value || !IsTriviallyMoveAssignable<ClassName>::value, "macro not needed"); \
127 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> \
129 static const bool canInitializeWithMemset = true; \
130 static const bool canClearUnusedSlotsWithMemset = true; \
131 static const bool canMoveWithMemcpy = true; \
135 #define WTF_ALLOW_INIT_WITH_MEM_FUNCTIONS(ClassName) \
137 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value, "macro not needed"); \
139 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> \
141 static const bool canInitializeWithMemset = true; \
142 static const bool canClearUnusedSlotsWithMemset = true; \
146 #define WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(ClassName) \
148 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value, "macro not needed"); \
150 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> \
152 static const bool canClearUnusedSlotsWithMemset = true; \
156 using WTF::VectorTraits
;
157 using WTF::SimpleClassVectorTraits
;
159 #endif // WTF_VectorTraits_h