From 1c552c2c5ce2c0a397d3abbd9e95202f45e3ad8a Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Tue, 4 Oct 2022 20:03:46 +0300 Subject: [PATCH] fix: replace namespace_name_t implementation with vector inheritence --- include/lang/common.hh | 13 ++++--------- src/lang/common.cc | 19 ++++++++++++++----- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/include/lang/common.hh b/include/lang/common.hh index 75d7801..00670ad 100644 --- a/include/lang/common.hh +++ b/include/lang/common.hh @@ -14,21 +14,16 @@ namespace ppc::lang { located_t(Args ...args): T(args...), location(location_t::NONE) { } }; - struct namespace_name_t { - std::vector segments; - - bool is_empty() const { return segments.empty(); } - - auto begin() { return segments.begin(); } - auto end() { return segments.end(); } + struct namespace_name_t : public std::vector { + using base = std::vector; bool operator ==(const namespace_name_t &other) const; - const std::string &operator[](size_t i) const { return segments[i]; } + bool operator !=(const namespace_name_t &other) const; std::string to_string() const; namespace_name_t() { } - namespace_name_t(std::initializer_list segments): segments(segments.begin(), segments.end()) { } + namespace_name_t(std::initializer_list segments): base(segments.begin(), segments.end()) { } }; bool is_identifier_valid(messages::msg_stack_t &msg_stack, ppc::location_t location, const std::string &name); diff --git a/src/lang/common.cc b/src/lang/common.cc index 7b6c9f5..99aa799 100644 --- a/src/lang/common.cc +++ b/src/lang/common.cc @@ -6,19 +6,28 @@ namespace ppc::lang { std::string namespace_name_t::to_string() const { std::stringstream res; - for (size_t i = 0; i < segments.size(); i++) { + for (size_t i = 0; i < size(); i++) { if (i != 0) res << "::"; - res << segments[i]; + res << (*this)[i]; } return res.str(); } bool namespace_name_t::operator==(const namespace_name_t &other) const { - if (other.segments.size() != segments.size()) return false; + if (other.size() != size()) return false; - for (size_t i = 0; i < segments.size(); i++) { - if (other[i] != segments[i]) return false; + for (size_t i = 0; i < size(); i++) { + if (other[i] != (*this)[i]) return false; + } + + return true; + } + bool namespace_name_t::operator!=(const namespace_name_t &other) const { + if (other.size() != size()) return true; + + for (size_t i = 0; i < size(); i++) { + if (other[i] == (*this)[i]) return false; } return true;