Add ability to iterate over API::Array

https://bugs.webkit.org/show_bug.cgi?id=124533

Patch by Martin Hock <mhock@apple.com> on 2013-12-02
Reviewed by Sam Weinig.

* GNUmakefile.list.am:
* Shared/APIArray.h:
* Shared/FilterIterator.h:
* Shared/IteratorPair.h:
* WebKit2.xcodeproj/project.pbxproj:
* WebProcess/InjectedBundle/InjectedBundle.cpp:
(WebKit::toStringVector):
* WebProcess/InjectedBundle/InjectedBundlePageEditorClient.cpp:
(WebKit::InjectedBundlePageEditorClient::getPasteboardDataForRange):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@159992 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent a1a1baa5
2013-12-02 Martin Hock <mhock@apple.com>
Add ability to iterate over API::Array
https://bugs.webkit.org/show_bug.cgi?id=124533
Reviewed by Sam Weinig.
* GNUmakefile.list.am:
* Shared/APIArray.h:
* Shared/FilterIterator.h:
* Shared/IteratorPair.h:
* WebKit2.xcodeproj/project.pbxproj:
* WebProcess/InjectedBundle/InjectedBundle.cpp:
(WebKit::toStringVector):
* WebProcess/InjectedBundle/InjectedBundlePageEditorClient.cpp:
(WebKit::InjectedBundlePageEditorClient::getPasteboardDataForRange):
2013-12-02 Dan Bernstein <mitz@apple.com>
[Cocoa] WebProtectionSpace has a generic wrapper
......
......@@ -422,6 +422,7 @@ webkit2_sources += \
Source/WebKit2/Shared/Downloads/soup/DownloadSoupErrors.h \
Source/WebKit2/Shared/EditorState.cpp \
Source/WebKit2/Shared/EditorState.h \
Source/WebKit2/Shared/FilterIterator.h \
Source/WebKit2/Shared/FontSmoothingLevel.h \
Source/WebKit2/Shared/ProcessExecutablePath.h \
Source/WebKit2/Shared/cairo/ShareableBitmapCairo.cpp \
......@@ -438,6 +439,7 @@ webkit2_sources += \
Source/WebKit2/Shared/ImageOptions.h \
Source/WebKit2/Shared/ImmutableDictionary.cpp \
Source/WebKit2/Shared/ImmutableDictionary.h \
Source/WebKit2/Shared/IteratorPair.h \
Source/WebKit2/Shared/LayerTreeContext.h \
Source/WebKit2/Shared/MutableDictionary.cpp \
Source/WebKit2/Shared/MutableDictionary.h \
......
/*
* Copyright (C) 2010 Apple Inc. All rights reserved.
* Copyright (C) 2010, 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
......@@ -27,6 +27,8 @@
#define APIArray_h
#include "APIObject.h"
#include "FilterIterator.h"
#include "IteratorPair.h"
#include <wtf/Forward.h>
#include <wtf/PassRefPtr.h>
#include <wtf/Vector.h>
......@@ -34,6 +36,13 @@
namespace API {
class Array FINAL : public TypedObject<Object::Type::Array> {
private:
template<typename T>
static inline const T* getObject(const RefPtr<Object>& object) { return static_cast<const T*>(object.get()); }
template<typename T>
static inline bool isType(const RefPtr<Object>& object) { return object->type() == T::APIType; }
public:
static PassRefPtr<Array> create();
static PassRefPtr<Array> create(Vector<RefPtr<Object>> elements);
......@@ -45,7 +54,7 @@ public:
template<typename T>
T* at(size_t i) const
{
if (m_elements[i]->type() != T::APIType)
if (!isType<T>(m_elements[i]))
return nullptr;
return static_cast<T*>(m_elements[i].get());
......@@ -57,6 +66,12 @@ public:
const Vector<RefPtr<Object>>& elements() const { return m_elements; }
Vector<RefPtr<Object>>& elements() { return m_elements; }
template<typename T>
IteratorPair<FilterIterator<decltype(&isType<T>), decltype(&getObject<T>), Vector<RefPtr<Object>>::const_iterator>> elementsOfType()
{
return IteratorPair<FilterIterator<decltype(&isType<T>), decltype(&getObject<T>), Vector<RefPtr<Object>>::const_iterator>>(FilterIterator<decltype(&isType<T>), decltype(&getObject<T>), Vector<RefPtr<Object>>::const_iterator>(isType<T>, getObject<T>, m_elements.begin(), m_elements.end()), FilterIterator<decltype(&isType<T>), decltype(&getObject<T>), Vector<RefPtr<Object>>::const_iterator>(isType<T>, getObject<T>, m_elements.end(), m_elements.end()));
}
private:
explicit Array(Vector<RefPtr<Object>> elements);
......
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FilterIterator_h
#define FilterIterator_h
#include "APIObject.h"
#include <wtf/Forward.h>
#include <wtf/PassRefPtr.h>
#include <wtf/Vector.h>
namespace API {
template<typename Predicate, typename Cast, typename Iterator>
class FilterIterator {
public:
FilterIterator(Predicate pred, Cast cast, Iterator begin, Iterator end)
: m_pred(std::move(pred))
, m_cast(std::move(cast))
, m_iter(std::move(begin))
, m_end(std::move(end))
{
while (m_iter != m_end && !m_pred(*m_iter))
++m_iter;
}
FilterIterator& operator++()
{
while (m_iter != m_end) {
++m_iter;
if (m_iter == m_end || m_pred(*m_iter))
break;
}
return *this;
}
const decltype(std::declval<Cast>()(*std::declval<Iterator>())) operator*() const
{
ASSERT(m_iter != m_end);
ASSERT(m_pred(*m_iter));
return m_cast(*m_iter);
}
inline bool operator==(FilterIterator& other) const { return m_iter == other.m_iter; }
inline bool operator!=(FilterIterator& other) const { return m_iter != other.m_iter; }
private:
const Predicate m_pred;
const Cast m_cast;
Iterator m_iter;
Iterator m_end;
};
} // namespace API
#endif // FilterIterator_h
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
template<typename Iterator> class IteratorPair {
public:
IteratorPair(Iterator begin, Iterator end)
: m_begin(std::move(begin))
, m_end(std::move(end))
{
}
Iterator begin() { return m_begin; }
Iterator end() { return m_end; }
private:
Iterator m_begin, m_end;
};
......@@ -684,6 +684,7 @@
6EE849C81368D9390038D481 /* WKInspectorPrivateMac.h in Headers */ = {isa = PBXBuildFile; fileRef = 6EE849C61368D92D0038D481 /* WKInspectorPrivateMac.h */; settings = {ATTRIBUTES = (Private, ); }; };
728E86F11795188C0087879E /* WebColorPickerMac.h in Headers */ = {isa = PBXBuildFile; fileRef = 728E86EF1795188C0087879E /* WebColorPickerMac.h */; };
728E86F21795188C0087879E /* WebColorPickerMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 728E86F01795188C0087879E /* WebColorPickerMac.mm */; };
752A8077183D6DA7003D659E /* FilterIterator.h in Copy Files */ = {isa = PBXBuildFile; fileRef = 752A8076183D6DA7003D659E /* FilterIterator.h */; };
755422BB18062B9C0046F6A8 /* WKOriginDataManager.cpp in Copy Files */ = {isa = PBXBuildFile; fileRef = 755422B918062B9C0046F6A8 /* WKOriginDataManager.cpp */; };
755422BC18062B9C0046F6A8 /* WKOriginDataManager.h in Copy Files */ = {isa = PBXBuildFile; fileRef = 755422BA18062B9C0046F6A8 /* WKOriginDataManager.h */; };
755422BD18062BB20046F6A8 /* WKOriginDataManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 755422B918062B9C0046F6A8 /* WKOriginDataManager.cpp */; };
......@@ -691,6 +692,7 @@
755422C418062BF90046F6A8 /* WebOriginDataManagerProxy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 755422BE18062BE40046F6A8 /* WebOriginDataManagerProxy.cpp */; };
755422CB180650020046F6A8 /* WebOriginDataManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 755422C618064FFC0046F6A8 /* WebOriginDataManager.cpp */; };
755422CC180773CE0046F6A8 /* WebOriginDataManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 755422C718064FFC0046F6A8 /* WebOriginDataManager.h */; };
757AF978184D131400E6316E /* IteratorPair.h in Headers */ = {isa = PBXBuildFile; fileRef = 757AF976184D130500E6316E /* IteratorPair.h */; };
759CCD581808F1640078E8A8 /* WebOriginDataManagerProxyChangeClient.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 759CCD541808F1600078E8A8 /* WebOriginDataManagerProxyChangeClient.cpp */; };
759CCD591808F1690078E8A8 /* WebOriginDataManagerProxyChangeClient.h in Headers */ = {isa = PBXBuildFile; fileRef = 759CCD551808F1600078E8A8 /* WebOriginDataManagerProxyChangeClient.h */; };
759CCD5A1808F6160078E8A8 /* WebOriginDataManagerProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = 755422BF18062BE40046F6A8 /* WebOriginDataManagerProxy.h */; };
......@@ -1467,6 +1469,7 @@
51A60B2A180CCEB500F3BF50 /* DatabaseProcess.app in Copy Files */,
755422C318062BE40046F6A8 /* WebOriginDataManagerProxy.messages.in in Copy Files */,
1A50DB66110A3D57000D3FE5 /* WebProcess.app in Copy Files */,
752A8077183D6DA7003D659E /* FilterIterator.h in Copy Files */,
755422BB18062B9C0046F6A8 /* WKOriginDataManager.cpp in Copy Files */,
);
name = "Copy Files";
......@@ -2252,6 +2255,7 @@
6EE849C61368D92D0038D481 /* WKInspectorPrivateMac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = WKInspectorPrivateMac.h; path = mac/WKInspectorPrivateMac.h; sourceTree = "<group>"; };
728E86EF1795188C0087879E /* WebColorPickerMac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebColorPickerMac.h; sourceTree = "<group>"; };
728E86F01795188C0087879E /* WebColorPickerMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebColorPickerMac.mm; sourceTree = "<group>"; };
752A8076183D6DA7003D659E /* FilterIterator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FilterIterator.h; sourceTree = "<group>"; };
755422B918062B9C0046F6A8 /* WKOriginDataManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WKOriginDataManager.cpp; sourceTree = "<group>"; };
755422BA18062B9C0046F6A8 /* WKOriginDataManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKOriginDataManager.h; sourceTree = "<group>"; };
755422BE18062BE40046F6A8 /* WebOriginDataManagerProxy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebOriginDataManagerProxy.cpp; sourceTree = "<group>"; };
......@@ -2260,6 +2264,7 @@
755422C618064FFC0046F6A8 /* WebOriginDataManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = WebOriginDataManager.cpp; path = OriginData/WebOriginDataManager.cpp; sourceTree = "<group>"; };
755422C718064FFC0046F6A8 /* WebOriginDataManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = WebOriginDataManager.h; path = OriginData/WebOriginDataManager.h; sourceTree = "<group>"; };
755422C818064FFC0046F6A8 /* WebOriginDataManager.messages.in */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = WebOriginDataManager.messages.in; path = OriginData/WebOriginDataManager.messages.in; sourceTree = "<group>"; };
757AF976184D130500E6316E /* IteratorPair.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IteratorPair.h; sourceTree = "<group>"; };
759CCD541808F1600078E8A8 /* WebOriginDataManagerProxyChangeClient.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebOriginDataManagerProxyChangeClient.cpp; sourceTree = "<group>"; };
759CCD551808F1600078E8A8 /* WebOriginDataManagerProxyChangeClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebOriginDataManagerProxyChangeClient.h; sourceTree = "<group>"; };
75E749E5180DBB9800088BA6 /* WebOriginDataManagerMessageReceiver.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebOriginDataManagerMessageReceiver.cpp; sourceTree = "<group>"; };
......@@ -3425,6 +3430,7 @@
0FB659221208B4DB0044816C /* DrawingAreaInfo.h */,
8CFECE931490F140002AAA32 /* EditorState.cpp */,
1AA41AB412C02EC4002BE67B /* EditorState.h */,
752A8076183D6DA7003D659E /* FilterIterator.h */,
BCE81D8A1319F7EF00241910 /* FontInfo.cpp */,
BCE81D8B1319F7EF00241910 /* FontInfo.h */,
762B7481120BBA0100819339 /* FontSmoothingLevel.h */,
......@@ -3432,6 +3438,7 @@
BCCF6B2312C93E7A008F9C35 /* ImageOptions.h */,
BCBCB0CC1215E33A00DE59CA /* ImmutableDictionary.cpp */,
BCBCB0CA1215E32100DE59CA /* ImmutableDictionary.h */,
757AF976184D130500E6316E /* IteratorPair.h */,
1A92DC1012F8BA460017AF65 /* LayerTreeContext.h */,
BCB0AEE8122F53E300B1341E /* MutableDictionary.cpp */,
BCB0AEE7122F53E300B1341E /* MutableDictionary.h */,
......@@ -5796,6 +5803,7 @@
51D130561382EAC000351EDD /* SecItemResponseData.h in Headers */,
E179088F169BAA6A006904C7 /* SecItemShim.h in Headers */,
511F8A7B138B460900A95F44 /* SecItemShimLibrary.h in Headers */,
757AF978184D131400E6316E /* IteratorPair.h in Headers */,
E1790891169BAA82006904C7 /* SecItemShimMessages.h in Headers */,
E18E690C169B563F009B6670 /* SecItemShimProxy.h in Headers */,
E18E6918169B667B009B6670 /* SecItemShimProxyMessages.h in Headers */,
......
......@@ -485,11 +485,8 @@ static Vector<String> toStringVector(API::Array* patterns)
return patternsVector;
patternsVector.reserveInitialCapacity(size);
for (size_t i = 0; i < size; ++i) {
WebString* entry = patterns->at<WebString>(i);
if (entry)
patternsVector.uncheckedAppend(entry->string());
}
for (const auto& entry : patterns->elementsOfType<WebString>())
patternsVector.uncheckedAppend(entry->string());
return patternsVector;
}
......
......@@ -154,20 +154,12 @@ void InjectedBundlePageEditorClient::getPasteboardDataForRange(WebPage* page, Ra
ASSERT(typesArray->size() == dataArray->size());
size_t size = typesArray->size();
for (size_t i = 0; i < size; ++i) {
WebString* type = typesArray->at<WebString>(i);
if (type)
pasteboardTypes.append(type->string());
}
for (const auto& type : typesArray->elementsOfType<WebString>())
pasteboardTypes.append(type->string());
size = dataArray->size();
for (size_t i = 0; i < size; ++i) {
WebData* item = dataArray->at<WebData>(i);
if (item) {
RefPtr<SharedBuffer> buffer = SharedBuffer::create(item->bytes(), item->size());
pasteboardData.append(buffer);
}
for (const auto& item : dataArray->elementsOfType<WebData>()) {
RefPtr<SharedBuffer> buffer = SharedBuffer::create(item->bytes(), item->size());
pasteboardData.append(buffer);
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment