Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exception-safe object creation, fixes #118 #122

Merged
merged 1 commit into from
Oct 3, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 25 additions & 42 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,17 @@ 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 +636,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 +675,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 +891,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 +950,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 +1310,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 +1409,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 +1505,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 @@ -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