00001 #ifndef SIMGEAR_EXTENDEDPROPERTYADAPTER_HXX
00002 #define SIMGEAR_EXTENDEDPROPERTYADAPTER_HXX 1
00003
00004 #include <algorithm>
00005
00006 #include <boost/bind.hpp>
00007
00008 #include <simgear/math/SGMath.hxx>
00009 #include <simgear/structure/exception.hxx>
00010
00011 #include "props.hxx"
00012
00013 namespace simgear
00014 {
00015
00016 namespace props
00017 {
00018
00019
00020 template<typename T> struct NumComponents;
00021
00022 template<> struct NumComponents<SGVec3d>
00023 {
00024 enum { num_components = 3 };
00025 };
00026
00027 template<> struct NumComponents<SGVec4d>
00028 {
00029 enum { num_components = 4 };
00030 };
00031
00032 }
00033
00034 template<typename T, typename NodeContainer>
00035 class ExtendedPropertyAdapter
00036 {
00037 public:
00038 enum { num_components = props::NumComponents<T>::num_components };
00039 ExtendedPropertyAdapter(const NodeContainer& elements)
00040 : _elements(elements)
00041 {
00042 }
00043 T operator()() const
00044 {
00045 T result;
00046 if (_elements.size() < num_components)
00047 throw sg_exception();
00048 for (int i = 0; i < num_components; ++i)
00049 result[i] = _elements[i]->getValue<double>();
00050 return result;
00051 }
00052 void set(const T& val)
00053 {
00054 if (_elements.size() < num_components)
00055 throw sg_exception();
00056 for (int i = 0; i < num_components; ++i)
00057 _elements[i]->setValue(val[i]);
00058 }
00059 private:
00060 const NodeContainer& _elements;
00061 };
00062
00063 template<typename InIterator, typename OutIterator>
00064 inline void makeChildList(SGPropertyNode* prop, InIterator inBegin,
00065 InIterator inEnd, OutIterator outBegin)
00066 {
00067 std::transform(inBegin, inEnd, outBegin,
00068 boost::bind(static_cast<SGPropertyNode* (SGPropertyNode::*)(const char*, int, bool)>(&SGPropertyNode::getChild), prop, _1, 0, true));
00069 }
00070
00071 }
00072 #endif