-
Notifications
You must be signed in to change notification settings - Fork 933
/
osgyaml.hpp
64 lines (48 loc) · 1.59 KB
/
osgyaml.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef OPENMW_COMPONENTS_SERIALIZATION_OSGYAML_H
#define OPENMW_COMPONENTS_SERIALIZATION_OSGYAML_H
#include <yaml-cpp/yaml.h>
#include <osg/Vec2f>
#include <osg/Vec3f>
#include <osg/Vec4f>
namespace Serialization
{
template <class OSGVec>
YAML::Node encodeOSGVec(const OSGVec& rhs)
{
YAML::Node node;
for (int i = 0; i < OSGVec::num_components; ++i)
node.push_back(rhs[i]);
return node;
}
template <class OSGVec>
bool decodeOSGVec(const YAML::Node& node, OSGVec& rhs)
{
if (!node.IsSequence() || node.size() != OSGVec::num_components)
return false;
for (int i = 0; i < OSGVec::num_components; ++i)
rhs[i] = node[i].as<typename OSGVec::value_type>();
return true;
}
}
namespace YAML
{
template <>
struct convert<osg::Vec2f>
{
static Node encode(const osg::Vec2f& rhs) { return Serialization::encodeOSGVec(rhs); }
static bool decode(const Node& node, osg::Vec2f& rhs) { return Serialization::decodeOSGVec(node, rhs); }
};
template <>
struct convert<osg::Vec3f>
{
static Node encode(const osg::Vec3f& rhs) { return Serialization::encodeOSGVec(rhs); }
static bool decode(const Node& node, osg::Vec3f& rhs) { return Serialization::decodeOSGVec(node, rhs); }
};
template <>
struct convert<osg::Vec4f>
{
static Node encode(const osg::Vec4f& rhs) { return Serialization::encodeOSGVec(rhs); }
static bool decode(const Node& node, osg::Vec4f& rhs) { return Serialization::decodeOSGVec(node, rhs); }
};
}
#endif