1/* 2 * Copyright (C) 2010, 2013 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#ifndef APIArray_h 27#define APIArray_h 28 29#include "APIObject.h" 30#include <wtf/Forward.h> 31#include <wtf/IteratorAdaptors.h> 32#include <wtf/IteratorRange.h> 33#include <wtf/PassRefPtr.h> 34#include <wtf/Vector.h> 35 36namespace API { 37 38class Array final : public ObjectImpl<Object::Type::Array> { 39private: 40 template <class T> 41 struct IsTypePredicate { 42 bool operator()(const RefPtr<Object>& object) const { return object->type() == T::APIType; } 43 }; 44 45 template <class T> 46 struct GetObjectTransform { 47 const T* operator()(const RefPtr<Object>& object) const { return static_cast<const T*>(object.get()); } 48 }; 49 50 template <typename T> 51 using ElementsOfTypeRange = WTF::IteratorRange<WTF::TransformIterator<GetObjectTransform<T>, WTF::FilterIterator<IsTypePredicate<T>, Vector<RefPtr<Object>>::const_iterator>>>; 52 53public: 54 static PassRefPtr<Array> create(); 55 static PassRefPtr<Array> create(Vector<RefPtr<Object>> elements); 56 static PassRefPtr<Array> createStringArray(const Vector<WTF::String>&); 57 Vector<WTF::String> toStringVector(); 58 59 virtual ~Array(); 60 61 template<typename T> 62 T* at(size_t i) const 63 { 64 if (!m_elements[i] || m_elements[i]->type() != T::APIType) 65 return nullptr; 66 67 return static_cast<T*>(m_elements[i].get()); 68 } 69 70 Object* at(size_t i) const { return m_elements[i].get(); } 71 size_t size() const { return m_elements.size(); } 72 73 const Vector<RefPtr<Object>>& elements() const { return m_elements; } 74 Vector<RefPtr<Object>>& elements() { return m_elements; } 75 76 template<typename T> 77 ElementsOfTypeRange<T> elementsOfType() const 78 { 79 return WTF::makeIteratorRange( 80 WTF::makeTransformIterator(GetObjectTransform<T>(), WTF::makeFilterIterator(IsTypePredicate<T>(), m_elements.begin(), m_elements.end())), 81 WTF::makeTransformIterator(GetObjectTransform<T>(), WTF::makeFilterIterator(IsTypePredicate<T>(), m_elements.end(), m_elements.end())) 82 ); 83 } 84 85private: 86 explicit Array(Vector<RefPtr<Object>> elements); 87 88 Vector<RefPtr<Object>> m_elements; 89}; 90 91} // namespace API 92 93#endif // APIArray_h 94