openscenegraph
buffered_value
Go to the documentation of this file.
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
14#ifndef OSG_BUFFERED_VALUE
15#define OSG_BUFFERED_VALUE 1
16
17#include <osg/DisplaySettings>
18#include <vector>
19
20namespace osg {
21
22/** Implements a simple buffered value for values that need to be buffered on
23 * a per graphics context basis.
24*/
25template<class T>
26class buffered_value
27{
28 public:
29
30 inline buffered_value():
31 _array(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0)
32 {}
33
34 inline buffered_value(unsigned int size):
35 _array(size,0)
36 {}
37
38 buffered_value& operator = (const buffered_value& rhs)
39 {
40 _array = rhs._array;
41 return *this;
42 }
43
44 inline void setAllElementsTo(const T& t) { std::fill(_array.begin(),_array.end(),t); }
45
46 inline void clear() { _array.clear(); }
47
48 inline bool empty() const { return _array.empty(); }
49
50 inline unsigned int size() const { return _array.size(); }
51
52 inline void resize(unsigned int newSize) { _array.resize(newSize,0); }
53
54 inline T& operator[] (unsigned int pos)
55 {
56 // automatically resize array.
57 if (_array.size()<=pos)
58 _array.resize(pos+1,0);
59
60 return _array[pos];
61 }
62
63 inline T operator[] (unsigned int pos) const
64 {
65 // automatically resize array.
66 if (_array.size()<=pos)
67 _array.resize(pos+1,0);
68
69 return _array[pos];
70 }
71
72 protected:
73
74 mutable std::vector<T> _array;
75};
76
77template<class T>
78class buffered_object
79{
80 public:
81
82 inline buffered_object():
83 _array(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts())
84 {}
85
86 inline buffered_object(unsigned int size):
87 _array(size)
88 {}
89
90 buffered_object& operator = (const buffered_object& rhs)
91 {
92 _array = rhs._array;
93 return *this;
94 }
95
96 inline void setAllElementsTo(const T& t) { std::fill(_array.begin(),_array.end(),t); }
97
98 inline void clear() { _array.clear(); }
99
100 inline bool empty() const { return _array.empty(); }
101
102 inline unsigned int size() const { return _array.size(); }
103
104 inline void resize(unsigned int newSize) { _array.resize(newSize); }
105
106 inline T& operator[] (unsigned int pos)
107 {
108 // automatically resize array.
109 if (_array.size()<=pos)
110 _array.resize(pos+1);
111
112 return _array[pos];
113 }
114
115 inline const T& operator[] (unsigned int pos) const
116 {
117 // automatically resize array.
118 if (_array.size()<=pos)
119 _array.resize(pos+1);
120
121 return _array[pos];
122 }
123
124
125 protected:
126
127 mutable std::vector<T> _array;
128};
129
130}
131
132#endif