changed: auto add updateData callback to stages so that stagedata can be updated...
[opensg.git] / Source / Base / Statistics / OSGStatTimeElem.cpp
blob60ca8b43fb90862454195988a28043631e63c51a
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include "OSGStatTimeElem.h"
49 #include "OSGBaseFieldTraits.h"
51 #include <boost/format.hpp>
53 OSG_USING_NAMESPACE
56 /***************************************************************************\
57 * Description *
58 \***************************************************************************/
60 /*! \class OSG::StatTimeElem
62 The StatIntElem keeps a OSG::Time value for time measurements,
63 see \ref PageSystemStatistics for details.
66 /***************************************************************************\
67 * Instance methods *
68 \***************************************************************************/
70 /*------------- constructors & destructors --------------------------------*/
72 StatTimeElem::StatTimeElem(StatElemDescBase *desc) :
73 StatElem (desc),
74 _startTime( 0),
75 _time ( 0)
79 StatElem *StatTimeElem::create(StatElemDescBase *desc)
81 return new StatTimeElem(desc);
85 StatTimeElem::~StatTimeElem(void)
89 /*------------------------------ access -----------------------------------*/
91 void StatTimeElem::putToString(
92 std::string &str, const std::string &format) const
94 if(format.empty())
96 // Confusing if %e is used.
98 Char8 temp[64];
100 sprintf(temp, "%f", _time);
102 str.assign(temp);
104 else
106 std::string formatCopy = format;
107 std::string::size_type pos = formatCopy.find("%");
108 Time val = _time;
110 if(pos != std::string::npos)
112 if((pos = formatCopy.find("%ms")) != std::string::npos)
114 formatCopy.replace(pos, 3, "%.2f");
115 val *= 1000.f;
117 else if((pos = formatCopy.find("%r")) != std::string::npos)
119 formatCopy.replace(pos, 2, "%");
120 if(val <= TypeTraits<Time>::ZeroEps())
122 val = 0.;
124 else
126 val = 1. / val;
131 boost::format fmt(formatCopy);
133 fmt % val;
135 str = fmt.str();
139 bool StatTimeElem::getFromCString(const Char8 *&inVal)
141 return FieldTraits<Time, 1>::getFromCString(_time, inVal);
144 Real64 StatTimeElem::getValue(void) const
146 return static_cast<Real64>(getTime());
149 void StatTimeElem::reset(void)
151 _time = 0;
154 /*-------------------------- assignment -----------------------------------*/
156 StatTimeElem& StatTimeElem::operator = (const StatTimeElem &source)
158 if (this == &source)
159 return *this;
161 _startTime = source._startTime;
162 _time = source._time;
164 return *this;
167 /*-------------------------- comparison -----------------------------------*/
169 bool StatTimeElem::operator < (const StatTimeElem &other) const
171 return _time < other._time;
174 /*--------------------------- creation ------------------------------------*/
176 StatElem *StatTimeElem::clone(void) const
178 StatTimeElem *e = new StatTimeElem(getDesc());
180 *e = *this;
182 return e;
185 /*--------------------------- operators ------------------------------------*/
187 StatElem &StatTimeElem::operator += (const StatElem &other)
189 const StatTimeElem *o = dynamic_cast<const StatTimeElem *>(&other);
191 _time += o->_time;
193 return *this;