UIObjectParent 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // Code by: Jeremy Moles (cubicool) 2007-2008
  14. #ifndef OSGWIDGET_UI_OBJECT_PARENT
  15. #define OSGWIDGET_UI_OBJECT_PARENT
  16. #include <osg/Object>
  17. #include <osg/observer_ptr>
  18. namespace osgWidget {
  19. template <typename T>
  20. class UIObjectParent
  21. {
  22. public:
  23. typedef T object_type;
  24. typedef osg::observer_ptr<object_type> ptr_type;
  25. typedef std::vector<ptr_type> Vector;
  26. typedef typename Vector::iterator Iterator;
  27. typedef typename Vector::const_iterator ConstIterator;
  28. Iterator begin() {
  29. return _objects.begin();
  30. }
  31. ConstIterator begin() const {
  32. return _objects.begin();
  33. }
  34. Iterator end() {
  35. return _objects.end();
  36. }
  37. ConstIterator end() const {
  38. return _objects.end();
  39. }
  40. typename Vector::size_type size() const {
  41. return _objects.size();
  42. }
  43. object_type* getByName(const std::string& name) {
  44. return _getByName(name);
  45. }
  46. const object_type* getByName(const std::string& name) const {
  47. return _getByName(name);
  48. }
  49. object_type* getByIndex(unsigned int index) {
  50. return _getByIndex(index);
  51. }
  52. const object_type* getByIndex(unsigned int index) const {
  53. return _getByIndex(index);
  54. }
  55. unsigned int getNumObjects() const {
  56. return _objects.size();
  57. }
  58. Vector& getObjects() {
  59. return _objects;
  60. }
  61. const Vector& getObjects() const {
  62. return _objects;
  63. }
  64. protected:
  65. bool _remove(object_type* obj) {
  66. Iterator i = std::find(begin(), end(), obj);
  67. if(i == end()) return false;
  68. _objects.erase(i);
  69. return true;
  70. }
  71. bool _removeByName(const std::string& name) {
  72. for(Iterator i = begin(); i != end(); i++) if(i->get()->getName() == name) {
  73. _objects.erase(i);
  74. return true;
  75. }
  76. return false;
  77. }
  78. Vector _objects;
  79. private:
  80. // I had to add this to avoid ambiguity errors with MSVC. Garbage.
  81. object_type* _getByName(const std::string& name) const {
  82. for(ConstIterator i = begin(); i != end(); i++) {
  83. if(i->valid() && i->get()->getName() == name) return i->get();
  84. }
  85. return 0;
  86. }
  87. object_type* _getByIndex(unsigned int index) const {
  88. for(ConstIterator i = begin(); i != end(); i++) {
  89. if(i->valid() && i->get()->getIndex() == index) return i->get();
  90. }
  91. return 0;
  92. }
  93. };
  94. }
  95. #endif