Skip to content

Commit 99b7c7c

Browse files
cjh1henryiii
authored andcommitted
Patch nlohmann/json for GCC 4.8
See nlohmann#212 for details
1 parent e184b6e commit 99b7c7c

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

include/nlohmann/detail/macro_scope.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
1111
#endif
1212
#elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
13-
#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40900
13+
#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
1414
#error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
1515
#endif
1616
#endif

include/nlohmann/json.hpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4943,6 +4943,23 @@ class basic_json
49434943
return {it, res.second};
49444944
}
49454945

4946+
/// helper for insertion of an iterator (supports GCC 4.8+)
4947+
template<typename... Args>
4948+
iterator insert_iterator(const_iterator pos, Args&& ... args)
4949+
{
4950+
iterator result(this);
4951+
assert(m_value.array != nullptr);
4952+
4953+
auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
4954+
m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
4955+
result.m_it.array_iterator = m_value.array->begin() + insert_pos;
4956+
4957+
// For GCC 4.9+ only, this could become:
4958+
// result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
4959+
4960+
return result;
4961+
}
4962+
49464963
/*!
49474964
@brief inserts element
49484965
@@ -4977,9 +4994,7 @@ class basic_json
49774994
}
49784995

49794996
// insert to array and return iterator
4980-
iterator result(this);
4981-
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, val);
4982-
return result;
4997+
return insert_iterator(pos, val);
49834998
}
49844999

49855000
JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
@@ -5030,9 +5045,7 @@ class basic_json
50305045
}
50315046

50325047
// insert to array and return iterator
5033-
iterator result(this);
5034-
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
5035-
return result;
5048+
return insert_iterator(pos, cnt, val);
50365049
}
50375050

50385051
JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
@@ -5094,12 +5107,7 @@ class basic_json
50945107
}
50955108

50965109
// insert to array and return iterator
5097-
iterator result(this);
5098-
result.m_it.array_iterator = m_value.array->insert(
5099-
pos.m_it.array_iterator,
5100-
first.m_it.array_iterator,
5101-
last.m_it.array_iterator);
5102-
return result;
5110+
return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
51035111
}
51045112

51055113
/*!
@@ -5141,9 +5149,7 @@ class basic_json
51415149
}
51425150

51435151
// insert to array and return iterator
5144-
iterator result(this);
5145-
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, ilist.begin(), ilist.end());
5146-
return result;
5152+
return insert_iterator(pos, ilist.begin(), ilist.end());
51475153
}
51485154

51495155
/*!

single_include/nlohmann/json.hpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ using json = basic_json<>;
125125
#error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
126126
#endif
127127
#elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
128-
#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40900
128+
#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
129129
#error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
130130
#endif
131131
#endif
@@ -16016,6 +16016,23 @@ class basic_json
1601616016
return {it, res.second};
1601716017
}
1601816018

16019+
/// helper for insertion of an iterator (supports GCC 4.8+)
16020+
template<typename... Args>
16021+
iterator insert_iterator(const_iterator pos, Args&& ... args)
16022+
{
16023+
iterator result(this);
16024+
assert(m_value.array != nullptr);
16025+
16026+
auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
16027+
m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
16028+
result.m_it.array_iterator = m_value.array->begin() + insert_pos;
16029+
16030+
// For GCC 4.9+ only, this could become:
16031+
// result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
16032+
16033+
return result;
16034+
}
16035+
1601916036
/*!
1602016037
@brief inserts element
1602116038

@@ -16050,9 +16067,7 @@ class basic_json
1605016067
}
1605116068

1605216069
// insert to array and return iterator
16053-
iterator result(this);
16054-
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, val);
16055-
return result;
16070+
return insert_iterator(pos, val);
1605616071
}
1605716072

1605816073
JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
@@ -16103,9 +16118,7 @@ class basic_json
1610316118
}
1610416119

1610516120
// insert to array and return iterator
16106-
iterator result(this);
16107-
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
16108-
return result;
16121+
return insert_iterator(pos, cnt, val);
1610916122
}
1611016123

1611116124
JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
@@ -16167,12 +16180,7 @@ class basic_json
1616716180
}
1616816181

1616916182
// insert to array and return iterator
16170-
iterator result(this);
16171-
result.m_it.array_iterator = m_value.array->insert(
16172-
pos.m_it.array_iterator,
16173-
first.m_it.array_iterator,
16174-
last.m_it.array_iterator);
16175-
return result;
16183+
return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
1617616184
}
1617716185

1617816186
/*!
@@ -16214,9 +16222,7 @@ class basic_json
1621416222
}
1621516223

1621616224
// insert to array and return iterator
16217-
iterator result(this);
16218-
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, ilist.begin(), ilist.end());
16219-
return result;
16225+
return insert_iterator(pos, ilist.begin(), ilist.end());
1622016226
}
1622116227

1622216228
/*!

0 commit comments

Comments
 (0)