WTF
VectorTraits.h
Go to the documentation of this file.
00001 // -*- mode: c++; c-basic-offset: 4 -*- 00002 /* 00003 * This file is part of the KDE libraries 00004 * Copyright (C) 2006 Apple Computer, Inc. 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public License 00017 * along with this library; see the file COPYING.LIB. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 * 00021 */ 00022 00023 #ifndef WTF_VectorTraits_h 00024 #define WTF_VectorTraits_h 00025 00026 #include "RefPtr.h" 00027 #include <utility> 00028 00029 using std::pair; 00030 00031 namespace WTF { 00032 00033 template <typename T> struct IsPod { static const bool value = false; }; 00034 template <> struct IsPod<bool> { static const bool value = true; }; 00035 template <> struct IsPod<char> { static const bool value = true; }; 00036 template <> struct IsPod<signed char> { static const bool value = true; }; 00037 template <> struct IsPod<unsigned char> { static const bool value = true; }; 00038 template <> struct IsPod<short> { static const bool value = true; }; 00039 template <> struct IsPod<unsigned short> { static const bool value = true; }; 00040 template <> struct IsPod<int> { static const bool value = true; }; 00041 template <> struct IsPod<unsigned int> { static const bool value = true; }; 00042 template <> struct IsPod<long> { static const bool value = true; }; 00043 template <> struct IsPod<unsigned long> { static const bool value = true; }; 00044 template <> struct IsPod<long long> { static const bool value = true; }; 00045 template <> struct IsPod<unsigned long long> { static const bool value = true; }; 00046 template <> struct IsPod<float> { static const bool value = true; }; 00047 template <> struct IsPod<double> { static const bool value = true; }; 00048 template <> struct IsPod<long double> { static const bool value = true; }; 00049 template <typename P> struct IsPod<P *> { static const bool value = true; }; 00050 00051 template<bool isPod, typename T> 00052 struct VectorTraitsBase; 00053 00054 template<typename T> 00055 struct VectorTraitsBase<false, T> 00056 { 00057 static const bool needsDestruction = true; 00058 static const bool needsInitialization = true; 00059 static const bool canInitializeWithMemset = false; 00060 static const bool canMoveWithMemcpy = false; 00061 static const bool canCopyWithMemcpy = false; 00062 static const bool canFillWithMemset = false; 00063 static const bool canCompareWithMemcmp = false; 00064 }; 00065 00066 template<typename T> 00067 struct VectorTraitsBase<true, T> 00068 { 00069 static const bool needsDestruction = false; 00070 static const bool needsInitialization = false; 00071 static const bool canInitializeWithMemset = false; 00072 static const bool canMoveWithMemcpy = true; 00073 static const bool canCopyWithMemcpy = true; 00074 static const bool canFillWithMemset = sizeof(T) == sizeof(char); 00075 static const bool canCompareWithMemcmp = true; 00076 }; 00077 00078 template<typename T> 00079 struct VectorTraits : VectorTraitsBase<IsPod<T>::value, T> { }; 00080 00081 struct SimpleClassVectorTraits 00082 { 00083 static const bool needsDestruction = true; 00084 static const bool needsInitialization = true; 00085 static const bool canInitializeWithMemset = true; 00086 static const bool canMoveWithMemcpy = true; 00087 static const bool canCopyWithMemcpy = false; 00088 static const bool canFillWithMemset = false; 00089 static const bool canCompareWithMemcmp = true; 00090 }; 00091 00092 // we know RefPtr is simple enough that initializing to 0 and moving with memcpy 00093 // (and then not destructing the original) will totally work 00094 template<typename P> 00095 struct VectorTraits<RefPtr<P> > : SimpleClassVectorTraits { }; 00096 00097 template<typename First, typename Second> 00098 struct VectorTraits<pair<First, Second> > 00099 { 00100 typedef VectorTraits<First> FirstTraits; 00101 typedef VectorTraits<Second> SecondTraits; 00102 00103 static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction; 00104 static const bool needsInitialization = FirstTraits::needsInitialization || SecondTraits::needsInitialization; 00105 static const bool canInitializeWithMemset = FirstTraits::canInitializeWithMemset && SecondTraits::canInitializeWithMemset; 00106 static const bool canMoveWithMemcpy = FirstTraits::canMoveWithMemcpy && SecondTraits::canMoveWithMemcpy; 00107 static const bool canCopyWithMemcpy = FirstTraits::canCopyWithMemcpy && SecondTraits::canCopyWithMemcpy; 00108 static const bool canFillWithMemset = false; 00109 static const bool canCompareWithMemcmp = FirstTraits::canCompareWithMemcmp && SecondTraits::canCompareWithMemcmp; 00110 }; 00111 00112 } // namespace WTF 00113 00114 using WTF::VectorTraits; 00115 using WTF::SimpleClassVectorTraits; 00116 00117 #endif // WTF_VectorTraits_h
KDE 4.6 API Reference