Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
yaml-cpp
Commits
3d84f570
Commit
3d84f570
authored
Sep 08, 2011
by
Jesse Beder
Browse files
Switched convert to a templated struct that can be specialized (so we can partially specialize it)
parent
a7ebb361
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
80 additions
and
58 deletions
+80
-58
include/yaml-cpp/value.h
include/yaml-cpp/value.h
+1
-0
include/yaml-cpp/value/convert.h
include/yaml-cpp/value/convert.h
+30
-0
include/yaml-cpp/value/detail/impl.h
include/yaml-cpp/value/detail/impl.h
+2
-2
include/yaml-cpp/value/impl.h
include/yaml-cpp/value/impl.h
+2
-2
include/yaml-cpp/value/value.h
include/yaml-cpp/value/value.h
+1
-4
src/value/convert.cpp
src/value/convert.cpp
+40
-46
util/value.cpp
util/value.cpp
+4
-4
No files found.
include/yaml-cpp/value.h
View file @
3d84f570
...
@@ -8,6 +8,7 @@
...
@@ -8,6 +8,7 @@
#include "yaml-cpp/value/value.h"
#include "yaml-cpp/value/value.h"
#include "yaml-cpp/value/impl.h"
#include "yaml-cpp/value/impl.h"
#include "yaml-cpp/value/convert.h"
#include "yaml-cpp/value/detail/impl.h"
#include "yaml-cpp/value/detail/impl.h"
#endif // VALUE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#endif // VALUE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
include/yaml-cpp/value/convert.h
0 → 100644
View file @
3d84f570
#ifndef VALUE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define VALUE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
#pragma once
#endif
#include "yaml-cpp/value/value.h"
#include <sstream>
namespace
YAML
{
// std::string
template
<
>
struct
convert
<
std
::
string
>
{
static
Value
encode
(
const
std
::
string
&
rhs
)
{
return
Value
(
rhs
);
}
static
bool
decode
(
const
Value
&
value
,
std
::
string
&
rhs
)
{
if
(
value
.
Type
()
!=
ValueType
::
Scalar
)
return
false
;
rhs
=
value
.
scalar
();
return
true
;
}
};
}
#endif // VALUE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
include/yaml-cpp/value/detail/impl.h
View file @
3d84f570
...
@@ -78,7 +78,7 @@ namespace YAML
...
@@ -78,7 +78,7 @@ namespace YAML
inline
bool
node_data
::
equals
(
detail
::
shared_node
pNode
,
const
T
&
rhs
,
detail
::
shared_memory_holder
pMemory
)
inline
bool
node_data
::
equals
(
detail
::
shared_node
pNode
,
const
T
&
rhs
,
detail
::
shared_memory_holder
pMemory
)
{
{
T
lhs
;
T
lhs
;
if
(
convert
(
Value
(
pNode
,
pMemory
),
lhs
))
if
(
convert
<
T
>::
decode
(
Value
(
pNode
,
pMemory
),
lhs
))
return
lhs
==
rhs
;
return
lhs
==
rhs
;
return
false
;
return
false
;
}
}
...
@@ -86,7 +86,7 @@ namespace YAML
...
@@ -86,7 +86,7 @@ namespace YAML
template
<
typename
T
>
template
<
typename
T
>
inline
shared_node
node_data
::
convert_to_node
(
const
T
&
rhs
,
detail
::
shared_memory_holder
pMemory
)
inline
shared_node
node_data
::
convert_to_node
(
const
T
&
rhs
,
detail
::
shared_memory_holder
pMemory
)
{
{
Value
value
=
convert
(
rhs
);
Value
value
=
convert
<
T
>::
encode
(
rhs
);
pMemory
->
merge
(
*
value
.
m_pMemory
);
pMemory
->
merge
(
*
value
.
m_pMemory
);
return
value
.
m_pNode
;
return
value
.
m_pNode
;
}
}
...
...
include/yaml-cpp/value/impl.h
View file @
3d84f570
...
@@ -51,7 +51,7 @@ namespace YAML
...
@@ -51,7 +51,7 @@ namespace YAML
inline
const
T
Value
::
as
()
const
inline
const
T
Value
::
as
()
const
{
{
T
t
;
T
t
;
if
(
convert
<
T
>
(
*
this
,
t
))
if
(
convert
<
T
>
::
decode
(
*
this
,
t
))
return
t
;
return
t
;
throw
std
::
runtime_error
(
"Unable to convert to type"
);
throw
std
::
runtime_error
(
"Unable to convert to type"
);
}
}
...
@@ -85,7 +85,7 @@ namespace YAML
...
@@ -85,7 +85,7 @@ namespace YAML
template
<
typename
T
>
template
<
typename
T
>
inline
void
Value
::
Assign
(
const
T
&
rhs
)
inline
void
Value
::
Assign
(
const
T
&
rhs
)
{
{
AssignData
(
convert
<
T
>
(
rhs
));
AssignData
(
convert
<
T
>
::
encode
(
rhs
));
}
}
template
<
>
template
<
>
...
...
include/yaml-cpp/value/value.h
View file @
3d84f570
...
@@ -83,10 +83,7 @@ namespace YAML
...
@@ -83,10 +83,7 @@ namespace YAML
bool
is
(
const
Value
&
lhs
,
const
Value
&
rhs
);
bool
is
(
const
Value
&
lhs
,
const
Value
&
rhs
);
template
<
typename
T
>
template
<
typename
T
>
Value
convert
(
const
T
&
rhs
);
struct
convert
;
template
<
typename
T
>
bool
convert
(
const
Value
&
value
,
T
&
rhs
);
}
}
#endif // VALUE_VALUE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#endif // VALUE_VALUE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
src/value/convert.cpp
View file @
3d84f570
#include "yaml-cpp/value.h"
#include "yaml-cpp/value/convert.h"
#include <sstream>
namespace
YAML
namespace
YAML
{
{
// std::string
template
<
>
Value
convert
(
const
std
::
string
&
rhs
)
{
return
Value
(
rhs
);
}
template
<
>
bool
convert
(
const
Value
&
value
,
std
::
string
&
rhs
)
{
if
(
value
.
Type
()
!=
ValueType
::
Scalar
)
return
false
;
rhs
=
value
.
scalar
();
return
true
;
}
#define YAML_DEFINE_CONVERT_STREAMABLE(type)\
//#define YAML_DEFINE_CONVERT_STREAMABLE(type)\
template<> Value convert(const type& rhs) {\
// template<> Value convert(const type& rhs) {\
std::stringstream stream;\
// std::stringstream stream;\
stream << rhs;\
// stream << rhs;\
return Value(stream.str());\
// return Value(stream.str());\
}\
// }\
template<> bool convert(const Value& value, type& rhs) {\
// template<> bool convert(const Value& value, type& rhs) {\
if(value.Type() != ValueType::Scalar)\
// if(value.Type() != ValueType::Scalar)\
return false;\
// return false;\
std::stringstream stream(value.scalar());\
// std::stringstream stream(value.scalar());\
stream >> rhs;\
// stream >> rhs;\
return !!stream;\
// return !!stream;\
}
// }
//
YAML_DEFINE_CONVERT_STREAMABLE
(
int
)
// YAML_DEFINE_CONVERT_STREAMABLE(int)
YAML_DEFINE_CONVERT_STREAMABLE
(
unsigned
)
// YAML_DEFINE_CONVERT_STREAMABLE(unsigned)
YAML_DEFINE_CONVERT_STREAMABLE
(
short
)
// YAML_DEFINE_CONVERT_STREAMABLE(short)
YAML_DEFINE_CONVERT_STREAMABLE
(
unsigned
short
)
// YAML_DEFINE_CONVERT_STREAMABLE(unsigned short)
YAML_DEFINE_CONVERT_STREAMABLE
(
long
)
// YAML_DEFINE_CONVERT_STREAMABLE(long)
YAML_DEFINE_CONVERT_STREAMABLE
(
unsigned
long
)
// YAML_DEFINE_CONVERT_STREAMABLE(unsigned long)
YAML_DEFINE_CONVERT_STREAMABLE
(
long
long
)
// YAML_DEFINE_CONVERT_STREAMABLE(long long)
YAML_DEFINE_CONVERT_STREAMABLE
(
unsigned
long
long
)
// YAML_DEFINE_CONVERT_STREAMABLE(unsigned long long)
//
YAML_DEFINE_CONVERT_STREAMABLE
(
char
)
// YAML_DEFINE_CONVERT_STREAMABLE(char)
YAML_DEFINE_CONVERT_STREAMABLE
(
unsigned
char
)
// YAML_DEFINE_CONVERT_STREAMABLE(unsigned char)
//
YAML_DEFINE_CONVERT_STREAMABLE
(
float
)
// YAML_DEFINE_CONVERT_STREAMABLE(float)
YAML_DEFINE_CONVERT_STREAMABLE
(
double
)
// YAML_DEFINE_CONVERT_STREAMABLE(double)
YAML_DEFINE_CONVERT_STREAMABLE
(
long
double
)
// YAML_DEFINE_CONVERT_STREAMABLE(long double)
//
#undef YAML_DEFINE_CONVERT_STREAMABLE
//#undef YAML_DEFINE_CONVERT_STREAMABLE
//
// template<typename K, typename V>
// Value convert<std::map<K, V> >(const std::map<K, V>& rhs) {
// Value value(ValueType::Map);
// for(std::map<K, V>::const_iterator it=rhs.begin();it!=rhs.end();++it)
// value[it->first] = it->second;
// return value;
// }
}
}
util/value.cpp
View file @
3d84f570
...
@@ -7,10 +7,10 @@ int main()
...
@@ -7,10 +7,10 @@ int main()
std
::
cout
<<
value
[
"key"
].
as
<
std
::
string
>
()
<<
"
\n
"
;
std
::
cout
<<
value
[
"key"
].
as
<
std
::
string
>
()
<<
"
\n
"
;
value
[
"key"
][
"key"
]
=
"value"
;
value
[
"key"
][
"key"
]
=
"value"
;
std
::
cout
<<
value
[
"key"
][
"key"
].
as
<
std
::
string
>
()
<<
"
\n
"
;
std
::
cout
<<
value
[
"key"
][
"key"
].
as
<
std
::
string
>
()
<<
"
\n
"
;
value
[
5
]
=
"monkey"
;
//
value[5] = "monkey";
std
::
cout
<<
value
[
5
].
as
<
std
::
string
>
()
<<
"
\n
"
;
//
std::cout << value[5].as<std::string>() << "\n";
value
[
"monkey"
]
=
5
;
//
value["monkey"] = 5;
std
::
cout
<<
value
[
"monkey"
].
as
<
int
>
()
<<
"
\n
"
;
//
std::cout << value["monkey"].as<int>() << "\n";
return
0
;
return
0
;
}
}
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