File: Stats

package info (click to toggle)
openscenegraph 2.8.3-5
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 33,968 kB
  • ctags: 30,935
  • sloc: cpp: 287,169; ansic: 9,050; sh: 654; yacc: 548; objc: 374; makefile: 264; lex: 151; perl: 119
file content (125 lines) | stat: -rw-r--r-- 4,582 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSG_STATS
#define OSG_STATS 1

#include <osg/Referenced>
#include <OpenThreads/Mutex>
#include <OpenThreads/ScopedLock>

#include <string>
#include <map>
#include <vector>
#include <ostream>

namespace osg {

class OSG_EXPORT Stats : public osg::Referenced
{
    public:

        Stats(const std::string& name);

        Stats(const std::string& name, unsigned int numberOfFrames);
        
        void setName(const std::string& name) { _name = name; }
        const std::string& getName() const { return _name; }
        
        void allocate(unsigned int numberOfFrames);
        
        int getEarliestFrameNumber() const { return _latestFrameNumber < static_cast<int>(_attributeMapList.size()) ? 0 : _latestFrameNumber - static_cast<int>(_attributeMapList.size()) + 1; }
        int getLatestFrameNumber() const { return _latestFrameNumber; }
        
        typedef std::map<std::string, double> AttributeMap;
        typedef std::vector<AttributeMap> AttributeMapList;

        bool setAttribute(int frameNumber, const std::string& attributeName, double value);
        
        inline bool getAttribute(int frameNumber, const std::string& attributeName, double& value) const
        {
            OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
            return getAttributeNoMutex(frameNumber, attributeName, value);
        }

        bool getAveragedAttribute(const std::string& attributeName, double& value, bool averageInInverseSpace=false) const;
        
        bool getAveragedAttribute(int startFrameNumber, int endFrameNumber, const std::string& attributeName, double& value, bool averageInInverseSpace=false) const;        
        
        inline AttributeMap& getAttributeMap(int frameNumber)
        {
            OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
            return getAttributeMapNoMutex(frameNumber);
        }
        
        inline const AttributeMap& getAttributeMap(int frameNumber) const
        {
            OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
            return getAttributeMapNoMutex(frameNumber);
        }
        
        typedef std::map<std::string, bool> CollectMap;
        
        void collectStats(const std::string& str, bool flag) { _collectMap[str] = flag; }
        
        inline bool collectStats(const std::string& str) const
        {
            OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
            
            CollectMap::const_iterator itr = _collectMap.find(str); 
            return (itr !=  _collectMap.end()) ? itr->second : false;
        }

        void report(std::ostream& out, const char* indent=0) const;
        void report(std::ostream& out, unsigned int frameNumber, const char* indent=0) const;
        
    protected:
    
        virtual ~Stats() {}
        
        bool getAttributeNoMutex(int frameNumber, const std::string& attributeName, double& value) const;

        AttributeMap& getAttributeMapNoMutex(int frameNumber);
        const AttributeMap& getAttributeMapNoMutex(int frameNumber) const;


        int getIndex(int frameNumber) const
        {
            // reject frame that are in the future
            if (frameNumber > _latestFrameNumber) return -1;
            
            // reject frames that are too early
            if (frameNumber < getEarliestFrameNumber()) return -1;
            
            if (frameNumber >= _baseFrameNumber) return frameNumber - _baseFrameNumber;
            else return static_cast<int>(_attributeMapList.size()) - (_baseFrameNumber-frameNumber);
        }

        std::string         _name;
        
        mutable OpenThreads::Mutex  _mutex;
        
        int                 _baseFrameNumber;
        int                 _latestFrameNumber;
        
        AttributeMapList    _attributeMapList;
        AttributeMap        _invalidAttributeMap;
        
        CollectMap          _collectMap;
        
};


}

#endif