1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
13 /*---------------------------------------------------------------------------*\
16 * This library is free software; you can redistribute it and/or modify it *
17 * under the terms of the GNU Library General Public License as published *
18 * by the Free Software Foundation, version 2. *
20 * This library is distributed in the hope that it will be useful, but *
21 * WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
23 * Library General Public License for more details. *
25 * You should have received a copy of the GNU Library General Public *
26 * License along with this library; if not, write to the Free Software *
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
29 \*---------------------------------------------------------------------------*/
31 /*---------------------------------------------------------------------------*\
39 \*---------------------------------------------------------------------------*/
43 /*-------------------------------------------------------------------------*/
45 /*! Returns the number StatElems this collector can currently store.
46 Normally this corresponds to the number of registered StatElems in the
47 system, only if new ones were registered after this collector instance was
48 created the numbers can differ.
50 \warning This is not the number of StatElems actually stored in this
53 \return Number of StatElems this collector can currently store.
57 UInt32 StatCollector::getNumOfElems(void) const
59 return UInt32(_elemVec.size());
62 /*-------------------------------------------------------------------------*/
64 /*! Tests if the given \a id is valid for this collector, i.e. if it can be
65 used to refer to a StatElem in it.
67 \warning It does not test if there actually is a StatElem with this id in
68 this collector, only if there can be one.
72 bool StatCollector::isValidID(Int32 elemId) const
74 return (elemId >= 0) && (elemId < static_cast<Int32>(_elemVec.size()));
77 /*-------------------------------------------------------------------------*/
79 /*! Return a pointer to the StatElem for the id.
80 This function has two modes. By default it's careful and sets
81 the create argument to true. In this case it checks whether the
82 Collector already contains a StatElem for the given id and creates
83 it if necessary. If you know that your Collector already contains that
84 element (because you explicitly created it outside the testing loop)
85 you can set it to false and speed up the whole function a bit.
89 StatElem *StatCollector::getElem(Int32 elemId, bool create)
93 // This only happens when dynamically adding StatElems
94 // but it's really nasty if it happens.
96 if(elemId >= static_cast<Int32>(_elemVec.size()))
99 StatElem *elem = _elemVec[elemId];
103 StatElemDescBase *desc = StatElemDescBase::getDesc(elemId);
104 elem = _elemVec[elemId] = desc->createElem();
110 return isValidID(elemId) ? _elemVec[elemId] : NULL;
114 /*! Return a pointer to the StatElem for the id.
115 It will return NULL if the element doesn't exist yet.
117 \param[in] elemId Id of the StatElem to return.
118 \return StatElem with given id, or NULL if it does not exist.
121 inline StatElem *StatCollector::getElem(Int32 elemId) const
123 return isValidID(elemId) ? _elemVec[elemId] : NULL;
126 /*-------------------------------------------------------------------------*/
128 /*! Return the StatElem for the given descriptor \a desc. If the StatElem
129 does not yet exist in this collector and \a create is true, it will be
130 created. If \a create is false only existing StatElems will be returned and
131 NULL for those that do not exist yet.
133 \param[in] desc Descriptor for the StatElem to return.
134 \param[in] create If true, not existing StatElem will be created.
135 \return StatElem for the given descriptor, or NULL if it does not exist.
139 StatElem *StatCollector::getElem(StatElemDescBase &desc, bool create)
141 return getElem(desc.getID(), create);
144 /*-------------------------------------------------------------------------*/
146 /*! Return a typed StatElem for the given descriptor \a desc. If the StatElem
147 does not yet exist in this collector and \a create is true, it will be
148 created. If \a create is false only existing StatElems will be returned and
149 NULL for those that do not exist yet.
150 The return value will be a pointer to a subclass of StatElem, the exact type
151 depends on the type of \a desc.
153 \param[in] desc Descriptor for the StatElem to return.
154 \param[in] create If true, not existing StatElem will be created.
155 \return StatElem for the given descriptor, or NULL if it does not exist.
158 template<class T> inline
159 T *StatCollector::getElem(StatElemDesc<T> &desc, bool create)
161 return reinterpret_cast<T*>(getElem(desc.getID(), create));