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/Vector.h>
34
35namespace API {
36
37class Array final : public ObjectImpl<Object::Type::Array> {
38private:
39 template <class T>
40 struct IsTypePredicate {
41 bool operator()(const RefPtr<Object>& object) const { return object->type() == T::APIType; }
42 };
43
44 template <class T>
45 struct GetObjectTransform {
46 const T* operator()(const RefPtr<Object>& object) const { return static_cast<const T*>(object.get()); }
47 };
48
49 template <typename T>
50 using ElementsOfTypeRange = WTF::IteratorRange<WTF::TransformIterator<GetObjectTransform<T>, WTF::FilterIterator<IsTypePredicate<T>, Vector<RefPtr<Object>>::const_iterator>>>;
51
52public:
53 static Ref<Array> create();
54 static Ref<Array> create(Vector<RefPtr<Object>>&&);
55 static Ref<Array> createStringArray(const Vector<WTF::String>&);
56 Vector<WTF::String> toStringVector();
57 Ref<Array> copy();
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
85 template<typename MatchFunction>
86 unsigned removeAllMatching(const MatchFunction& matchFunction)
87 {
88 return m_elements.removeAllMatching(matchFunction);
89 }
90
91 template<typename T, typename MatchFunction>
92 unsigned removeAllOfTypeMatching(const MatchFunction& matchFunction)
93 {
94 return m_elements.removeAllMatching([&] (const RefPtr<Object>& object) -> bool {
95 if (object->type() != T::APIType)
96 return false;
97 return matchFunction(static_pointer_cast<T>(object));
98 });
99 }
100
101private:
102 explicit Array(Vector<RefPtr<Object>>&& elements)
103 : m_elements(WTFMove(elements))
104 {
105 }
106
107 Vector<RefPtr<Object>> m_elements;
108};
109
110} // namespace API
111
112#endif // APIArray_h
113