diff --git a/JSONAdapterInterface/IJSONValue.h b/JSONAdapterInterface/IJSONValue.h index ee43137..c10016f 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_; + using ArrayConstIterator = ArrayIterator_; + 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,41 @@ 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: + + ~ArrayIterator_() = default; + + ArrayIterator_() = delete; + + ArrayIterator_(const IJSONValue& value, int distance) : pVal_(&value), distance_(distance) {}; + + Ref_ operator*() const + { + return (Ref_)pVal_->getArrayValue(distance_); + } + + void operator++() + { + ++distance_; + } + + bool operator!=(const ArrayIterator_& it) + { + return distance_ != it.distance_; + } + + private: + + const IJSONValue* pVal_; + int distance_; + }; }; }} 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)