summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-08-07 09:33:05 +0200
committerAndreas Kling <kling@serenityos.org>2020-08-07 09:33:05 +0200
commit420e809feefa647fbe6f624f7ca02206ad6aa7ee (patch)
tree7363c7f0f4d620a4d94461f11934262047faa418
parent08e5371f4447f7174dc8eb81bd7a9c58ea9783d1 (diff)
downloadserenity-420e809feefa647fbe6f624f7ca02206ad6aa7ee.zip
Shell: Store ListValue's values in a NonnullRefPtrVector<Value>
A ListValue never stores null Values, so it makes sense to restrict it. This also propagates use of NonnullRefPtr to the create() helpers. There's a small bit of awkwardness around the use of initializer_list, since we cannot directly construct a NonnullRefPtrVector from one.
-rw-r--r--Shell/AST.cpp20
-rw-r--r--Shell/AST.h10
-rw-r--r--Shell/Builtin.cpp2
3 files changed, 16 insertions, 16 deletions
diff --git a/Shell/AST.cpp b/Shell/AST.cpp
index bb441d907d..69e80ee612 100644
--- a/Shell/AST.cpp
+++ b/Shell/AST.cpp
@@ -36,13 +36,13 @@
namespace AST {
template<typename T, typename... Args>
-static inline RefPtr<T> create(Args... args)
+static inline NonnullRefPtr<T> create(Args... args)
{
return adopt(*new T(args...));
}
template<typename T>
-static inline RefPtr<T> create(std::initializer_list<RefPtr<Value>> arg)
+static inline NonnullRefPtr<T> create(std::initializer_list<NonnullRefPtr<Value>> arg)
{
return adopt(*new T(arg));
}
@@ -226,7 +226,7 @@ RefPtr<Value> ListConcatenate::run(RefPtr<Shell> shell)
for (auto& element : m_list) {
if (!result) {
- result = create<ListValue>({ element->run(shell)->resolve_without_cast(shell) });
+ result = create<ListValue>({ element->run(shell)->resolve_without_cast(shell).release_nonnull() });
continue;
}
auto element_value = element->run(shell)->resolve_without_cast(shell);
@@ -239,7 +239,7 @@ RefPtr<Value> ListConcatenate::run(RefPtr<Shell> shell)
else
result = create<CommandSequenceValue>(move(joined_commands));
} else {
- Vector<RefPtr<Value>> values;
+ NonnullRefPtrVector<Value> values;
if (result->is_list_without_resolution()) {
values.append(static_cast<ListValue*>(result.ptr())->values());
@@ -248,7 +248,7 @@ RefPtr<Value> ListConcatenate::run(RefPtr<Shell> shell)
values.append(create<StringValue>(result));
}
- values.append(move(element_value));
+ values.append(element_value.release_nonnull());
result = create<ListValue>(move(values));
}
@@ -496,7 +496,7 @@ RefPtr<Value> CastToList::run(RefPtr<Shell> shell)
return inner_value;
auto values = inner_value->resolve_as_list(shell);
- Vector<RefPtr<Value>> cast_values;
+ NonnullRefPtrVector<Value> cast_values;
for (auto& value : values)
cast_values.append(create<StringValue>(value));
@@ -754,7 +754,7 @@ RefPtr<Value> ForLoop::run(RefPtr<Shell> shell)
if (!m_block)
return create<ListValue>({});
- Vector<RefPtr<Value>> values;
+ NonnullRefPtrVector<Value> values;
auto resolved = m_iterated_expression->run(shell)->resolve_without_cast(shell);
if (resolved->is_list_without_resolution())
values = static_cast<ListValue*>(resolved.ptr())->values();
@@ -1899,16 +1899,16 @@ Vector<String> ListValue::resolve_as_list(RefPtr<Shell> shell)
{
Vector<String> values;
for (auto& value : m_contained_values)
- values.append(value->resolve_as_list(shell));
+ values.append(value.resolve_as_list(shell));
return values;
}
RefPtr<Value> ListValue::resolve_without_cast(RefPtr<Shell> shell)
{
- Vector<RefPtr<Value>> values;
+ NonnullRefPtrVector<Value> values;
for (auto& value : m_contained_values)
- values.append(value->resolve_without_cast(shell));
+ values.append(value.resolve_without_cast(shell).release_nonnull());
return create<ListValue>(move(values));
}
diff --git a/Shell/AST.h b/Shell/AST.h
index 4cdd805e00..546c9882a4 100644
--- a/Shell/AST.h
+++ b/Shell/AST.h
@@ -238,16 +238,16 @@ public:
virtual bool is_list() const override { return true; }
virtual bool is_list_without_resolution() const override { return true; }
ListValue(Vector<String> values);
- ListValue(Vector<RefPtr<Value>> values)
- : m_contained_values(move(values))
+ ListValue(Vector<NonnullRefPtr<Value>> values)
+ : m_contained_values(move(static_cast<NonnullRefPtrVector<Value>&>(values)))
{
}
- const Vector<RefPtr<Value>>& values() const { return m_contained_values; }
- Vector<RefPtr<Value>>& values() { return m_contained_values; }
+ const NonnullRefPtrVector<Value>& values() const { return m_contained_values; }
+ NonnullRefPtrVector<Value>& values() { return m_contained_values; }
private:
- Vector<RefPtr<Value>> m_contained_values;
+ NonnullRefPtrVector<Value> m_contained_values;
};
class StringValue final : public Value {
diff --git a/Shell/Builtin.cpp b/Shell/Builtin.cpp
index 8e547f0c0d..9ca91a71ee 100644
--- a/Shell/Builtin.cpp
+++ b/Shell/Builtin.cpp
@@ -664,7 +664,7 @@ int Shell::builtin_shift(int argc, const char** argv)
}
if (!argv_->is_list())
- argv_ = adopt(*new AST::ListValue({ argv_ }));
+ argv_ = adopt(*new AST::ListValue({ argv_.release_nonnull() }));
auto& values = static_cast<AST::ListValue*>(argv_.ptr())->values();
if ((size_t)count > values.size()) {