diff options
author | miloyip <miloyip@gmail.com> | 2015-05-02 16:52:49 +0300 |
---|---|---|
committer | miloyip <miloyip@gmail.com> | 2015-05-02 16:52:49 +0300 |
commit | 32b45f6e6d15b229ea1907e9569e4792bff6e207 (patch) | |
tree | e58a1ffa53cc7b73a04486e6ce0766b01fa0ced5 /include/rapidjson/pointer.h | |
parent | 2ddbd09031b90765e31fc31a7607e3e0248a17d1 (diff) |
Add GetWithDefault() overloads
Diffstat (limited to 'include/rapidjson/pointer.h')
-rw-r--r-- | include/rapidjson/pointer.h | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h index bc2795fe..16d2e602 100644 --- a/include/rapidjson/pointer.h +++ b/include/rapidjson/pointer.h @@ -248,6 +248,28 @@ public: return v; } + ValueType& GetWithDefault(ValueType& root, GenericStringRef<Ch> defaultValue, typename ValueType::AllocatorType& allocator) const { + ValueType v(defaultValue); + return GetWithDefault(root, v, allocator); + } + + ValueType& GetWithDefault(ValueType& root, const Ch* defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + Value& v = Create(root, allocator, &alreadyExist); + if (!alreadyExist) { + Value clone(defaultValue, allocator); // This has overhead, so do it inside if. + v = clone; + } + return v; + } + + template <typename T> + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (ValueType&)) + GetWithDefault(ValueType& root, T defaultValue, typename ValueType::AllocatorType& allocator) const { + ValueType v(defaultValue); + return GetWithDefault(root, v, allocator); + } + // Move semantics, create parents if non-exist ValueType& Set(ValueType& root, ValueType& value, typename ValueType::AllocatorType& allocator) const { return Create(root, allocator) = value; |