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