]> git.mxchange.org Git - simgear.git/blob - simgear/props/ExtendedPropertyAdapter.hxx
Melchior FRANZ: fix SGPropertyNode::LAST_USED_ATTRIBUTE
[simgear.git] / simgear / props / ExtendedPropertyAdapter.hxx
1 #ifndef SIMGEAR_EXTENDEDPROPERTYADAPTER_HXX
2 #define SIMGEAR_EXTENDEDPROPERTYADAPTER_HXX 1
3
4 #include <algorithm>
5
6 #include <boost/bind.hpp>
7
8 #include <simgear/math/SGMath.hxx>
9 #include <simgear/structure/exception.hxx>
10
11 #include "props.hxx"
12
13 namespace simgear
14 {
15
16 namespace props
17 {
18 // This should be in simgear/math/SGVec.hxx and friends
19
20 template<typename T> struct NumComponents;
21
22 template<> struct NumComponents<SGVec3d>
23 {
24     enum { num_components = 3 };
25 };
26
27 template<> struct NumComponents<SGVec4d>
28 {
29     enum { num_components = 4 };
30 };
31
32 }
33
34 template<typename T, typename NodeContainer>
35 class ExtendedPropertyAdapter
36 {
37 public:
38     enum { num_components = props::NumComponents<T>::num_components };
39     ExtendedPropertyAdapter(const NodeContainer& elements)
40         : _elements(elements)
41     {
42     }
43     T operator()() const
44     {
45         T result;
46         if (_elements.size() < num_components)
47             throw sg_exception();
48         for (int i = 0; i < num_components; ++i)
49             result[i] = _elements[i]->getValue<double>();
50         return result;
51     }
52     void set(const T& val)
53     {
54         if (_elements.size() < num_components)
55             throw sg_exception();
56         for (int i = 0; i < num_components; ++i)
57             _elements[i]->setValue(val[i]);
58     }
59 private:
60     const NodeContainer& _elements;
61 };
62
63 template<typename InIterator, typename OutIterator>
64 inline void makeChildList(SGPropertyNode* prop, InIterator inBegin,
65                           InIterator inEnd, OutIterator outBegin)
66 {
67     std::transform(inBegin, inEnd, outBegin,
68                    boost::bind(static_cast<SGPropertyNode* (SGPropertyNode::*)(const char*, int, bool)>(&SGPropertyNode::getChild), prop, _1, 0, true));
69 }
70
71 }
72 #endif