Skip to content

Added support for range based loop - #2

Open
joancarreras wants to merge 3 commits into
systelab:masterfrom
joancarreras:range-based-loop
Open

Added support for range based loop#2
joancarreras wants to merge 3 commits into
systelab:masterfrom
joancarreras:range-based-loop

Conversation

@joancarreras

Copy link
Copy Markdown
  • TBD

@lurume84 lurume84 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand this iterator inheritance is the whole point of the implementation. Since ArrayIterator_ class does not have members there should not be any memory leak because of parent's non-virtual destructor, so I'm ok with it.

However, I would suggest to address the two other comments

Comment thread JSONAdapterInterface/IJSONValue.h Outdated

// Array iterator for range-loop
template <typename BaseIterator_, typename Ref_>
class ArrayIterator_ : public BaseIterator_

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is normally considered bad practice to inherit from STL (in this case vector's iterator) as they do not declare dtor virtual on purpose

@joancarreras joancarreras Oct 14, 2021

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation of the whole class has changed in order to be unrelated to the vector<unique_ptr> (because it reveals the current way to store values contained in the JSONValue implementation, something inherent in the implementation but not in the interface)

Comment thread JSONAdapterInterface/IJSONValue.h Outdated

Ref_ operator*() const
{
const BaseIterator_ it = *this;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

original implementation does this function in one line

reference operator*() const
{ // return designated object
return ((reference)**(_Mybase *)this);
}

I recommend condensing it also in one line to reduce code

Comment thread JSONAdapterInterface/IJSONValue.h Outdated
{
public:

virtual ~ArrayIterator_() = default;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed as this is a final class, adding this virtual method implies allocating vtable unnecessarily

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree, thanks! (as a previous approach I had considered making this class as an 'IArrayIterator' interface; because of this the 'virtual' in the destructor has remained here without being necessary now)

Redesign of ArrayIterator class for range base loops, in order to lean on the existing virtual method IJSONValue::getArrayValue(), avoiding to mention vector<unique_ptr<IJSONValue>>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants