fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Base / Statistics / OSGStatIntOnceElem.cpp
blobffe7e732c99a999155c3035f89752ffd23852158
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"
47 #include "OSGSysFieldTraits.h"
49 #include "OSGStatIntOnceElem.h"
51 #include <boost/format.hpp>
53 OSG_USING_NAMESPACE
56 /***************************************************************************\
57 * Description *
58 \***************************************************************************/
60 /*! \class OSG::StatIntOnceElem
62 The StatIntOnceElem is similar to the OSG::StatIntElem, but it keeps
63 track of who has contributed already and only allows each ID to
64 contribute once. \ref PageSystemStatistics for details.
67 /***************************************************************************\
68 * Instance methods *
69 \***************************************************************************/
71 /*------------- constructors & destructors --------------------------------*/
73 StatIntOnceElem::StatIntOnceElem(StatElemDescBase *desc) :
74 Inherited(desc),
75 _value ( 0),
76 _ids ( )
80 StatElem *StatIntOnceElem::create(StatElemDescBase *desc)
82 return new StatIntOnceElem(desc);
86 StatIntOnceElem::~StatIntOnceElem(void)
90 /*------------------------------ access -----------------------------------*/
92 void StatIntOnceElem::putToString(
93 std::string &str, const std::string &format) const
95 if(format.empty())
97 FieldTraits<Int64>::putToString(_value, str);
99 else
101 std::string formatCopy = format;
102 std::string::size_type pos = formatCopy.find("%");
103 Real64 val = _value;
105 if(pos != std::string::npos)
107 if((pos = formatCopy.find("%KB")) != std::string::npos)
109 formatCopy.replace(pos, 3, "%.2f");
110 val /= 1024.f;
112 else if((pos = formatCopy.find("%MB")) != std::string::npos)
114 formatCopy.replace(pos, 3, "%.2f");
115 val /= 1024.f * 1024.f;
117 else if((pos = formatCopy.find("%GB")) != std::string::npos)
119 formatCopy.replace(pos, 3, "%.2f");
120 val /= 1024.f * 1024.f * 1024.f;
124 boost::format fmt(formatCopy);
126 fmt % val;
128 str = fmt.str();
132 bool StatIntOnceElem::getFromCString(const Char8 *&inVal)
134 return FieldTraits<Int64>::getFromCString(_value, inVal);
137 Real64 StatIntOnceElem::getValue(void) const
139 return static_cast<Real64>(get());
142 void StatIntOnceElem::reset(void)
144 _value = 0;
146 _ids.clear();
149 /*-------------------------- assignment -----------------------------------*/
151 StatIntOnceElem& StatIntOnceElem::operator = (const StatIntOnceElem &source)
153 if (this == &source)
154 return *this;
156 _value = source._value;
157 _ids = source._ids;
159 return *this;
162 /*-------------------------- comparison -----------------------------------*/
164 bool StatIntOnceElem::operator < (const StatIntOnceElem &other) const
166 return this->get() < other.get();
169 /*--------------------------- creation ------------------------------------*/
171 StatElem *StatIntOnceElem::clone(void) const
173 StatIntOnceElem *e = new StatIntOnceElem(getDesc());
175 *e = *this;
177 return e;
180 /*--------------------------- operators ------------------------------------*/
182 StatElem &StatIntOnceElem::operator += (const StatElem &other)
184 const StatIntOnceElem *o = dynamic_cast<const StatIntOnceElem *>(&other);
186 _value += o->_value;
188 IdHash::const_iterator it = o->_ids.begin();
190 while(it != o->_ids.end())
192 _ids.insert(*it);
194 ++it;
197 return *this;