From db7f81e202f69e7181fc241e2b46ce9e6c0031f2 Mon Sep 17 00:00:00 2001 From: jcarreras Date: Wed, 13 Oct 2021 15:48:36 +0200 Subject: [PATCH 1/3] Added new methods to allow range based loop --- JSONAdapterInterface/IJSONValue.h | 35 ++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/JSONAdapterInterface/IJSONValue.h b/JSONAdapterInterface/IJSONValue.h index ee43137..79edeb1 100644 --- a/JSONAdapterInterface/IJSONValue.h +++ b/JSONAdapterInterface/IJSONValue.h @@ -4,7 +4,6 @@ #include #include - namespace systelab { namespace json { enum Type @@ -22,7 +21,13 @@ namespace systelab { namespace json { class IJSONValue { + template class ArrayIterator_; + public: + + using ArrayIterator = ArrayIterator_< std::vector>::iterator, IJSONValue&>; + using ArrayConstIterator = ArrayIterator_< std::vector>::const_iterator, const IJSONValue&>; + virtual ~IJSONValue() {}; virtual Type getType() const = 0; @@ -69,6 +74,11 @@ namespace systelab { namespace json { // Only for array values virtual unsigned int getArrayValueCount() const = 0; virtual IJSONValue& getArrayValue(unsigned int) const = 0; + + virtual ArrayIterator begin() = 0; + virtual ArrayIterator end() = 0; + virtual ArrayConstIterator begin() const = 0; + virtual ArrayConstIterator end() const = 0; virtual void addArrayValue(std::unique_ptr) = 0; virtual void clearArray() = 0; @@ -80,6 +90,29 @@ namespace systelab { namespace json { // Factory methods virtual std::unique_ptr buildValue(Type) const = 0; virtual std::unique_ptr buildDocument() const = 0; + + private: + + // Array iterator for range-loop + template + class ArrayIterator_ : public BaseIterator_ + { + public: + + virtual ~ArrayIterator_() = default; + + ArrayIterator_() = delete; + + ArrayIterator_(const BaseIterator_& it) : BaseIterator_(it) {}; + + Ref_ operator*() const + { + const BaseIterator_ it = *this; + const BaseIterator_::value_type& p = *it; + Ref_ r = *p; + return r; + } + }; }; }} From b1dc976fd85f8eeea5dfe4e24bad2f1963411d15 Mon Sep 17 00:00:00 2001 From: jcarreras Date: Wed, 13 Oct 2021 15:49:00 +0200 Subject: [PATCH 2/3] Added new range based loop methods to mock --- JSONAdapterTestUtilities/Mocks/MockJSONValue.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/JSONAdapterTestUtilities/Mocks/MockJSONValue.h b/JSONAdapterTestUtilities/Mocks/MockJSONValue.h index a910e05..69ea89a 100644 --- a/JSONAdapterTestUtilities/Mocks/MockJSONValue.h +++ b/JSONAdapterTestUtilities/Mocks/MockJSONValue.h @@ -56,6 +56,11 @@ namespace systelab { namespace json { namespace test_utility { MOCK_CONST_METHOD0(getArrayValueCount, unsigned int()); MOCK_CONST_METHOD1(getArrayValue, IJSONValue&(unsigned int)); + + MOCK_METHOD0(begin, ArrayIterator()); + MOCK_METHOD0(end, ArrayIterator()); + MOCK_CONST_METHOD0(begin, ArrayConstIterator()); + MOCK_CONST_METHOD0(end, ArrayConstIterator()); MOCK_CONST_METHOD1(addArrayValueProxy, void(IJSONValue*)); void addArrayValue(std::unique_ptr value) From cad358cd9e9336a06ff71f10ae8987c44289413d Mon Sep 17 00:00:00 2001 From: jcarreras Date: Wed, 13 Oct 2021 18:43:44 +0200 Subject: [PATCH 3/3] Update IJSONValue.h Redesign of ArrayIterator class for range base loops, in order to lean on the existing virtual method IJSONValue::getArrayValue(), avoiding to mention vector>. --- JSONAdapterInterface/IJSONValue.h | 34 +++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/JSONAdapterInterface/IJSONValue.h b/JSONAdapterInterface/IJSONValue.h index 79edeb1..c10016f 100644 --- a/JSONAdapterInterface/IJSONValue.h +++ b/JSONAdapterInterface/IJSONValue.h @@ -21,12 +21,12 @@ namespace systelab { namespace json { class IJSONValue { - template class ArrayIterator_; + template class ArrayIterator_; public: - using ArrayIterator = ArrayIterator_< std::vector>::iterator, IJSONValue&>; - using ArrayConstIterator = ArrayIterator_< std::vector>::const_iterator, const IJSONValue&>; + using ArrayIterator = ArrayIterator_; + using ArrayConstIterator = ArrayIterator_; virtual ~IJSONValue() {}; @@ -94,24 +94,36 @@ namespace systelab { namespace json { private: // Array iterator for range-loop - template - class ArrayIterator_ : public BaseIterator_ + template + class ArrayIterator_ { public: - virtual ~ArrayIterator_() = default; + ~ArrayIterator_() = default; ArrayIterator_() = delete; - ArrayIterator_(const BaseIterator_& it) : BaseIterator_(it) {}; + ArrayIterator_(const IJSONValue& value, int distance) : pVal_(&value), distance_(distance) {}; Ref_ operator*() const { - const BaseIterator_ it = *this; - const BaseIterator_::value_type& p = *it; - Ref_ r = *p; - return r; + return (Ref_)pVal_->getArrayValue(distance_); } + + void operator++() + { + ++distance_; + } + + bool operator!=(const ArrayIterator_& it) + { + return distance_ != it.distance_; + } + + private: + + const IJSONValue* pVal_; + int distance_; }; };