From b4eeadfd9ba44ad57b26586482b0a66dd9685bfd Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Fri, 1 Nov 2019 06:07:53 -0300 Subject: [PATCH] operand: Use pure virtual functions --- src/literal_number.cpp | 12 ++++++------ src/literal_number.h | 7 ++++--- src/literal_string.cpp | 10 +++++----- src/literal_string.h | 9 +++++---- src/op.cpp | 6 +++--- src/op.h | 7 ++++--- src/operand.cpp | 21 --------------------- src/operand.h | 18 ++++++++++++------ 8 files changed, 39 insertions(+), 51 deletions(-) diff --git a/src/literal_number.cpp b/src/literal_number.cpp index dc17b13..4fafb9e 100644 --- a/src/literal_number.cpp +++ b/src/literal_number.cpp @@ -22,16 +22,16 @@ void LiteralNumber::Fetch(Stream& stream) const { } } -u16 LiteralNumber::GetWordCount() const { +u16 LiteralNumber::GetWordCount() const noexcept { return is_32 ? 1 : 2; } -bool LiteralNumber::operator==(const Operand& other) const { - if (GetType() == other.GetType()) { - const auto& o{static_cast(other)}; - return o.raw == raw && o.is_32 == is_32; +bool LiteralNumber::operator==(const Operand& other) const noexcept { + if (!EqualType(other)) { + return false; } - return false; + const auto& o{static_cast(other)}; + return o.raw == raw && o.is_32 == is_32; } } // namespace Sirit diff --git a/src/literal_number.h b/src/literal_number.h index a72718a..fda0226 100644 --- a/src/literal_number.h +++ b/src/literal_number.h @@ -13,15 +13,16 @@ namespace Sirit { -class LiteralNumber : public Operand { +class LiteralNumber final : public Operand { public: explicit LiteralNumber(u64 raw, bool is_32); ~LiteralNumber() override; void Fetch(Stream& stream) const override; - u16 GetWordCount() const override; - bool operator==(const Operand& other) const override; + u16 GetWordCount() const noexcept override; + + bool operator==(const Operand& other) const noexcept override; template static std::unique_ptr Create(T value) { diff --git a/src/literal_string.cpp b/src/literal_string.cpp index e04dd8b..9b8f062 100644 --- a/src/literal_string.cpp +++ b/src/literal_string.cpp @@ -19,15 +19,15 @@ void LiteralString::Fetch(Stream& stream) const { stream.Write(string); } -u16 LiteralString::GetWordCount() const { +u16 LiteralString::GetWordCount() const noexcept { return static_cast(string.size() / 4 + 1); } -bool LiteralString::operator==(const Operand& other) const { - if (GetType() == other.GetType()) { - return static_cast(other).string == string; +bool LiteralString::operator==(const Operand& other) const noexcept { + if (!EqualType(other)) { + return false; } - return false; + return static_cast(other).string == string; } } // namespace Sirit diff --git a/src/literal_string.h b/src/literal_string.h index c89f533..4cf5287 100644 --- a/src/literal_string.h +++ b/src/literal_string.h @@ -12,18 +12,19 @@ namespace Sirit { -class LiteralString : public Operand { +class LiteralString final : public Operand { public: LiteralString(std::string string); ~LiteralString() override; void Fetch(Stream& stream) const override; - u16 GetWordCount() const override; - bool operator==(const Operand& other) const override; + u16 GetWordCount() const noexcept override; + + bool operator==(const Operand& other) const noexcept override; private: - const std::string string; + std::string string; }; } // namespace Sirit diff --git a/src/op.cpp b/src/op.cpp index 8c387c9..f5b9b75 100644 --- a/src/op.cpp +++ b/src/op.cpp @@ -25,12 +25,12 @@ void Op::Fetch(Stream& stream) const { stream.Write(id.value()); } -u16 Op::GetWordCount() const { +u16 Op::GetWordCount() const noexcept { return 1; } -bool Op::operator==(const Operand& other) const { - if (GetType() != other.GetType()) { +bool Op::operator==(const Operand& other) const noexcept { + if (!EqualType(other)) { return false; } const auto& op = static_cast(other); diff --git a/src/op.h b/src/op.h index 5d3b450..a7413f8 100644 --- a/src/op.h +++ b/src/op.h @@ -14,15 +14,16 @@ namespace Sirit { -class Op : public Operand { +class Op final : public Operand { public: explicit Op(spv::Op opcode, std::optional id = {}, Id result_type = nullptr); ~Op() override; void Fetch(Stream& stream) const override; - u16 GetWordCount() const override; - bool operator==(const Operand& other) const override; + u16 GetWordCount() const noexcept override; + + bool operator==(const Operand& other) const noexcept override; void Write(Stream& stream) const; diff --git a/src/operand.cpp b/src/operand.cpp index e52b4e4..196c64f 100644 --- a/src/operand.cpp +++ b/src/operand.cpp @@ -13,25 +13,4 @@ Operand::Operand(OperandType operand_type) : operand_type{operand_type} {} Operand::~Operand() = default; -void Operand::Fetch([[maybe_unused]] Stream& stream) const { - assert(!"Fetching unimplemented operand"); -} - -u16 Operand::GetWordCount() const { - assert(!"Fetching unimplemented operand"); - return 0; -} - -bool Operand::operator==([[maybe_unused]] const Operand& other) const { - return false; -} - -bool Operand::operator!=(const Operand& other) const { - return !(*this == other); -} - -OperandType Operand::GetType() const { - return operand_type; -} - } // namespace Sirit diff --git a/src/operand.h b/src/operand.h index 4367ce5..64e951b 100644 --- a/src/operand.h +++ b/src/operand.h @@ -17,16 +17,22 @@ public: explicit Operand(OperandType operand_type); virtual ~Operand(); - virtual void Fetch(Stream& stream) const; - virtual u16 GetWordCount() const; + virtual void Fetch(Stream& stream) const = 0; - virtual bool operator==(const Operand& other) const; - bool operator!=(const Operand& other) const; + virtual u16 GetWordCount() const noexcept = 0; - OperandType GetType() const; + virtual bool operator==(const Operand& other) const noexcept = 0; + + bool operator!=(const Operand& other) const noexcept { + return !operator==(other); + } + + bool EqualType(const Operand& other) const noexcept { + return operand_type == other.operand_type; + } private: - OperandType operand_type{}; + OperandType operand_type; }; } // namespace Sirit