From a5023c4fa4bcec202b4727db09e7faf59a9e7add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Thu, 19 Sep 2024 00:30:05 +0200 Subject: [PATCH] =?UTF-8?q?Another=20bugfix=20in=20rfl::Object:=20Make=20s?= =?UTF-8?q?ure=20that=20access=20in=20the=20wrong=20ord=E2=80=A6=20(#197)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rfl/Object.hpp | 83 ++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/include/rfl/Object.hpp b/include/rfl/Object.hpp index 684837ad..c6e5aa4a 100644 --- a/include/rfl/Object.hpp +++ b/include/rfl/Object.hpp @@ -144,17 +144,9 @@ class Object { /// Returns the element signified by the key or creates a new one. T& operator[](const std::string& _key) { - for (auto it = it_; it != end(); ++it) { - if (it->first == _key) { - it_ = it + 1; - return it->second; - } - } - for (auto it = begin(); it != it_; ++it) { - if (it->first == _key) { - it_ = it + 1; - return it->second; - } + const auto it = find(_key); + if (it != end()) { + return it->second; } data_.emplace_back(std::make_pair(_key, T())); it_ = data_.begin(); @@ -163,17 +155,9 @@ class Object { /// Returns the element signified by the key or creates a new one. T& operator[](std::string&& _key) { - for (auto it = it_; it != end(); ++it) { - if (it->first == _key) { - it_ = it + 1; - return it->second; - } - } - for (auto it = begin(); it != it_; ++it) { - if (it->first == _key) { - it_ = it + 1; - return it->second; - } + const auto it = find(_key); + if (it != end()) { + return it->second; } data_.emplace_back(std::make_pair(std::move(_key), T())); it_ = data_.begin(); @@ -188,35 +172,62 @@ class Object { /// Returns the element signified by the key or throws an exception. T& at(const std::string& _key) { - for (auto it = it_; it != end(); ++it) { - if (it->first == _key) { - it_ = it + 1; - return it->second; - } + const auto it = find(_key); + if (it == end()) { + throw std::runtime_error("Key named '" + _key + "' not found."); } - throw std::runtime_error("Key named '" + _key + "' not found."); + return it->second; } /// Returns the element signified by the key or throws an exception. const T& at(const std::string& _key) const { - for (auto it = it_; it != end(); ++it) { - if (it->first == _key) { - it_ = it + 1; - return it->second; - } + const auto it = find(_key); + if (it == end()) { + throw std::runtime_error("Key named '" + _key + "' not found."); } - throw std::runtime_error("Key named '" + _key + "' not found."); + return it->second; } /// Returns a result wrapping the element signified by the key. Result get(const std::string& _key) const noexcept { + const auto it = find(_key); + if (it == end()) { + return Error("Key named '" + _key + "' not found."); + } + return it->second; + } + + private: + iterator find(const std::string& _key) { for (auto it = it_; it != end(); ++it) { if (it->first == _key) { it_ = it + 1; - return it->second; + return it; } } - return Error("Key named '" + _key + "' not found."); + for (auto it = begin(); it != it_; ++it) { + if (it->first == _key) { + it_ = it + 1; + return it; + } + } + return end(); + } + + const_iterator find(const std::string& _key) const { + for (auto it = it_; it != end(); ++it) { + if (it->first == _key) { + it_ = it + 1; + return it; + } + } + for (auto it = begin(); it != it_; ++it) { + if (it->first == _key) { + it_ = it + 1; + return it; + } + } + return end(); } private: