fixed: gcc8 compile issues
[opensg.git] / Source / Base / Statistics / OSGStatCollector.cpp
blobf9486c15b5fa72bdf1b45f2e28b3cb037f3244fd
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(),
75 _elemVec ()
77 refitElemNum();
80 /*! copy constructor
83 StatCollector::StatCollector(const StatCollector &source) :
84 Inherited(source),
85 _elemVec ( )
87 _elemVec.resize(source._elemVec.size());
89 for(UInt32 i = 0; i < source._elemVec.size(); ++i)
91 if(source._elemVec[i])
93 _elemVec[i] = source._elemVec[i]->clone();
95 else
97 _elemVec[i] = NULL;
102 StatCollector *StatCollector::create(void)
104 return new StatCollector();
107 StatCollector::~StatCollector(void)
109 for(UInt32 i = 0; i < _elemVec.size(); ++i)
110 delete _elemVec[i];
113 /*-------------------------- your_category---------------------------------*/
115 /*! Increase the size of the StatCollector's data array. This is called during
116 construction and will only be needed later, when a new StatElem has been
117 added after the StatCollector was instanced. This will very rarely be the
118 case.
120 void StatCollector::refitElemNum(void)
122 SizeT eN = _elemVec.size();
123 SizeT dN = StatElemDescBase::getNumOfDescs();
125 if(eN != dN)
127 _elemVec.resize(dN, 0);
131 /*! Convert the current contents into a string. This
132 string can be used as a compact representation of the data, and as input
133 for StatCollector::getFromString.
135 void StatCollector::putToString(std::string &str) const
137 bool first = true;
139 str = "{";
141 StatElemStoreConstIt it = _elemVec.begin();
142 StatElemStoreConstIt endIt = _elemVec.end();
144 for(; it != endIt; ++it)
146 if(*it != NULL)
148 std::string elem;
150 if(!first)
151 str.append("|");
153 str.append((*it)->getDesc()->getName().c_str());
154 str.append("=");
155 (*it)->putToString(elem);
156 str.append(elem);
157 first = false;
160 str.append("}");
164 /*! Set the contents from a string. The string has to have the format that is
165 used by StatCollector::putToString.
167 bool StatCollector::getFromCString(const Char8 *&inVal)
169 const Char8 *c = inVal;
171 if(*c++ != '{')
172 return false;
174 StatElemDescBase *desc;
175 StatElem *elem;
177 clearElems();
179 while(*c && *c != '}')
181 const Char8 *end = c;
183 while(*end != 0 && *end != '=' && *end != '}' && *end != '|')
184 end++;
186 if(*end == 0 || *end == '}' || *end == '|')
187 return false;
189 std::string name(c, end - c);
190 desc = StatElemDescBase::findDescByName(name.c_str());
192 if(!desc)
193 return false;
195 elem = getElem(*desc);
197 c = end = end + 1;
198 while(*end != 0 && *end != '}' && *end != '|')
199 end++;
201 if(*end == 0)
202 return false;
204 std::string val(c, end - c);
205 const Char8 *valp = val.c_str();
206 if(!elem->getFromCString(valp))
207 return false;
209 c = end + 1;
211 return true;
214 /*! Get the value of the named element, if it exists, return false if not.
217 bool StatCollector::getValue(std::string &name, Real64 &val)
219 StatElemDescBase *desc = StatElemDescBase::findDescByName(name.c_str());
221 if(!desc)
222 return false;
224 StatElem *el = getElem(*desc, false);
226 if(!el)
227 return false;
229 val = el->getValue();
231 return true;
234 /*! Remove all elements from the collector.
236 void StatCollector::clearElems(void)
238 StatElemStoreIt it = _elemVec.begin();
239 StatElemStoreConstIt endIt = _elemVec.end();
241 for(; it != endIt; ++it)
243 if(*it != NULL)
245 delete *it;
247 *it = NULL;
250 _elemVec.clear();
253 /*! Reset all elements to the start value.
255 void StatCollector::reset(StatElemDescBase::ResetMode mode)
257 StatElemStoreIt it = _elemVec.begin();
258 StatElemStoreConstIt endIt = _elemVec.end();
260 for(; it != endIt; ++it)
262 if(*it != NULL && (*it)->getDesc()->getResetMode() >= mode)
264 (*it)->reset();
269 /*-------------------------- assignment -----------------------------------*/
271 const StatCollector &StatCollector::operator = (const StatCollector &source)
273 if(this == &source)
274 return *this;
276 _elemVec = source._elemVec;
278 return *this;
281 /*-------------------------- comparison -----------------------------------*/
283 /*! The comparison is only done on the addresses, as a real comparison is not
284 well defined on a StatCollector.
286 bool StatCollector::operator < (const StatCollector &other) const
288 return this < &other;
291 bool StatCollector::operator == (const StatCollector &rhs ) const
293 return false;
296 StatCollector StatCollector::operator + (const StatCollector &other)
298 StatCollector res(*this);
300 res += other;
302 return res;
305 StatCollector &StatCollector::operator += (const StatCollector &other)
307 if(other._elemVec.size() > _elemVec.size())
309 _elemVec.resize(other._elemVec.size());
312 for(UInt32 i = 0; i < _elemVec.size(); ++i)
314 if(_elemVec[i])
316 *_elemVec[i] += *other._elemVec[i];
320 return *this;
323 OSG_END_NAMESPACE
325 #include "OSGSField.h"
326 #include "OSGSField.ins"
328 #include "OSGMField.h"
329 #include "OSGMField.ins"
331 #include "OSGFieldContainer.h"
333 OSG_BEGIN_NAMESPACE
335 #ifndef OSG_DO_DOC
337 /*-------------------------- field instantiations -------------------------*/
339 DataType FieldTraits<StatCollectorP>::_type("StatCollectorP",
340 "TypeRoot");
343 DataType &FieldTraits<StatCollectorP>::getType(void)
345 return _type;
347 #endif
349 OSG_FIELD_DLLEXPORT_DEF1(SField, StatCollectorP)
351 OSG_END_NAMESPACE