op: Replace owning star pointers with unique_ptr
This commit is contained in:
parent
ef47087d88
commit
de91660f3c
3 changed files with 10 additions and 19 deletions
|
@ -7,6 +7,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <typeindex>
|
||||
#include "operand.h"
|
||||
#include "stream.h"
|
||||
|
@ -24,10 +25,10 @@ public:
|
|||
bool operator==(const Operand& other) const override;
|
||||
|
||||
template <typename T>
|
||||
static LiteralNumber* Create(T value) {
|
||||
static std::unique_ptr<LiteralNumber> 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<LiteralNumber>(std::type_index(typeid(T)));
|
||||
number->is_32 = sizeof(T) == 4;
|
||||
std::memcpy(&number->raw, &value, sizeof(T));
|
||||
return number;
|
||||
|
|
20
src/op.cpp
20
src/op.cpp
|
@ -62,20 +62,13 @@ void Op::Write(Stream& stream) const {
|
|||
}
|
||||
}
|
||||
|
||||
void Op::Sink(Operand* operand) {
|
||||
assert(operand);
|
||||
Add(static_cast<const Operand*>(operand));
|
||||
operand_store.push_back(std::unique_ptr<Operand>(operand));
|
||||
}
|
||||
|
||||
void Op::Sink(const std::vector<Operand*>& operands_) {
|
||||
for (auto* operand : operands_) {
|
||||
Sink(operand);
|
||||
}
|
||||
void Op::Sink(std::unique_ptr<Operand> operand) {
|
||||
Add(static_cast<const Operand*>(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<Literal>& 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<LiteralString>(std::move(string)));
|
||||
}
|
||||
|
||||
void Op::Add(const std::vector<Id>& ids) {
|
||||
|
|
4
src/op.h
4
src/op.h
|
@ -26,9 +26,7 @@ public:
|
|||
|
||||
void Write(Stream& stream) const;
|
||||
|
||||
void Sink(Operand* operand);
|
||||
|
||||
void Sink(const std::vector<Operand*>& operands_);
|
||||
void Sink(std::unique_ptr<Operand> operand);
|
||||
|
||||
void Add(const Literal& literal);
|
||||
|
||||
|
|
Loading…
Reference in a new issue