From 85d54c9bcb7048a9e5cddad5b9511f5cf80db63b Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 20 Aug 2022 02:28:56 +0200 Subject: [PATCH] Emulator: Prefer string_view over const char* --- src/emu.cpp | 8 ++++---- src/emu.h | 14 +++++++------- src/processing-unit.h | 5 +++-- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/emu.cpp b/src/emu.cpp index fed9ff3..7b160fc 100644 --- a/src/emu.cpp +++ b/src/emu.cpp @@ -29,23 +29,23 @@ void Emu::update() } } -void Emu::addProcessingUnit(const char* name, ProcessingUnit* processing_unit) +void Emu::addProcessingUnit(std::string_view name, ProcessingUnit* processing_unit) { m_processing_units.emplace(name, processing_unit); } -void Emu::addMemorySpace(const char* name, uint32_t size) +void Emu::addMemorySpace(std::string_view name, uint32_t size) { std::vector memory(size); m_memory_spaces.emplace(name, memory); } -void Emu::writeMemory(const char* memory_space, uint32_t location, uint32_t value) +void Emu::writeMemory(std::string_view memory_space, uint32_t location, uint32_t value) { m_memory_spaces[memory_space][location] = value; } -uint32_t Emu::readMemory(const char* memory_space, uint32_t location) +uint32_t Emu::readMemory(std::string_view memory_space, uint32_t location) { return m_memory_spaces[memory_space][location]; } diff --git a/src/emu.h b/src/emu.h index 9d0ccfe..02d19a2 100644 --- a/src/emu.h +++ b/src/emu.h @@ -17,15 +17,15 @@ public: void update(); - void addProcessingUnit(const char* name, ProcessingUnit* processing_unit); - void addMemorySpace(const char* name, uint32_t size); + void addProcessingUnit(std::string_view name, ProcessingUnit* processing_unit); + void addMemorySpace(std::string_view name, uint32_t size); - void writeMemory(const char* memory_space, uint32_t location, uint32_t value); - uint32_t readMemory(const char* memory_space, uint32_t location); + void writeMemory(std::string_view memory_space, uint32_t location, uint32_t value); + uint32_t readMemory(std::string_view memory_space, uint32_t location); // ------------------------------------- - ProcessingUnit* processingUnit(const char* name) const { return m_processing_units.at(name); } + ProcessingUnit* processingUnit(std::string_view name) const { return m_processing_units.at(name); } private: uint32_t m_frequency { 0 }; @@ -36,8 +36,8 @@ private: ruc::Timer m_timer; - std::unordered_map m_processing_units; - std::unordered_map> m_memory_spaces; + std::unordered_map m_processing_units; + std::unordered_map> m_memory_spaces; std::string_view m_bootrom; }; diff --git a/src/processing-unit.h b/src/processing-unit.h index 24435fd..094d4ab 100644 --- a/src/processing-unit.h +++ b/src/processing-unit.h @@ -8,6 +8,7 @@ #pragma once #include // uint32_t +#include #include class ProcessingUnit { @@ -20,9 +21,9 @@ public: // ------------------------------------- uint32_t frequency() const { return m_frequency; }; - uint32_t* sharedRegister(const char* shared_register) const { return m_shared_registers.at(shared_register); } + uint32_t* sharedRegister(std::string_view shared_register) const { return m_shared_registers.at(shared_register); } protected: uint32_t m_frequency { 0 }; - std::unordered_map m_shared_registers; + std::unordered_map m_shared_registers; };