From de91660f3c1510d2cd08788dd4f108484d6f6209 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Fri, 1 Nov 2019 05:33:53 -0300 Subject: [PATCH] op: Replace owning star pointers with unique_ptr --- src/literal_number.h | 5 +++-- src/op.cpp | 20 ++++++-------------- src/op.h | 4 +--- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/literal_number.h b/src/literal_number.h index 6baece8..a053057 100644 --- a/src/literal_number.h +++ b/src/literal_number.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include "operand.h" #include "stream.h" @@ -24,10 +25,10 @@ public: bool operator==(const Operand& other) const override; template - static LiteralNumber* Create(T value) { + static std::unique_ptr Create(T value) { static_assert(sizeof(T) == 4 || sizeof(T) == 8); - LiteralNumber* number = new LiteralNumber(std::type_index(typeid(T))); + auto number = std::make_unique(std::type_index(typeid(T))); number->is_32 = sizeof(T) == 4; std::memcpy(&number->raw, &value, sizeof(T)); return number; diff --git a/src/op.cpp b/src/op.cpp index 974f0c3..8b50600 100644 --- a/src/op.cpp +++ b/src/op.cpp @@ -62,20 +62,13 @@ void Op::Write(Stream& stream) const { } } -void Op::Sink(Operand* operand) { - assert(operand); - Add(static_cast(operand)); - operand_store.push_back(std::unique_ptr(operand)); -} - -void Op::Sink(const std::vector& operands_) { - for (auto* operand : operands_) { - Sink(operand); - } +void Op::Sink(std::unique_ptr operand) { + Add(static_cast(operand.get())); + operand_store.push_back(std::move(operand)); } void Op::Add(const Literal& literal) { - Operand* operand = [&]() { + Sink([&] { switch (literal.index()) { case 0: return LiteralNumber::Create(std::get<0>(literal)); @@ -93,8 +86,7 @@ void Op::Add(const Literal& literal) { assert(!"Invalid literal type"); abort(); } - }(); - Sink(operand); + }()); } void Op::Add(const std::vector& literals) { @@ -113,7 +105,7 @@ void Op::Add(u32 integer) { } void Op::Add(std::string string) { - Sink(new LiteralString(std::move(string))); + Sink(std::make_unique(std::move(string))); } void Op::Add(const std::vector& ids) { diff --git a/src/op.h b/src/op.h index 91a3e25..5d3b450 100644 --- a/src/op.h +++ b/src/op.h @@ -26,9 +26,7 @@ public: void Write(Stream& stream) const; - void Sink(Operand* operand); - - void Sink(const std::vector& operands_); + void Sink(std::unique_ptr operand); void Add(const Literal& literal);