1/*
2 * Copyright (C) 2006-2019 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#pragma once
22
23#include <wtf/Ref.h>
24#include <wtf/RefPtr.h>
25#include <utility>
26#include <memory>
27#include <type_traits>
28
29namespace WTF {
30
31 class AtomString;
32
33 template<bool isPod, typename T>
34 struct VectorTraitsBase;
35
36 template<typename T>
37 struct VectorTraitsBase<false, T>
38 {
39 static constexpr bool needsInitialization = true;
40 static constexpr bool canInitializeWithMemset = false;
41 static constexpr bool canMoveWithMemcpy = false;
42 static constexpr bool canCopyWithMemcpy = false;
43 static constexpr bool canFillWithMemset = false;
44 static constexpr bool canCompareWithMemcmp = false;
45 };
46
47 template<typename T>
48 struct VectorTraitsBase<true, T>
49 {
50 static constexpr bool needsInitialization = false;
51 static constexpr bool canInitializeWithMemset = true;
52 static constexpr bool canMoveWithMemcpy = true;
53 static constexpr bool canCopyWithMemcpy = true;
54 static constexpr bool canFillWithMemset = sizeof(T) == sizeof(char) && std::is_integral<T>::value;
55 static constexpr bool canCompareWithMemcmp = true;
56 };
57
58 template<typename T>
59 struct VectorTraits : VectorTraitsBase<std::is_pod<T>::value, T> { };
60
61 struct SimpleClassVectorTraits : VectorTraitsBase<false, void>
62 {
63 static constexpr bool canInitializeWithMemset = true;
64 static constexpr bool canMoveWithMemcpy = true;
65 static constexpr bool canCompareWithMemcmp = true;
66 };
67
68 // We know smart pointers are simple enough that initializing to 0 and moving with memcpy
69 // (and then not destructing the original) will work.
70
71 template<typename P> struct VectorTraits<RefPtr<P>> : SimpleClassVectorTraits { };
72 template<typename P> struct VectorTraits<std::unique_ptr<P>> : SimpleClassVectorTraits { };
73 template<typename P> struct VectorTraits<Ref<P>> : SimpleClassVectorTraits { };
74 template<> struct VectorTraits<AtomString> : SimpleClassVectorTraits { };
75
76 template<typename First, typename Second>
77 struct VectorTraits<std::pair<First, Second>>
78 {
79 typedef VectorTraits<First> FirstTraits;
80 typedef VectorTraits<Second> SecondTraits;
81
82 static constexpr bool needsInitialization = FirstTraits::needsInitialization || SecondTraits::needsInitialization;
83 static constexpr bool canInitializeWithMemset = FirstTraits::canInitializeWithMemset && SecondTraits::canInitializeWithMemset;
84 static constexpr bool canMoveWithMemcpy = FirstTraits::canMoveWithMemcpy && SecondTraits::canMoveWithMemcpy;
85 static constexpr bool canCopyWithMemcpy = FirstTraits::canCopyWithMemcpy && SecondTraits::canCopyWithMemcpy;
86 static constexpr bool canFillWithMemset = false;
87 static constexpr bool canCompareWithMemcmp = FirstTraits::canCompareWithMemcmp && SecondTraits::canCompareWithMemcmp;
88 };
89
90} // namespace WTF
91
92using WTF::VectorTraits;
93using WTF::SimpleClassVectorTraits;
94