Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
R
rapidjson
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
rapidjson
Commits
513fe789
Commit
513fe789
authored
Feb 12, 2016
by
Milo Yip
Browse files
Options
Browse Files
Download
Plain Diff
Add templated accessors for C string
parents
4d648fdc
e61169e6
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
3 deletions
+17
-3
document.h
include/rapidjson/document.h
+12
-3
valuetest.cpp
test/unittest/valuetest.cpp
+5
-0
No files found.
include/rapidjson/document.h
View file @
513fe789
...
...
@@ -401,7 +401,7 @@ namespace internal {
template
<
typename
ValueType
,
typename
T
>
struct
TypeHelper
{
static
bool
Is
(
const
ValueType
&
)
{
RAPIDJSON_ASSERT
(
false
&&
"Unsupport type"
);
}
static
T
Get
(
const
ValueType
&
)
{
RAPIDJSON_ASSERT
(
false
&&
"Unsupport type"
);
}
static
T
Get
(
const
ValueType
&
)
{
RAPIDJSON_ASSERT
(
false
&&
"Unsupport type"
);
return
T
();
}
static
ValueType
&
Set
(
ValueType
&
,
T
)
{
RAPIDJSON_ASSERT
(
false
&&
"Unsupport type"
);
}
static
ValueType
&
Set
(
ValueType
&
,
T
,
typename
ValueType
::
AllocatorType
&
)
{
RAPIDJSON_ASSERT
(
false
&&
"Unsupport type"
);
}
};
...
...
@@ -462,6 +462,15 @@ struct TypeHelper<ValueType, float> {
static
ValueType
&
Set
(
ValueType
&
v
,
float
data
,
typename
ValueType
::
AllocatorType
&
)
{
return
v
.
SetFloat
(
data
);
}
};
template
<
typename
ValueType
>
struct
TypeHelper
<
ValueType
,
const
typename
ValueType
::
Ch
*>
{
typedef
const
typename
ValueType
::
Ch
*
StringType
;
static
bool
Is
(
const
ValueType
&
v
)
{
return
v
.
IsString
();
}
static
StringType
Get
(
const
ValueType
&
v
)
{
return
v
.
GetString
();
}
static
ValueType
&
Set
(
ValueType
&
v
,
const
StringType
data
)
{
return
v
.
SetString
(
typename
ValueType
::
StringRefType
(
data
));
}
static
ValueType
&
Set
(
ValueType
&
v
,
const
StringType
data
,
typename
ValueType
::
AllocatorType
&
a
)
{
return
v
.
SetString
(
data
,
a
);
}
};
#if RAPIDJSON_HAS_STDSTRING
template
<
typename
ValueType
>
struct
TypeHelper
<
ValueType
,
std
::
basic_string
<
typename
ValueType
::
Ch
>
>
{
...
...
@@ -1561,10 +1570,10 @@ public:
RAPIDJSON_ASSERT
((
flags_
&
kUint64Flag
)
!=
0
);
return
static_cast
<
double
>
(
data_
.
n
.
u64
);
// uint64_t -> double (may lose precision)
}
//! Get the value as
double
type.
//! Get the value as
float
type.
/*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessFloat() to check whether the converison is lossless.
*/
double
GetFloat
()
const
{
float
GetFloat
()
const
{
RAPIDJSON_ASSERT
(
IsFloat
());
return
static_cast
<
float
>
(
GetDouble
());
}
...
...
test/unittest/valuetest.cpp
View file @
513fe789
...
...
@@ -768,6 +768,11 @@ TEST(Value, String) {
EXPECT_STREQ
(
"World"
,
w
.
GetString
());
EXPECT_EQ
(
5u
,
w
.
GetStringLength
());
// templated functions
EXPECT_TRUE
(
z
.
Is
<
const
char
*>
());
EXPECT_STREQ
(
cstr
,
z
.
Get
<
const
char
*>
());
EXPECT_STREQ
(
"Apple"
,
z
.
Set
<
const
char
*>
(
"Apple"
).
Get
<
const
char
*>
());
#if RAPIDJSON_HAS_STDSTRING
{
std
::
string
str
=
"Hello World"
;
...
...
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