buffered_value 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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. #ifndef OSG_BUFFERED_VALUE
  14. #define OSG_BUFFERED_VALUE 1
  15. #include <osg/DisplaySettings>
  16. #include <vector>
  17. namespace osg {
  18. /** Implements a simple buffered value for values that need to be buffered on
  19. * a per graphics context basis.
  20. */
  21. template<class T>
  22. class buffered_value
  23. {
  24. public:
  25. inline buffered_value():
  26. _array(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0)
  27. {}
  28. inline buffered_value(unsigned int size):
  29. _array(size,0)
  30. {}
  31. buffered_value& operator = (const buffered_value& rhs)
  32. {
  33. _array = rhs._array;
  34. return *this;
  35. }
  36. inline void setAllElementsTo(const T& t) { std::fill(_array.begin(),_array.end(),t); }
  37. inline void clear() { _array.clear(); }
  38. inline bool empty() const { return _array.empty(); }
  39. inline unsigned int size() const { return _array.size(); }
  40. inline void resize(unsigned int newSize) { _array.resize(newSize,0); }
  41. inline T& operator[] (unsigned int pos)
  42. {
  43. // automatically resize array.
  44. if (_array.size()<=pos)
  45. _array.resize(pos+1,0);
  46. return _array[pos];
  47. }
  48. inline T operator[] (unsigned int pos) const
  49. {
  50. // automatically resize array.
  51. if (_array.size()<=pos)
  52. _array.resize(pos+1,0);
  53. return _array[pos];
  54. }
  55. protected:
  56. mutable std::vector<T> _array;
  57. };
  58. template<class T>
  59. class buffered_object
  60. {
  61. public:
  62. inline buffered_object():
  63. _array(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts())
  64. {}
  65. inline buffered_object(unsigned int size):
  66. _array(size)
  67. {}
  68. buffered_object& operator = (const buffered_object& rhs)
  69. {
  70. _array = rhs._array;
  71. return *this;
  72. }
  73. inline void setAllElementsTo(const T& t) { std::fill(_array.begin(),_array.end(),t); }
  74. inline void clear() { _array.clear(); }
  75. inline bool empty() const { return _array.empty(); }
  76. inline unsigned int size() const { return _array.size(); }
  77. inline void resize(unsigned int newSize) { _array.resize(newSize); }
  78. inline T& operator[] (unsigned int pos)
  79. {
  80. // automatically resize array.
  81. if (_array.size()<=pos)
  82. _array.resize(pos+1);
  83. return _array[pos];
  84. }
  85. inline const T& operator[] (unsigned int pos) const
  86. {
  87. // automatically resize array.
  88. if (_array.size()<=pos)
  89. _array.resize(pos+1);
  90. return _array[pos];
  91. }
  92. protected:
  93. mutable std::vector<T> _array;
  94. };
  95. }
  96. #endif