| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | // Code by: Jeremy Moles (cubicool) 2007-2008 |
|---|
| 15 | |
|---|
| 16 | #ifndef OSGWIDGET_UI_OBJECT_PARENT |
|---|
| 17 | #define OSGWIDGET_UI_OBJECT_PARENT |
|---|
| 18 | |
|---|
| 19 | #include <osg/Object> |
|---|
| 20 | #include <osg/observer_ptr> |
|---|
| 21 | |
|---|
| 22 | namespace osgWidget { |
|---|
| 23 | |
|---|
| 24 | template <typename T> |
|---|
| 25 | class UIObjectParent |
|---|
| 26 | { |
|---|
| 27 | public: |
|---|
| 28 | |
|---|
| 29 | typedef T object_type; |
|---|
| 30 | typedef osg::observer_ptr<object_type> ptr_type; |
|---|
| 31 | typedef std::vector<ptr_type> Vector; |
|---|
| 32 | typedef typename Vector::iterator Iterator; |
|---|
| 33 | typedef typename Vector::const_iterator ConstIterator; |
|---|
| 34 | |
|---|
| 35 | Iterator begin() { |
|---|
| 36 | return _objects.begin(); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | ConstIterator begin() const { |
|---|
| 40 | return _objects.begin(); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | Iterator end() { |
|---|
| 44 | return _objects.end(); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | ConstIterator end() const { |
|---|
| 48 | return _objects.end(); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | typename Vector::size_type size() const { |
|---|
| 52 | return _objects.size(); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | object_type* getByName(const std::string& name) { |
|---|
| 56 | return _getByName(name); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | const object_type* getByName(const std::string& name) const { |
|---|
| 60 | return _getByName(name); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | object_type* getByIndex(unsigned int index) { |
|---|
| 64 | return _getByIndex(index); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | const object_type* getByIndex(unsigned int index) const { |
|---|
| 68 | return _getByIndex(index); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | unsigned int getNumObjects() const { |
|---|
| 72 | return _objects.size(); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | Vector& getObjects() { |
|---|
| 76 | return _objects; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | const Vector& getObjects() const { |
|---|
| 80 | return _objects; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | protected: |
|---|
| 84 | |
|---|
| 85 | bool _remove(object_type* obj) { |
|---|
| 86 | Iterator i = std::find(begin(), end(), obj); |
|---|
| 87 | |
|---|
| 88 | if(i == end()) return false; |
|---|
| 89 | |
|---|
| 90 | _objects.erase(i); |
|---|
| 91 | |
|---|
| 92 | return true; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | bool _removeByName(const std::string& name) { |
|---|
| 96 | for(Iterator i = begin(); i != end(); i++) if(i->get()->getName() == name) { |
|---|
| 97 | _objects.erase(i); |
|---|
| 98 | |
|---|
| 99 | return true; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | return false; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | Vector _objects; |
|---|
| 106 | |
|---|
| 107 | private: |
|---|
| 108 | |
|---|
| 109 | // I had to add this to avoid ambiguity errors with MSVC. Garbage. |
|---|
| 110 | object_type* _getByName(const std::string& name) const { |
|---|
| 111 | for(ConstIterator i = begin(); i != end(); i++) { |
|---|
| 112 | if(i->valid() && i->get()->getName() == name) return i->get(); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | return 0; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | object_type* _getByIndex(unsigned int index) const { |
|---|
| 119 | for(ConstIterator i = begin(); i != end(); i++) { |
|---|
| 120 | if(i->valid() && i->get()->getIndex() == index) return i->get(); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | return 0; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | }; |
|---|
| 127 | |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | #endif |
|---|