VectorTraits.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef WTF_VectorTraits_h
  21. #define WTF_VectorTraits_h
  22. #include <wtf/OwnArrayPtr.h>
  23. #include <wtf/OwnPtr.h>
  24. #include <wtf/RefPtr.h>
  25. #include <wtf/TypeTraits.h>
  26. #include <utility>
  27. #include <memory>
  28. using std::pair;
  29. namespace WTF {
  30. class AtomicString;
  31. template<bool isPod, typename T>
  32. struct VectorTraitsBase;
  33. template<typename T>
  34. struct VectorTraitsBase<false, T>
  35. {
  36. static const bool needsDestruction = true;
  37. static const bool needsInitialization = true;
  38. static const bool canInitializeWithMemset = false;
  39. static const bool canMoveWithMemcpy = false;
  40. static const bool canCopyWithMemcpy = false;
  41. static const bool canFillWithMemset = false;
  42. static const bool canCompareWithMemcmp = false;
  43. };
  44. template<typename T>
  45. struct VectorTraitsBase<true, T>
  46. {
  47. static const bool needsDestruction = false;
  48. static const bool needsInitialization = false;
  49. static const bool canInitializeWithMemset = false;
  50. static const bool canMoveWithMemcpy = true;
  51. static const bool canCopyWithMemcpy = true;
  52. static const bool canFillWithMemset = sizeof(T) == sizeof(char);
  53. static const bool canCompareWithMemcmp = true;
  54. };
  55. template<typename T>
  56. struct VectorTraits : VectorTraitsBase<IsPod<T>::value, T> { };
  57. struct SimpleClassVectorTraits : VectorTraitsBase<false, void>
  58. {
  59. static const bool canInitializeWithMemset = true;
  60. static const bool canMoveWithMemcpy = true;
  61. static const bool canCompareWithMemcmp = true;
  62. };
  63. // we know OwnPtr and RefPtr are simple enough that initializing to 0 and moving with memcpy
  64. // (and then not destructing the original) will totally work
  65. template<typename P>
  66. struct VectorTraits<RefPtr<P> > : SimpleClassVectorTraits { };
  67. template<typename P>
  68. struct VectorTraits<OwnPtr<P> > : SimpleClassVectorTraits { };
  69. template<typename P>
  70. struct VectorTraits<OwnArrayPtr<P> > : SimpleClassVectorTraits { };
  71. template<>
  72. struct VectorTraits<AtomicString> : SimpleClassVectorTraits { };
  73. template<typename First, typename Second>
  74. struct VectorTraits<pair<First, Second> >
  75. {
  76. typedef VectorTraits<First> FirstTraits;
  77. typedef VectorTraits<Second> SecondTraits;
  78. static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;
  79. static const bool needsInitialization = FirstTraits::needsInitialization || SecondTraits::needsInitialization;
  80. static const bool canInitializeWithMemset = FirstTraits::canInitializeWithMemset && SecondTraits::canInitializeWithMemset;
  81. static const bool canMoveWithMemcpy = FirstTraits::canMoveWithMemcpy && SecondTraits::canMoveWithMemcpy;
  82. static const bool canCopyWithMemcpy = FirstTraits::canCopyWithMemcpy && SecondTraits::canCopyWithMemcpy;
  83. static const bool canFillWithMemset = false;
  84. static const bool canCompareWithMemcmp = FirstTraits::canCompareWithMemcmp && SecondTraits::canCompareWithMemcmp;
  85. };
  86. } // namespace WTF
  87. using WTF::VectorTraits;
  88. using WTF::SimpleClassVectorTraits;
  89. #endif // WTF_VectorTraits_h