Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
growable_array.h
Go to the documentation of this file.
1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4// Defines growable array classes, that differ where they are allocated:
5// - GrowableArray: allocated on stack.
6// - ZoneGrowableArray: allocated in the zone.
7// - MallocGrowableArray: allocates using malloc/realloc; free is only called
8// at destruction.
9
10#ifndef RUNTIME_VM_GROWABLE_ARRAY_H_
11#define RUNTIME_VM_GROWABLE_ARRAY_H_
12
13#include <initializer_list>
14
16#include "vm/thread_state.h"
17#include "vm/zone.h"
18
19namespace dart {
20
21template <typename T>
22class GrowableArray : public BaseGrowableArray<T, ValueObject, Zone> {
23 public:
24 GrowableArray(Zone* zone, intptr_t initial_capacity)
25 : BaseGrowableArray<T, ValueObject, Zone>(initial_capacity,
26 ASSERT_NOTNULL(zone)) {}
27 explicit GrowableArray(intptr_t initial_capacity)
29 initial_capacity,
30 ASSERT_NOTNULL(ThreadState::Current()->zone())) {}
34
35 GrowableArray(std::initializer_list<T> values)
37 values.size(),
38 ASSERT_NOTNULL(ThreadState::Current()->zone())) {
39 for (auto& value : values) {
40 this->Add(value);
41 }
42 }
43
44 GrowableArray(GrowableArray&& other) = default;
46 ~GrowableArray() = default;
48};
49
50template <typename T>
51class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated, Zone> {
52 public:
53 ZoneGrowableArray(Zone* zone, intptr_t initial_capacity)
54 : BaseGrowableArray<T, ZoneAllocated, Zone>(initial_capacity,
55 ASSERT_NOTNULL(zone)) {}
56 explicit ZoneGrowableArray(intptr_t initial_capacity)
58 initial_capacity,
59 ASSERT_NOTNULL(ThreadState::Current()->zone())) {}
63
66 ~ZoneGrowableArray() = default;
68};
69
70// T must be a Handle type.
71template <typename T, typename B>
73 public:
74 BaseGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity)
75 : zone_(zone), array_(zone, initial_capacity) {}
76
77 // Use unique zone handles to store objects.
78 void Add(const T& t) { array_.Add(&T::ZoneHandle(zone_, t.ptr())); }
79
80 T& operator[](intptr_t index) const { return *array_[index]; }
81
82 const T& At(intptr_t index) const { return operator[](index); }
83
84 void SetAt(intptr_t index, const T& t) {
85 array_[index] = &T::ZoneHandle(zone_, t.ptr());
86 }
87
88 intptr_t length() const { return array_.length(); }
89
90 void Clear() { array_.Clear(); }
91
92 const GrowableArray<T*>& growable_array() const { return array_; }
93
94 private:
95 Zone* zone_;
96 GrowableArray<T*> array_;
97
99};
100
101template <typename T>
103 : public BaseGrowableHandlePtrArray<T, ValueObject> {
104 public:
105 GrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity)
106 : BaseGrowableHandlePtrArray<T, ValueObject>(zone, initial_capacity) {}
107};
108
109template <typename T>
111 : public BaseGrowableHandlePtrArray<T, ZoneAllocated> {
112 public:
113 ZoneGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity)
114 : BaseGrowableHandlePtrArray<T, ZoneAllocated>(zone, initial_capacity) {}
115};
116
117} // namespace dart
118
119#endif // RUNTIME_VM_GROWABLE_ARRAY_H_
#define ASSERT_NOTNULL(ptr)
Definition assert.h:323
intptr_t length() const
const GrowableArray< T * > & growable_array() const
T & operator[](intptr_t index) const
BaseGrowableHandlePtrArray(Zone *zone, intptr_t initial_capacity)
void SetAt(intptr_t index, const T &t)
const T & At(intptr_t index) const
GrowableArray(Zone *zone, intptr_t initial_capacity)
DISALLOW_COPY_AND_ASSIGN(GrowableArray)
GrowableArray(GrowableArray &&other)=default
GrowableArray(intptr_t initial_capacity)
~GrowableArray()=default
GrowableArray & operator=(GrowableArray &&other)=default
GrowableArray(std::initializer_list< T > values)
GrowableHandlePtrArray(Zone *zone, intptr_t initial_capacity)
DISALLOW_COPY_AND_ASSIGN(ZoneGrowableArray)
ZoneGrowableArray(ZoneGrowableArray &&other)=default
ZoneGrowableArray & operator=(ZoneGrowableArray &&other)=default
ZoneGrowableArray(Zone *zone, intptr_t initial_capacity)
ZoneGrowableArray(intptr_t initial_capacity)
ZoneGrowableHandlePtrArray(Zone *zone, intptr_t initial_capacity)
uint8_t value
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581
#define T