From aa789f47d5f2899f489cc1a1288f96280d6da9db Mon Sep 17 00:00:00 2001 From: Chris Harris Date: Thu, 20 Sep 2018 11:19:33 -0400 Subject: [PATCH] Patch nlohmann/json for GCC 4.8 See https://github.com/nlohmann/json/pull/212 for details --- include/nlohmann/detail/macro_scope.hpp | 2 +- include/nlohmann/json.hpp | 36 ++++++++++++++----------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index a5b6101e83..745a0c4d7f 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -10,7 +10,7 @@ #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" #endif #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER)) - #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40900 + #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800 #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers" #endif #endif diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 1f39e3ec76..30e12e0e34 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -4943,6 +4943,23 @@ class basic_json return {it, res.second}; } + /// helper for insertion of an iterator (supports GCC 4.8+) + template + iterator insert_iterator(const_iterator pos, Args&& ... args) + { + iterator result(this); + assert(m_value.array != nullptr); + + auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator); + m_value.array->insert(pos.m_it.array_iterator, std::forward(args)...); + result.m_it.array_iterator = m_value.array->begin() + insert_pos; + + // For GCC 4.9+ only, this could become: + // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val); + + return result; + } + /*! @brief inserts element @@ -4977,9 +4994,7 @@ class basic_json } // insert to array and return iterator - iterator result(this); - result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, val); - return result; + return insert_iterator(pos, val); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); @@ -5030,9 +5045,7 @@ class basic_json } // insert to array and return iterator - iterator result(this); - result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val); - return result; + return insert_iterator(pos, cnt, val); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); @@ -5094,12 +5107,7 @@ class basic_json } // insert to array and return iterator - iterator result(this); - result.m_it.array_iterator = m_value.array->insert( - pos.m_it.array_iterator, - first.m_it.array_iterator, - last.m_it.array_iterator); - return result; + return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); } /*! @@ -5141,9 +5149,7 @@ class basic_json } // insert to array and return iterator - iterator result(this); - result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, ilist.begin(), ilist.end()); - return result; + return insert_iterator(pos, ilist.begin(), ilist.end()); } /*!