File indexing completed on 2024-05-12 15:43:40

0001 /*
0002  * This file is part of the KDE libraries
0003  * Copyright (C) 2006 Apple Computer, Inc.
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  *
0020  */
0021 
0022 #ifndef WTF_VectorTraits_h
0023 #define WTF_VectorTraits_h
0024 
0025 #include <wtf/RefPtr.h>
0026 #include <utility>
0027 
0028 using std::pair;
0029 
0030 namespace WTF
0031 {
0032 
0033 template <typename T> struct IsPod           {
0034     static const bool value = false;
0035 };
0036 template <> struct IsPod<bool>               {
0037     static const bool value = true;
0038 };
0039 template <> struct IsPod<char>               {
0040     static const bool value = true;
0041 };
0042 template <> struct IsPod<signed char>        {
0043     static const bool value = true;
0044 };
0045 template <> struct IsPod<unsigned char>      {
0046     static const bool value = true;
0047 };
0048 template <> struct IsPod<short>              {
0049     static const bool value = true;
0050 };
0051 template <> struct IsPod<unsigned short>     {
0052     static const bool value = true;
0053 };
0054 template <> struct IsPod<int>                {
0055     static const bool value = true;
0056 };
0057 template <> struct IsPod<unsigned int>       {
0058     static const bool value = true;
0059 };
0060 template <> struct IsPod<long>               {
0061     static const bool value = true;
0062 };
0063 template <> struct IsPod<unsigned long>      {
0064     static const bool value = true;
0065 };
0066 template <> struct IsPod<long long>          {
0067     static const bool value = true;
0068 };
0069 template <> struct IsPod<unsigned long long> {
0070     static const bool value = true;
0071 };
0072 template <> struct IsPod<float>              {
0073     static const bool value = true;
0074 };
0075 template <> struct IsPod<double>             {
0076     static const bool value = true;
0077 };
0078 template <> struct IsPod<long double>        {
0079     static const bool value = true;
0080 };
0081 template <typename P> struct IsPod<P *>      {
0082     static const bool value = true;
0083 };
0084 
0085 template<bool isPod, typename T>
0086 struct VectorTraitsBase;
0087 
0088 template<typename T>
0089 struct VectorTraitsBase<false, T> {
0090     static const bool needsDestruction = true;
0091     static const bool needsInitialization = true;
0092     static const bool canInitializeWithMemset = false;
0093     static const bool canMoveWithMemcpy = false;
0094     static const bool canCopyWithMemcpy = false;
0095     static const bool canFillWithMemset = false;
0096     static const bool canCompareWithMemcmp = false;
0097 };
0098 
0099 template<typename T>
0100 struct VectorTraitsBase<true, T> {
0101     static const bool needsDestruction = false;
0102     static const bool needsInitialization = false;
0103     static const bool canInitializeWithMemset = false;
0104     static const bool canMoveWithMemcpy = true;
0105     static const bool canCopyWithMemcpy = true;
0106     static const bool canFillWithMemset = sizeof(T) == sizeof(char);
0107     static const bool canCompareWithMemcmp = true;
0108 };
0109 
0110 template<typename T>
0111 struct VectorTraits : VectorTraitsBase<IsPod<T>::value, T> { };
0112 
0113 struct SimpleClassVectorTraits {
0114     static const bool needsDestruction = true;
0115     static const bool needsInitialization = true;
0116     static const bool canInitializeWithMemset = true;
0117     static const bool canMoveWithMemcpy = true;
0118     static const bool canCopyWithMemcpy = false;
0119     static const bool canFillWithMemset = false;
0120     static const bool canCompareWithMemcmp = true;
0121 };
0122 
0123 // we know RefPtr is simple enough that initializing to 0 and moving with memcpy
0124 // (and then not destructing the original) will totally work
0125 template<typename P>
0126 struct VectorTraits<RefPtr<P> > : SimpleClassVectorTraits { };
0127 
0128 template<typename First, typename Second>
0129 struct VectorTraits<pair<First, Second> > {
0130     typedef VectorTraits<First> FirstTraits;
0131     typedef VectorTraits<Second> SecondTraits;
0132 
0133     static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;
0134     static const bool needsInitialization = FirstTraits::needsInitialization || SecondTraits::needsInitialization;
0135     static const bool canInitializeWithMemset = FirstTraits::canInitializeWithMemset && SecondTraits::canInitializeWithMemset;
0136     static const bool canMoveWithMemcpy = FirstTraits::canMoveWithMemcpy && SecondTraits::canMoveWithMemcpy;
0137     static const bool canCopyWithMemcpy = FirstTraits::canCopyWithMemcpy && SecondTraits::canCopyWithMemcpy;
0138     static const bool canFillWithMemset = false;
0139     static const bool canCompareWithMemcmp = FirstTraits::canCompareWithMemcmp && SecondTraits::canCompareWithMemcmp;
0140 };
0141 
0142 } // namespace WTF
0143 
0144 using WTF::VectorTraits;
0145 using WTF::SimpleClassVectorTraits;
0146 
0147 #endif // WTF_VectorTraits_h