1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield
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.
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.
17#include <osg/Referenced>
18#include <OpenThreads/Mutex>
19#include <OpenThreads/ScopedLock>
28class OSG_EXPORT Stats : public osg::Referenced
32 Stats(const std::string& name);
34 Stats(const std::string& name, unsigned int numberOfFrames);
36 void setName(const std::string& name) { _name = name; }
37 const std::string& getName() const { return _name; }
39 void allocate(unsigned int numberOfFrames);
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; }
44 typedef std::map<std::string, double> AttributeMap;
45 typedef std::vector<AttributeMap> AttributeMapList;
47 bool setAttribute(unsigned int frameNumber, const std::string& attributeName, double value);
49 inline bool getAttribute(unsigned int frameNumber, const std::string& attributeName, double& value) const
51 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
52 return getAttributeNoMutex(frameNumber, attributeName, value);
55 bool getAveragedAttribute(const std::string& attributeName, double& value, bool averageInInverseSpace=false) const;
57 bool getAveragedAttribute(unsigned int startFrameNumber, unsigned int endFrameNumber, const std::string& attributeName, double& value, bool averageInInverseSpace=false) const;
59 inline AttributeMap& getAttributeMap(unsigned int frameNumber)
61 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
62 return getAttributeMapNoMutex(frameNumber);
65 inline const AttributeMap& getAttributeMap(unsigned int frameNumber) const
67 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
68 return getAttributeMapNoMutex(frameNumber);
71 typedef std::map<std::string, bool> CollectMap;
73 void collectStats(const std::string& str, bool flag) { _collectMap[str] = flag; }
75 inline bool collectStats(const std::string& str) const
77 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
79 CollectMap::const_iterator itr = _collectMap.find(str);
80 return (itr != _collectMap.end()) ? itr->second : false;
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;
90 bool getAttributeNoMutex(unsigned int frameNumber, const std::string& attributeName, double& value) const;
92 AttributeMap& getAttributeMapNoMutex(unsigned int frameNumber);
93 const AttributeMap& getAttributeMapNoMutex(unsigned int frameNumber) const;
96 int getIndex(unsigned int frameNumber) const
98 // reject frame that are in the future
99 if (frameNumber > _latestFrameNumber) return -1;
101 // reject frames that are too early
102 if (frameNumber < getEarliestFrameNumber()) return -1;
104 if (frameNumber >= _baseFrameNumber) return frameNumber - _baseFrameNumber;
105 else return static_cast<int>(_attributeMapList.size()) - (_baseFrameNumber-frameNumber);
110 mutable OpenThreads::Mutex _mutex;
112 unsigned int _baseFrameNumber;
113 unsigned int _latestFrameNumber;
115 AttributeMapList _attributeMapList;
116 AttributeMap _invalidAttributeMap;
118 CollectMap _collectMap;