Skip to content

move ctors #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 21, 2015
Merged

move ctors #267

merged 2 commits into from
Oct 21, 2015

Conversation

cdunn2001
Copy link
Contributor

  • Add move constructor to Value::CZString
  • Add unit test for Value move constructor
  • Allow includer to specify in advance the value for
    JSON_HAS_RVALUE_REFERENCES

rebased #264.

@cdunn2001
Copy link
Contributor Author

We still have to consider what to do for the older branch. We maintain strict binary-compatibility with ancient 0.6.0-rc2 on that branch. Adding a method is fine. I think even adding an rvalue method is fine. So maybe there are no issues.

@lanzkron
Copy link
Contributor

Thanks @cdunn2001 !

I see you also fixed my use of nullptr, I only realized it after my PR.

#endif
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
#define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message)))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I meant to ask: Why was JSON_DEPRECATED dropped for GNUC?

Copy link
Contributor

Choose a reason for hiding this comment

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

Did I do that? If so it was in error, sorry.

On 28 April 2015 at 10:51, Christopher Dunn [email protected]
wrote:

In include/json/config.h
#267 (comment)
:

#endif
-#elif defined(GNUC) && (GNUC > 4 || (GNUC == 4 && GNUC_MINOR >= 5))
-#define JSONCPP_DEPRECATED(message) attribute ((deprecated(message)))

Oh, I meant to ask: Why was JSON_DEPRECATED dropped for GNUC?


Reply to this email directly or view it on GitHub
https://github.com/open-source-parsers/jsoncpp/pull/267/files#r29220618.

@cdunn2001 cdunn2001 force-pushed the moveSemantics branch 3 times, most recently from b68e7cf to 643cd68 Compare April 28, 2015 08:27
@cdunn2001
Copy link
Contributor Author

Asked about backward-compatibility on stackoverflow.

@lanzkron
Copy link
Contributor

Answered on stackoverflow (mostly to get feedback from the community as to whether my understanding is correct).

@akrieger
Copy link

Can we add move assignment operators as well, so that we can efficiently do

Json::Value root;
root["key"] = functionThatGeneratesJson();

@cdunn2001
Copy link
Contributor Author

I agree, as in @BillyDonahue comments at #264:

class Value {
    ...
    Value(const Value&);
    Value(Value&&) noexcept;
    Value& operator=(const Value&);
    Value& operator=(Value&&) noexcept;
    void swap(Value& v) noexcept { std::swap(*this, v); }
    friend void swap(Value& a, Value& b) noexcept { a.swap(b); }
};

We also need a macro for noexcept, for the compilers which do not support. Personally, I'm fine with

#if dumb_compiler
#define noexcept
#endif

@akrieger
Copy link

I like the very political references to 'the compilers which do not support'.

I actually dug into it and it seems the existing operator= implementations exhibit move semantics, as of 45cd949. Looks like that can be reverted to the old copy semantics with the addition of proper move operators. Regardless of the philosophical motivation behind making copy assignment actually be move assignment.

@BillyDonahue
Copy link
Contributor

I think the direct #define of a keyword is asking for trouble, especially in a header. Let's be conservative and use JSON_NOEXCEPT. On such platforms, I'd imagine there are plenty of libs that might want to #define noexcept and we'd be conflicting if we did the same.

#define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message)))
#elif defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
#define JSONCPP_DEPRECATED(message) __attribute__((__deprecated__))
#if defined(_MSC_VER) // MSVC
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the reindent be its own PR? It's hard to separate cosmetics from substance as-is.
It would be nice to emphasize the substantive changes with concise PRs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I'll try re-indent first, and rebase this onto that...

@cdunn2001
Copy link
Contributor Author

You're probably right; not good in a header. JSON_NOEXCEPT then.

I like the very political references to 'the compilers which do not support'.

Yes! I have strong opinions.

@cdunn2001
Copy link
Contributor Author

@BillyDonahue, would you pull my code from this branch and add whatever other operators we need? I will take of the JSON_NOEXCEPT macro.

@lanzkron
Copy link
Contributor

@akrieger regarding move assignment operator:
Currently the assignment operator uses copy and swap, with the addition of a move constructor it would be able to use move and swap for rvalues which is almost as efficient as writing a special move assignment operator.

If you have a look at Json::Value's copy constructor you'll see that it's rather complex and in my opinion duplicating this logic in a move constructor and assignment operator isn't worth it for the potential improvements.

