Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
60e6f822
Commit
60e6f822
authored
Nov 29, 2016
by
Théo DELRIEU
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add support for non-default-constructible udt
parent
e5999c6c
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
140 additions
and
441 deletions
+140
-441
src/json.hpp
src/json.hpp
+87
-50
test/src/unit-udt.cpp
test/src/unit-udt.cpp
+53
-391
No files found.
src/json.hpp
View file @
60e6f822
...
...
@@ -290,14 +290,32 @@ struct is_compatible_basic_json_type
};
// This trait checks if JSONSerializer<T>::from_json exists
// This trait checks if JSONSerializer<T>::from_json
(json const&, udt&)
exists
template
<
template
<
typename
,
typename
>
class
JSONSerializer
,
typename
Json
,
typename
T
>
struct
has_from_json
{
private:
template
<
typename
U
,
typename
=
decltype
(
uncvref_t
<
U
>
::
from_json
(
std
::
declval
<
Json
>
(),
std
::
declval
<
T
&>
()))
>
// also check the return type of from_json
template
<
typename
U
,
typename
=
enable_if_t
<
std
::
is_same
<
void
,
decltype
(
uncvref_t
<
U
>
::
from_json
(
std
::
declval
<
Json
>
(),
std
::
declval
<
T
&>
()))
>::
value
>>
static
int
detect
(
U
&&
);
static
void
detect
(...);
public:
static
constexpr
bool
value
=
std
::
is_integral
<
decltype
(
detect
(
std
::
declval
<
JSONSerializer
<
T
,
void
>>
()))
>::
value
;
};
// This trait checks if JSONSerializer<T>::from_json(json const&) exists
// this overload is used for non-default-constructible user-defined-types
template
<
template
<
typename
,
typename
>
class
JSONSerializer
,
typename
Json
,
typename
T
>
struct
has_non_default_from_json
{
private:
template
<
typename
U
,
typename
=
enable_if_t
<
std
::
is_same
<
T
,
decltype
(
uncvref_t
<
U
>
::
from_json
(
std
::
declval
<
Json
>
()))
>::
value
>>
static
int
detect
(
U
&&
);
static
void
detect
(...);
...
...
@@ -326,8 +344,8 @@ public:
// those declarations are needed to workaround a MSVC bug related to ADL
// (taken from MSVC-Ranges implementation)
void
to_json
();
void
from_json
();
//
void to_json();
//
void from_json();
struct
to_json_fn
{
...
...
@@ -2467,57 +2485,63 @@ class basic_json
return
*
this
;
}
/*!
@brief destructor
// this overload is needed, since constructor for udt is explicit
template
<
typename
T
,
enable_if_t
<
not
detail
::
is_compatible_basic_json_type
<
uncvref_t
<
T
>,
basic_json_t
>::
value
and
detail
::
has_to_json
<
JSONSerializer
,
basic_json_t
,
uncvref_t
<
T
>>::
value
>>
reference
&
operator
=
(
T
&&
val
)
noexcept
(
std
::
is_nothrow_constructible
<
basic_json_t
,
uncvref_t
<
T
>>::
value
and
std
::
is_nothrow_move_assignable
<
uncvref_t
<
T
>>::
value
)
{
static_assert
(
sizeof
(
T
)
==
0
,
""
);
// I'm not sure this a is good practice...
return
*
this
=
basic_json_t
{
std
::
forward
<
T
>
(
val
)};
}
Destroys the JSON value and frees all allocated memory.
/*!
@brief destructor
@complexity Linear
.
Destroys the JSON value and frees all allocated memory
.
@requirement This function helps `basic_json` satisfying the
[Container](http://en.cppreference.com/w/cpp/concept/Container)
requirements:
- The complexity is linear.
- All stored elements are destroyed and all memory is freed.
@complexity Linear.
@since version 1.0.0
*/
~
basic_json
()
{
assert_invariant
();
@requirement This function helps `basic_json` satisfying the
[Container](http://en.cppreference.com/w/cpp/concept/Container)
requirements:
- The complexity is linear.
- All stored elements are destroyed and all memory is freed.
switch
(
m_type
)
{
case
value_t
:
:
object
:
{
AllocatorType
<
object_t
>
alloc
;
alloc
.
destroy
(
m_value
.
object
);
alloc
.
deallocate
(
m_value
.
object
,
1
);
break
;
}
case
value_t
:
:
array
:
{
AllocatorType
<
array_t
>
alloc
;
alloc
.
destroy
(
m_value
.
array
);
alloc
.
deallocate
(
m_value
.
array
,
1
);
break
;
}
case
value_t
:
:
string
:
{
AllocatorType
<
string_t
>
alloc
;
alloc
.
destroy
(
m_value
.
string
);
alloc
.
deallocate
(
m_value
.
string
,
1
);
break
;
}
@since version 1.0.0
*/
~
basic_json
()
{
assert_invariant
();
default:
{
// all other types need no specific destructor
break
;
}
}
switch
(
m_type
)
{
case
value_t
:
:
object
:
{
AllocatorType
<
object_t
>
alloc
;
alloc
.
destroy
(
m_value
.
object
);
alloc
.
deallocate
(
m_value
.
object
,
1
);
break
;
}
case
value_t
:
:
array
:
{
AllocatorType
<
array_t
>
alloc
;
alloc
.
destroy
(
m_value
.
array
);
alloc
.
deallocate
(
m_value
.
array
,
1
);
break
;
}
case
value_t
:
:
string
:
{
AllocatorType
<
string_t
>
alloc
;
alloc
.
destroy
(
m_value
.
string
);
alloc
.
deallocate
(
m_value
.
string
,
1
);
break
;
}
default:
{
// all other types need no specific destructor
break
;
}
}
}
/// @}
...
...
@@ -3273,6 +3297,19 @@ class basic_json
return
ret
;
}
// This overload is chosen for non-default constructible user-defined-types
template
<
typename
T
,
enable_if_t
<
not
detail
::
is_compatible_basic_json_type
<
T
,
basic_json_t
>
::
value
and
detail
::
has_non_default_from_json
<
JSONSerializer
,
basic_json_t
,
T
>::
value
,
short
>
=
0
>
T
get
()
const
{
return
JSONSerializer
<
T
>::
from_json
(
*
this
);
}
/*!
@brief get a pointer value (explicit)
...
...
test/src/unit-udt.cpp
View file @
60e6f822
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment