changed: auto add updateData callback to stages so that stagedata can be updated...
[opensg.git] / Source / Base / Statistics / OSGStatCollector.cpp
blob9375cd484002a958a19feb505e91227a4f5ff26f
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 "OSGStatElemDesc.h"
49 #include "OSGStatElem.h"
51 #include "OSGStatCollector.h"
53 OSG_BEGIN_NAMESPACE
55 /***************************************************************************\
56 * Description *
57 \***************************************************************************/
59 /*! \class OSG::StatCollector
61 The StatCollector is responsible for managing a set of OSG::StatElem
62 elements, see \ref PageSystemStatistics for details.
65 /***************************************************************************\
66 * Instance methods *
67 \***************************************************************************/
69 /*-------------------------------------------------------------------------*\
70 - public -
71 \*-------------------------------------------------------------------------*/
73 StatCollector::StatCollector(void) :
74 Inherited()
76 refitElemNum();
79 /*! copy constructor
82 StatCollector::StatCollector(const StatCollector &source) :
83 Inherited(source),
84 _elemVec ( )
86 _elemVec.resize(source._elemVec.size());
88 for(UInt32 i = 0; i < source._elemVec.size(); ++i)
90 if(source._elemVec[i])
92 _elemVec[i] = source._elemVec[i]->clone();
94 else
96 _elemVec[i] = NULL;
101 StatCollector *StatCollector::create(void)
103 return new StatCollector();
106 StatCollector::~StatCollector(void)
108 for(UInt32 i = 0; i < _elemVec.size(); ++i)
109 delete _elemVec[i];
112 /*-------------------------- your_category---------------------------------*/
114 /*! Increase the size of the StatCollector's data array. This is called during
115 construction and will only be needed later, when a new StatElem has been
116 added after the StatCollector was instanced. This will very rarely be the
117 case.
119 void StatCollector::refitElemNum(void)
121 SizeT eN = _elemVec.size();
122 SizeT dN = StatElemDescBase::getNumOfDescs();
124 if(eN != dN)
126 _elemVec.resize(dN, 0);
130 /*! Convert the current contents into a string. This
131 string can be used as a compact representation of the data, and as input
132 for StatCollector::getFromString.
134 void StatCollector::putToString(std::string &str) const
136 bool first = true;
138 str = "{";
140 StatElemStoreConstIt it = _elemVec.begin();
141 StatElemStoreConstIt endIt = _elemVec.end();
143 for(; it != endIt; ++it)
145 if(*it != NULL)
147 std::string elem;
149 if(!first)
150 str.append("|");
152 str.append((*it)->getDesc()->getName().c_str());
153 str.append("=");
154 (*it)->putToString(elem);
155 str.append(elem);
156 first = false;
159 str.append("}");
163 /*! Set the contents from a string. The string has to have the format that is
164 used by StatCollector::putToString.
166 bool StatCollector::getFromCString(const Char8 *&inVal)
168 const Char8 *c = inVal;
170 if(*c++ != '{')
171 return false;
173 StatElemDescBase *desc;
174 StatElem *elem;
176 clearElems();
178 while(*c && *c != '}')
180 const Char8 *end = c;
182 while(*end != 0 && *end != '=' && *end != '}' && *end != '|')
183 end++;
185 if(*end == 0 || *end == '}' || *end == '|')
186 return false;
188 std::string name(c, end - c);
189 desc = StatElemDescBase::findDescByName(name.c_str());
191 if(!desc)
192 return false;
194 elem = getElem(*desc);
196 c = end = end + 1;
197 while(*end != 0 && *end != '}' && *end != '|')
198 end++;
200 if(*end == 0)
201 return false;
203 std::string val(c, end - c);
204 const Char8 *valp = val.c_str();
205 if(!elem->getFromCString(valp))
206 return false;
208 c = end + 1;
210 return true;
213 /*! Get the value of the named element, if it exists, return false if not.
216 bool StatCollector::getValue(std::string &name, Real64 &val)
218 StatElemDescBase *desc = StatElemDescBase::findDescByName(name.c_str());
220 if(!desc)
221 return false;
223 StatElem *el = getElem(*desc, false);
225 if(!el)
226 return false;
228 val = el->getValue();
230 return true;
233 /*! Remove all elements from the collector.
235 void StatCollector::clearElems(void)
237 StatElemStoreIt it = _elemVec.begin();
238 StatElemStoreConstIt endIt = _elemVec.end();
240 for(; it != endIt; ++it)
242 if(*it != NULL)
244 delete *it;
246 *it = NULL;
249 _elemVec.clear();
252 /*! Reset all elements to the start value.
254 void StatCollector::reset(StatElemDescBase::ResetMode mode)
256 StatElemStoreIt it = _elemVec.begin();
257 StatElemStoreConstIt endIt = _elemVec.end();
259 for(; it != endIt; ++it)
261 if(*it != NULL && (*it)->getDesc()->getResetMode() >= mode)
263 (*it)->reset();
268 /*-------------------------- assignment -----------------------------------*/
270 const StatCollector &StatCollector::operator = (const StatCollector &source)
272 if(this == &source)
273 return *this;
275 _elemVec = source._elemVec;
277 return *this;
280 /*-------------------------- comparison -----------------------------------*/
282 /*! The comparison is only done on the addresses, as a real comparison is not
283 well defined on a StatCollector.
285 bool StatCollector::operator < (const StatCollector &other) const
287 return this < &other;
290 bool StatCollector::operator == (const StatCollector &rhs ) const
292 return false;
295 StatCollector StatCollector::operator + (const StatCollector &other)
297 StatCollector res(*this);
299 res += other;
301 return res;
304 StatCollector &StatCollector::operator += (const StatCollector &other)
306 if(other._elemVec.size() > _elemVec.size())
308 _elemVec.resize(other._elemVec.size());
311 for(UInt32 i = 0; i < _elemVec.size(); ++i)
313 if(_elemVec[i])
315 *_elemVec[i] += *other._elemVec[i];
319 return *this;
322 OSG_END_NAMESPACE
324 #include "OSGSField.h"
325 #include "OSGSField.ins"
327 #include "OSGMField.h"
328 #include "OSGMField.ins"
330 #include "OSGFieldContainer.h"
332 OSG_BEGIN_NAMESPACE
334 #ifndef OSG_DO_DOC
336 /*-------------------------- field instantiations -------------------------*/
338 DataType FieldTraits<StatCollectorP>::_type("StatCollectorP",
339 "TypeRoot");
342 DataType &FieldTraits<StatCollectorP>::getType(void)
344 return _type;
346 #endif
348 OSG_FIELD_DLLEXPORT_DEF1(SField, StatCollectorP);
350 OSG_END_NAMESPACE