@cdunn2001
Copy link
Contributor Author

@lanzkron, Do we need any extra methods, beyond what are already in this pull-request? Do we need the move-ctor for CZString at all? I'm not sure where we are.

@BillyDonahue
Copy link
Contributor

@cdunn : I'm still a github noob, but I'm figuring out what you need.
I'm a little confused about what that branch represents.

I'd like to ask about compatibility again. We are not strictly adding new
functions here. My ideal suggestion would be that we'd be REMOVING the
by-value "operator=(Value)" assignment and ADDING the by-cref and by-rref
"operator=(const Value&)" and "operator=(Value&&)" assignments.

Old code expecting to find "operator=(Value)" in libjsoncpp.so wouldn't
find it anymore. I'm not saying this is a problem. I haven't been following
the branching plans or compatibility requirements closely enough. I just
want to make sure this point about the disappearing by-value overload is
understood.

On Wed, Apr 29, 2015 at 12:16 AM, Christopher Dunn <[email protected]

wrote:

@BillyDonahue https://github.com/BillyDonahue, would you pull my code
from this branch https://github.com/cdunn2001/jsoncpp/tree/moveSemantics
and add whatever other operators we need? I will take of the JSON_NOEXCEPT
macro.


Reply to this email directly or view it on GitHub
#267 (comment)
.

@lanzkron
Copy link
Contributor

@cdunn2001
The move-ctor for CZString is good to have (it also helps when assigning from an rvalue due to the copy and swap assignment operator in CZString).

I'm also new to GitHub, who makes the final call? In my opinion having a move-ctor and keeping the copy/swap assignment operator is good enough. In which case I think we're good to go.

Keep in mind that my implementation of Value(Value&&) depends on swap. If we do decide to implement move ctor/op= from scratch then we'll have to duplicate the logic of the copy constructor (and keep them synced in future changes).

I don't currently have the capacity to do this. If that's what we want then we can dump my changes and wait for someone else to code support for move semantics.

@cdunn2001
Copy link
Contributor Author

@BillyDonahue, we could remove CZString::operator=(), because the likelihood that anyone uses it directly is miniscule. Technically, we shouldn't change Value::operator=(), but I don't think anyone currently relies on binary-compatibility for 1.0, so it's ok. That method in particular is a welcome change because that is already different in the 0.y.z branch. See 1c4f6a2:

-  Value& operator=(Value other);
+  Value &operator=(const Value &other);

We can remove anything in a future 2.0.0 release. At some point, we should move development to a new alpha branch. That's a discussion for another day.

@lanzkron, there is no final call. We can always add new features. It's better to add less, because it's hard to remove features.

@akrieger
Copy link

@lanzkron ah, yes, you're right, it is a copy and swap.

@lanzkron
Copy link
Contributor

@cdunn2001 I assume you mean replacing CZString's operator= with lvalue and rvalue versions, not remove it completely

@thelema
Copy link

thelema commented Oct 19, 2015

Is there any chance of this being merged? It looks a little dead. I'm hoping to have move constructors to greatly improve performance of building a very large json object in pieces, and I found this open pull request.

@cdunn2001
Copy link
Contributor Author

I'm concerned about removing symbols. I don't mind removing them from CZString because nobody who cares about binary compatibility would use it. But I don't think we can remove symbols from Value. I think 1.0 is now used in Debian, e.g.

I've just rebased this. Let's take another look now.

* Add move constructor to Value::CZString
* Add unit test for Value move constructor
* Allow includer to specify in advance the value for
JSON_HAS_RVALUE_REFERENCES
adjustments, based on comments in PR.
@cdunn2001
Copy link
Contributor Author

Ok, I'd be fine with merging this, if there are no objections. I'll wait a couple days...

@BillyDonahue
Copy link
Contributor

Looks good. I do wish we could have the move assignment operations.
Is there an advanced branch for that sort of thing free from the binary compatibility concerns?

@cdunn2001
Copy link
Contributor Author

Soon. The old branches are stable enough that we can soon consider master as a new pre-release for 2.0.

cdunn2001 added a commit that referenced this pull request Oct 21, 2015
@cdunn2001 cdunn2001 merged commit 34bdbb5 into open-source-parsers:master Oct 21, 2015
@cdunn2001 cdunn2001 deleted the moveSemantics branch October 21, 2015 06:14
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.

5 participants