experiment with user-defined allocator

This commit is contained in:
Niels 2015-02-15 22:49:26 +01:00
parent b1be1b45ee
commit 568d75954e
2 changed files with 56 additions and 16 deletions

View file

@ -59,7 +59,8 @@ template <
class StringType = std::string, class StringType = std::string,
class BooleanType = bool, class BooleanType = bool,
class NumberIntegerType = int64_t, class NumberIntegerType = int64_t,
class NumberFloatType = double class NumberFloatType = double,
template<typename U> class Allocator = std::allocator
> >
class basic_json class basic_json
{ {
@ -199,7 +200,11 @@ class basic_json
case (value_t::string): case (value_t::string):
{ {
m_value.string = new string_t(""); Allocator<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, "");
//m_value.string = new string_t("");
break; break;
} }
@ -270,13 +275,21 @@ class basic_json
/// create a string (explicit) /// create a string (explicit)
inline basic_json(const string_t& value) inline basic_json(const string_t& value)
: m_type(value_t::string), m_value(new string_t(value)) : m_type(value_t::string)//, m_value(new string_t(value))
{} {
Allocator<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, value);
}
/// create a string (explicit) /// create a string (explicit)
inline basic_json(const typename string_t::value_type* value) inline basic_json(const typename string_t::value_type* value)
: m_type(value_t::string), m_value(new string_t(value)) : m_type(value_t::string)//, m_value(new string_t(value))
{} {
Allocator<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, value);
}
/// create a string (implicit) /// create a string (implicit)
template <class V, typename template <class V, typename
@ -418,7 +431,10 @@ class basic_json
} }
case (value_t::string): case (value_t::string):
{ {
m_value.string = new string_t(*other.m_value.string); // m_value.string = new string_t(*other.m_value.string);
Allocator<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, *other.m_value.string);
break; break;
} }
case (value_t::boolean): case (value_t::boolean):
@ -483,8 +499,12 @@ class basic_json
case (value_t::string): case (value_t::string):
{ {
delete m_value.string; Allocator<string_t> alloc;
alloc.deallocate(m_value.string, 1);
m_value.string = nullptr; m_value.string = nullptr;
// delete m_value.string;
// m_value.string = nullptr;
break; break;
} }

View file

@ -59,7 +59,8 @@ template <
class StringType = std::string, class StringType = std::string,
class BooleanType = bool, class BooleanType = bool,
class NumberIntegerType = int64_t, class NumberIntegerType = int64_t,
class NumberFloatType = double class NumberFloatType = double,
template<typename U> class Allocator = std::allocator
> >
class basic_json class basic_json
{ {
@ -199,7 +200,11 @@ class basic_json
case (value_t::string): case (value_t::string):
{ {
m_value.string = new string_t(""); Allocator<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, "");
//m_value.string = new string_t("");
break; break;
} }
@ -270,13 +275,21 @@ class basic_json
/// create a string (explicit) /// create a string (explicit)
inline basic_json(const string_t& value) inline basic_json(const string_t& value)
: m_type(value_t::string), m_value(new string_t(value)) : m_type(value_t::string)//, m_value(new string_t(value))
{} {
Allocator<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, value);
}
/// create a string (explicit) /// create a string (explicit)
inline basic_json(const typename string_t::value_type* value) inline basic_json(const typename string_t::value_type* value)
: m_type(value_t::string), m_value(new string_t(value)) : m_type(value_t::string)//, m_value(new string_t(value))
{} {
Allocator<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, value);
}
/// create a string (implicit) /// create a string (implicit)
template <class V, typename template <class V, typename
@ -418,7 +431,10 @@ class basic_json
} }
case (value_t::string): case (value_t::string):
{ {
m_value.string = new string_t(*other.m_value.string); // m_value.string = new string_t(*other.m_value.string);
Allocator<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, *other.m_value.string);
break; break;
} }
case (value_t::boolean): case (value_t::boolean):
@ -483,8 +499,12 @@ class basic_json
case (value_t::string): case (value_t::string):
{ {
delete m_value.string; Allocator<string_t> alloc;
alloc.deallocate(m_value.string, 1);
m_value.string = nullptr; m_value.string = nullptr;
// delete m_value.string;
// m_value.string = nullptr;
break; break;
} }