OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Stats.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 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_STATS
15 #define OSG_STATS 1
16 
17 #include <osg/Referenced>
18 #include <OpenThreads/Mutex>
19 #include <OpenThreads/ScopedLock>
20 
21 #include <string>
22 #include <map>
23 #include <vector>
24 #include <ostream>
25 
26 namespace osg {
27 
29 {
30  public:
31 
32  Stats(const std::string& name);
33 
34  Stats(const std::string& name, unsigned int numberOfFrames);
35 
36  void setName(const std::string& name) { _name = name; }
37  const std::string& getName() const { return _name; }
38 
39  void allocate(unsigned int numberOfFrames);
40 
41  unsigned int getEarliestFrameNumber() const { return _latestFrameNumber < static_cast<unsigned int>(_attributeMapList.size()) ? 0 : _latestFrameNumber - static_cast<unsigned int>(_attributeMapList.size()) + 1; }
42  unsigned int getLatestFrameNumber() const { return _latestFrameNumber; }
43 
44  typedef std::map<std::string, double> AttributeMap;
45  typedef std::vector<AttributeMap> AttributeMapList;
46 
47  bool setAttribute(unsigned int frameNumber, const std::string& attributeName, double value);
48 
49  inline bool getAttribute(unsigned int frameNumber, const std::string& attributeName, double& value) const
50  {
52  return getAttributeNoMutex(frameNumber, attributeName, value);
53  }
54 
55  bool getAveragedAttribute(const std::string& attributeName, double& value, bool averageInInverseSpace=false) const;
56 
57  bool getAveragedAttribute(unsigned int startFrameNumber, unsigned int endFrameNumber, const std::string& attributeName, double& value, bool averageInInverseSpace=false) const;
58 
59  inline AttributeMap& getAttributeMap(unsigned int frameNumber)
60  {
62  return getAttributeMapNoMutex(frameNumber);
63  }
64 
65  inline const AttributeMap& getAttributeMap(unsigned int frameNumber) const
66  {
68  return getAttributeMapNoMutex(frameNumber);
69  }
70 
71  typedef std::map<std::string, bool> CollectMap;
72 
73  void collectStats(const std::string& str, bool flag) { _collectMap[str] = flag; }
74 
75  inline bool collectStats(const std::string& str) const
76  {
78 
79  CollectMap::const_iterator itr = _collectMap.find(str);
80  return (itr != _collectMap.end()) ? itr->second : false;
81  }
82 
83  void report(std::ostream& out, const char* indent=0) const;
84  void report(std::ostream& out, unsigned int frameNumber, const char* indent=0) const;
85 
86  protected:
87 
88  virtual ~Stats() {}
89 
90  bool getAttributeNoMutex(unsigned int frameNumber, const std::string& attributeName, double& value) const;
91 
92  AttributeMap& getAttributeMapNoMutex(unsigned int frameNumber);
93  const AttributeMap& getAttributeMapNoMutex(unsigned int frameNumber) const;
94 
95 
96  int getIndex(unsigned int frameNumber) const
97  {
98  // reject frame that are in the future
99  if (frameNumber > _latestFrameNumber) return -1;
100 
101  // reject frames that are too early
102  if (frameNumber < getEarliestFrameNumber()) return -1;
103 
104  if (frameNumber >= _baseFrameNumber) return frameNumber - _baseFrameNumber;
105  else return static_cast<int>(_attributeMapList.size()) - (_baseFrameNumber-frameNumber);
106  }
107 
108  std::string _name;
109 
111 
112  unsigned int _baseFrameNumber;
113  unsigned int _latestFrameNumber;
114 
115  AttributeMapList _attributeMapList;
116  AttributeMap _invalidAttributeMap;
117 
118  CollectMap _collectMap;
119 
120 };
121 
122 
123 }
124 
125 #endif
OpenThreads::Mutex _mutex
Definition: Stats.h:110
#define OSG_EXPORT
Definition: Export.h:43
std::map< std::string, bool > CollectMap
Definition: Stats.h:71
unsigned int _baseFrameNumber
Definition: Stats.h:112
This class provides an object-oriented thread mutex interface.
Definition: Mutex.h:31
unsigned int getEarliestFrameNumber() const
Definition: Stats.h:41
int getIndex(unsigned int frameNumber) const
Definition: Stats.h:96
void collectStats(const std::string &str, bool flag)
Definition: Stats.h:73
AttributeMap _invalidAttributeMap
Definition: Stats.h:116
unsigned int _latestFrameNumber
Definition: Stats.h:113
AttributeMapList _attributeMapList
Definition: Stats.h:115
bool collectStats(const std::string &str) const
Definition: Stats.h:75
const AttributeMap & getAttributeMap(unsigned int frameNumber) const
Definition: Stats.h:65
std::map< std::string, double > AttributeMap
Definition: Stats.h:44
CollectMap _collectMap
Definition: Stats.h:118
const std::string & getName() const
Definition: Stats.h:37
unsigned int getLatestFrameNumber() const
Definition: Stats.h:42
Definition: AlphaFunc.h:19
AttributeMap & getAttributeMap(unsigned int frameNumber)
Definition: Stats.h:59
bool getAttribute(unsigned int frameNumber, const std::string &attributeName, double &value) const
Definition: Stats.h:49
std::string _name
Definition: Stats.h:108
std::vector< AttributeMap > AttributeMapList
Definition: Stats.h:45
void setName(const std::string &name)
Definition: Stats.h:36
virtual ~Stats()
Definition: Stats.h:88