1 // OSGUtils.hxx - Useful templates for interfacing to Open Scene Graph
3 // Copyright (C) 2008 Tim Moore timoore@redhat.com
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the
17 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 // Boston, MA 02111-1307, USA.
20 #ifndef SIMGEAR_OSGUTILS_HXX
21 #define SIMGEAR_OSGUTILS_HXX 1
23 #include <boost/iterator/iterator_facade.hpp>
28 // RefPtrAdapter also appears in OpenSceneGraph's
29 // osgDB/DatabasePager.cpp. I wrote that code too. -- Tim Moore
31 // Convert function objects that take pointer args into functions that a
32 // reference to an osg::ref_ptr. This is quite useful for doing STL
33 // operations on lists of ref_ptr. This code assumes that a function
34 // with an argument const Foo* should be composed into a function of
35 // argument type ref_ptr<Foo>&, not ref_ptr<const Foo>&. Some support
36 // for that should be added to make this more general.
40 typedef class NullType {} PointeeType;
44 struct PointerTraits<U*>
46 typedef U PointeeType;
50 struct PointerTraits<const U*>
52 typedef U PointeeType;
55 template <typename FuncObj>
57 : public std::unary_function<const osg::ref_ptr<typename PointerTraits<typename FuncObj::argument_type>::PointeeType>,
58 typename FuncObj::result_type>
61 typedef typename PointerTraits<typename FuncObj::argument_type>::PointeeType PointeeType;
62 typedef osg::ref_ptr<PointeeType> RefPtrType;
63 explicit RefPtrAdapter(const FuncObj& funcObj) : _func(funcObj) {}
64 typename FuncObj::result_type operator()(const RefPtrType& refPtr) const
66 return _func(refPtr.get());
72 template <typename FuncObj>
73 RefPtrAdapter<FuncObj> refPtrAdapt(const FuncObj& func)
75 return RefPtrAdapter<FuncObj>(func);
78 /** Typesafe wrapper around OSG's object clone function. Something
79 * very similar is in current OSG sources.
83 template <typename T> class ref_ptr;
90 T* clone(const T* object, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY)
92 return static_cast<T*>(object->clone(copyop));
96 T* clone_ref(const osg::ref_ptr<T>& object,
97 const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY)
99 return static_cast<T*>(object->clone(copyop));
102 template<typename Container>
103 class BackRefInsertIterator
104 : public boost::iterator_facade<BackRefInsertIterator<Container>,
105 BackRefInsertIterator<Container>,
106 boost::incrementable_traversal_tag
110 typedef typename Container::value_type::element_type* PtrType;
111 BackRefInsertIterator() : _container(0) {}
112 explicit BackRefInsertIterator(Container& container)
113 : _container(&container)
117 BackRefInsertIterator&
118 operator=(const PtrType ptr)
120 _container->push_back(ptr);
125 friend class boost::iterator_core_access;
131 BackRefInsertIterator& dereference()
136 BackRefInsertIterator& dereference() const
138 return const_cast<BackRefInsertIterator&>(*this);
141 bool equal(const BackRefInsertIterator& rhs)
143 return _container == rhs._container;
146 Container* _container;
150 template<typename Container>
151 inline BackRefInsertIterator<Container>
152 backRefInsertIterator(Container& container)
154 return BackRefInsertIterator<Container>(container);