Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Oct 3, 2015
1 parent 2550d29 commit 57de1d6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 53 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ I deeply appreciate the help of the following people.
- [kepkin](https://github.com/kepkin) patiently pushed forward the support for Microsoft Visual studio.
- [gregmarr](https://github.com/gregmarr) simplified the implementation of reverse iterators.
- [Caio Luppi](https://github.com/caiovlp) fixed a bug in the Unicode handling.
- [dariomt](https://github.com/dariomt) fixed some typos in the examples.
- [Daniel Frey](https://github.com/d-frey) cleaned up some pointers and implemented exception-safe memory allocation.
- [Colin Hirsch](https://github.com/ColinH) took care of a small namespace issue.
- [Huu Nguyen](https://github.com/whoshuu) correct a variable name in the documentation.
- [Silverweed](https://github.com/silverweed) overloaded `parse()` to accept an rvalue reference.

Thanks a lot for helping out!

Expand Down
2 changes: 1 addition & 1 deletion doc/examples/get__PointerType.link
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/fdsMCtY67vLJrlzv"><b>online</b></a>
<a target="_blank" href="http://melpon.org/wandbox/permlink/OQ6W9gVosCWDgJHS"><b>online</b></a>
2 changes: 1 addition & 1 deletion doc/examples/get_ptr.link
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/Ka7Efl5Jo2FTDy69"><b>online</b></a>
<a target="_blank" href="http://melpon.org/wandbox/permlink/Onaz3FY3sow5TEwH"><b>online</b></a>
2 changes: 1 addition & 1 deletion doc/examples/object.link
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/kQKYwNmQRJAJhCTe"><b>online</b></a>
<a target="_blank" href="http://melpon.org/wandbox/permlink/wArBhgANa8p39v92"><b>online</b></a>
84 changes: 36 additions & 48 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,20 @@ class basic_json


private:
/// helper for exception-safe object creation
template<typename T, typename... Args>
static T* create( Args&& ... args )
{
AllocatorType<T> alloc;
auto deleter = [&](T * object)
{
alloc.deallocate(object, 1);
};
std::unique_ptr<T, decltype(deleter)> object(alloc.allocate(1), deleter);
alloc.construct(object.get(), std::forward<Args>(args)...);
return object.release();
}

////////////////////////
// JSON value storage //
////////////////////////
Expand Down Expand Up @@ -625,25 +639,19 @@ class basic_json

case (value_t::object):
{
AllocatorType<object_t> alloc;
object = alloc.allocate(1);
alloc.construct(object);
object = create<object_t>();
break;
}

case (value_t::array):
{
AllocatorType<array_t> alloc;
array = alloc.allocate(1);
alloc.construct(array);
array = create<array_t>();
break;
}

case (value_t::string):
{
AllocatorType<string_t> alloc;
string = alloc.allocate(1);
alloc.construct(string, "");
string = create<string_t>("");
break;
}

Expand All @@ -670,25 +678,19 @@ class basic_json
/// constructor for strings
json_value(const string_t& value)
{
AllocatorType<string_t> alloc;
string = alloc.allocate(1);
alloc.construct(string, value);
string = create<string_t>(value);
}

/// constructor for objects
json_value(const object_t& value)
{
AllocatorType<object_t> alloc;
object = alloc.allocate(1);
alloc.construct(object, value);
object = create<object_t>(value);
}

/// constructor for arrays
json_value(const array_t& value)
{
AllocatorType<array_t> alloc;
array = alloc.allocate(1);
alloc.construct(array, value);
array = create<array_t>(value);
}
};

Expand Down Expand Up @@ -892,11 +894,9 @@ class basic_json
basic_json(const CompatibleObjectType& value)
: m_type(value_t::object)
{
AllocatorType<object_t> alloc;
m_value.object = alloc.allocate(1);
using std::begin;
using std::end;
alloc.construct(m_value.object, begin(value), end(value));
m_value.object = create<object_t>(begin(value), end(value));
}

/*!
Expand Down Expand Up @@ -953,11 +953,9 @@ class basic_json
basic_json(const CompatibleArrayType& value)
: m_type(value_t::array)
{
AllocatorType<array_t> alloc;
m_value.array = alloc.allocate(1);
using std::begin;
using std::end;
alloc.construct(m_value.array, begin(value), end(value));
m_value.array = create<array_t>(begin(value), end(value));
}

/*!
Expand Down Expand Up @@ -1315,9 +1313,7 @@ class basic_json
{
// the initializer list describes an array -> create array
m_type = value_t::array;
AllocatorType<array_t> alloc;
m_value.array = alloc.allocate(1);
alloc.construct(m_value.array, std::move(init));
m_value.array = create<array_t>(std::move(init));
}
}

Expand Down Expand Up @@ -1416,9 +1412,7 @@ class basic_json
basic_json(size_type count, const basic_json& value)
: m_type(value_t::array)
{
AllocatorType<array_t> alloc;
m_value.array = alloc.allocate(1);
alloc.construct(m_value.array, count, value);
m_value.array = create<array_t>(count, value);
}

/*!
Expand Down Expand Up @@ -1514,17 +1508,13 @@ class basic_json

case value_t::object:
{
AllocatorType<object_t> alloc;
m_value.object = alloc.allocate(1);
alloc.construct(m_value.object, first.m_it.object_iterator, last.m_it.object_iterator);
m_value.object = create<object_t>(first.m_it.object_iterator, last.m_it.object_iterator);
break;
}

case value_t::array:
{
AllocatorType<array_t> alloc;
m_value.array = alloc.allocate(1);
alloc.construct(m_value.array, first.m_it.array_iterator, last.m_it.array_iterator);
m_value.array = create<array_t>(first.m_it.array_iterator, last.m_it.array_iterator);
break;
}

Expand Down Expand Up @@ -1658,8 +1648,8 @@ class basic_json
)
{
using std::swap;
std::swap(m_type, other.m_type);
std::swap(m_value, other.m_value);
swap(m_type, other.m_type);
swap(m_value, other.m_value);
return *this;
}

Expand All @@ -1683,7 +1673,6 @@ class basic_json
AllocatorType<object_t> alloc;
alloc.destroy(m_value.object);
alloc.deallocate(m_value.object, 1);
m_value.object = nullptr;
break;
}

Expand All @@ -1692,7 +1681,6 @@ class basic_json
AllocatorType<array_t> alloc;
alloc.destroy(m_value.array);
alloc.deallocate(m_value.array, 1);
m_value.array = nullptr;
break;
}

Expand All @@ -1701,7 +1689,6 @@ class basic_json
AllocatorType<string_t> alloc;
alloc.destroy(m_value.string);
alloc.deallocate(m_value.string, 1);
m_value.string = nullptr;
break;
}

Expand Down Expand Up @@ -2598,9 +2585,7 @@ class basic_json
if (m_type == value_t::null)
{
m_type = value_t::array;
AllocatorType<array_t> alloc;
m_value.array = alloc.allocate(1);
alloc.construct(m_value.array);
m_value.array = create<array_t>();
}

// [] only works for arrays
Expand Down Expand Up @@ -2670,9 +2655,7 @@ class basic_json
if (m_type == value_t::null)
{
m_type = value_t::object;
AllocatorType<object_t> alloc;
m_value.object = alloc.allocate(1);
alloc.construct(m_value.object);
m_value.object = create<object_t>();
}

// [] only works for objects
Expand Down Expand Up @@ -4471,6 +4454,11 @@ class basic_json
return parser(i, cb).parse();
}

static basic_json parse(std::istream&& i, parser_callback_t cb = nullptr)
{
return parser(i, cb).parse();
}

/*!
@brief deserialize from stream
Expand Down Expand Up @@ -6903,7 +6891,7 @@ class basic_json
@brief return number value for number tokens
This function translates the last token into a floating point number.
The pointer m_begin points to the beginning of the parsed number. We
The pointer m_start points to the beginning of the parsed number. We
pass this pointer to std::strtod which sets endptr to the first
character past the converted number. If this pointer is not the same as
m_cursor, then either more or less characters have been used during the
Expand Down
7 changes: 5 additions & 2 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,13 @@ class basic_json
private:
/// helper for exception-safe object creation
template<typename T, typename... Args>
static T* create( Args&&... args )
static T* create(Args&& ... args)
{
AllocatorType<T> alloc;
auto deleter = [&](T* object) { alloc.deallocate(object, 1); };
auto deleter = [&](T* object)
{
alloc.deallocate(object, 1);
};
std::unique_ptr<T, decltype(deleter)> object(alloc.allocate(1), deleter);
alloc.construct(object.get(), std::forward<Args>(args)...);
return object.release();
Expand Down

0 comments on commit 57de1d6

Please sign in to comment.