Overload on dynamic object reference type.
authorNick Terrell <terrelln@fb.com>
Fri, 31 Jul 2015 16:57:00 +0000 (09:57 -0700)
committerfacebook-github-bot-4 <folly-bot@fb.com>
Fri, 31 Jul 2015 17:22:29 +0000 (10:22 -0700)
commit117385fb60e5acff59f2182fce6a259cf36f4820
treeffe9496b233847d5cc29ffb45142a7d898f2f4ec
parentd615b7767ce982ca11fac9eba1b20cda825e73b3
Overload on dynamic object reference type.

Summary: There are a bunch of methods in `folly::dynamic` that return a
reference to a sub-object, e.g. `getString()`.
These methods are a bit unsafe because it allows you to write code with dangling
references when the `folly::dynamic` object is an rvalue:

  auto& obj = parse_json(some_string).at("key");

However, the bigger win is that when we have an rvalue `folly::dynamic`, such as
returned by `getDefault(...)`, we can move the sub-object out:

  auto obj = parse_json(some_string).at("key");
  auto str = obj.getDefault("another-key", "").getString();

In the first line, the subobject is copied out when it could be safely moved out.
In the second line `getDefault(...)` copies the dynamic sub-object out, and then
`getString()` copies the string out, when the string could be moved.

Also in the case of `getDefault()` being called on a rvalue, we can move the
sub-object out.

Reviewed By: @marcinpe

Differential Revision: D2267588
folly/dynamic-inl.h
folly/dynamic.cpp
folly/dynamic.h
folly/test/DynamicTest.cpp