/usr/include/trilinos/Teuchos_SimpleObjectDB.hpp is in libtrilinos-teuchos-dev 12.4.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | // @HEADER
// ***********************************************************************
//
// Teuchos: Common Tools Package
// Copyright (2004) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
// @HEADER
#ifndef TEUCHOS_SIMPLE_OBJECT_DB_HPP
#define TEUCHOS_SIMPLE_OBJECT_DB_HPP
#include "Teuchos_Array.hpp"
#include "Teuchos_ConstNonconstObjectContainer.hpp"
#include "Teuchos_as.hpp"
/*! \file Teuchos_SimpleObjectDB.hpp
\brief A simple object table class for Teuchos
*/
/*! \class Teuchos::SimpleObjectDB
\brief This class provides a central place to store objects
*/
namespace Teuchos
{
/** \brief Simple object object database.
*
* This simple class stores and retrieves objects (using an simple integral
* type index).
*
* The main advantages of using this class over a simple Teuchos::Array<RCP<T>
* > object are:
* <ul>
*
* <li>Object indexes are reused to allow large numbers of
* store[Nonconst,Const]Obj()/removeObj() calls without growing memory. A
* simple Teuchos::Array implementation would not allow for that.</li>
*
* <li>Seemlessly handle both const and non-const objects using the runtime
* protection of const.</li>
*
* </ul>
*
* NOTE: The type should always be a nonconst type (i.e. <tt>T=SomeType</tt>)
* and not a const type (i.e. not <tt>T=const SomeType</tt>). The class will
* not compile if given a const type.
*/
template <class T>
class SimpleObjectDB
{
public:
/** \brief Construct an empty DB. */
SimpleObjectDB();
/** \brief Return the current size of the table.
*
* This number will be greater or equal to the number of currently stored
* objects. This function is really only of interest to unit testing code.
*/
int tableSize() const;
/** \brief Return number of free indexes.
*
* This is the number of free table indexes that are ready to be recycled
* for objects to be added.
*/
int numFreeIndexes() const;
/** \brief Return number of non-null stored objects.
*/
int numObjects() const;
/** \brief Store a non-const object.
*
* \return Index to the object.
*/
int storeNonconstObj(const RCP<T> &obj);
/** \brief Store a const object.
*
* \return Index to the object.
*/
int storeConstObj(const RCP<const T> &obj);
/** \brief Performs an rcp_dynamic_cast<>() to store the obejct.
*
* If the dynamic cast fails, then an exception is thrown.
*
* \return Index to the object.
*/
template <class TOld>
int storeCastedNonconstObj(const RCP<TOld> & robj_old);
/** \brief Remove a stored object without returning it.
*
* This function avoids reference counting overhead when the user
* just wants the object to go away and does not care to grab it.
*/
void removeObj(const int index);
/** \breif Remove a stored non-const object from the table and return it.
*
* This will throw NonconstAccessError if a const object was added with
* <tt>storeConstObj()</tt>.
*/
RCP<T> removeNonconstObj(const int index);
/** \breif Remove a stored const object from the table and return it.
*/
RCP<const T> removeConstObj(const int index);
/** \brief Remove an indexed object from the table.
*
* If this object is solely owned by this table, then it will be destroyed
* according to the embedded RCP destruction policy object.
*
* \return The reference count of the object after it removed from the
* table. If this count is 0, then the object was freed as a side-effect.
*/
int removeRCP(int &index);
/** \brief Get an object (nonconst persisting association).
*
* This will throw NonconstAccessError if a const object was added with
* <tt>storeConstObj()</tt>.
*/
RCP<T> getNonconstObjRCP(const int index);
/** \brief Get an object (const persisting association).
*/
RCP<const T> getConstObjRCP(const int index) const;
/** \brief Get an object (nonconst semi-persisting association).
*
* This will throw NonconstAccessError if a const object was added with
* <tt>storeConstObj()</tt>.
*/
Ptr<T> getNonconstObjPtr(const int index);
/** \brief Get an object (const semi-persisting association).
*/
Ptr<const T> getConstObjPtr(const int index) const;
/** \brief Clear out all storage.
*
* Clears out the table storage and all RCPs to stored objects. Any objects
* solely owned by this table will be freed.
*/
void purge();
private:
typedef Array<ConstNonconstObjectContainer<T> > tableOfObjects_t;
typedef Array<int> freedIndices_t;
tableOfObjects_t tableOfObjects_;
freedIndices_t freedIndices_;
void validateIndex(const int index) const;
template <class T2>
int storeObjectImpl(const RCP<T2> &robj);
void removeObjImpl(const int index);
};
/** \brief Nonmember constructor */
template <class T>
RCP<SimpleObjectDB<T> > createSimpleObjectDB()
{
return rcp(new SimpleObjectDB<T>);
}
//
// Template definitions
//
template <class T>
SimpleObjectDB<T>::SimpleObjectDB()
{}
template <class T>
int SimpleObjectDB<T>::tableSize() const
{
return tableOfObjects_.size();
}
template <class T>
int SimpleObjectDB<T>::numFreeIndexes() const
{
return freedIndices_.size();
}
template <class T>
int SimpleObjectDB<T>::numObjects() const
{
return tableSize() - numFreeIndexes();
}
template <class T>
int SimpleObjectDB<T>::storeNonconstObj(const RCP<T> &obj)
{
return storeObjectImpl(obj);
}
template <class T>
int SimpleObjectDB<T>::storeConstObj(const RCP<const T> &obj)
{
return storeObjectImpl(obj);
}
template <class T>
template <class TOld>
int SimpleObjectDB<T>::storeCastedNonconstObj(const RCP<TOld> & robj_old)
{
return storeNonconstObj(rcp_dynamic_cast<T>(robj_old, true));
}
template <class T>
void SimpleObjectDB<T>::removeObj(const int index)
{
validateIndex(index);
removeObjImpl(index);
}
template <class T>
RCP<T> SimpleObjectDB<T>::removeNonconstObj(const int index)
{
validateIndex(index);
const RCP<T> obj = tableOfObjects_[index].getNonconstObj();
removeObjImpl(index);
return obj;
}
template <class T>
RCP<const T> SimpleObjectDB<T>::removeConstObj(const int index)
{
validateIndex(index);
const RCP<const T> obj = tableOfObjects_[index].getConstObj();
removeObjImpl(index);
return obj;
}
template <class T>
int SimpleObjectDB<T>::removeRCP(int &index)
{
const int index_in = index;
validateIndex(index);
const int cnt = tableOfObjects_[index_in].count();
removeObjImpl(index_in);
index = -1;
return (cnt - 1);
}
template <class T>
RCP<T> SimpleObjectDB<T>::getNonconstObjRCP(const int index)
{
validateIndex(index);
return tableOfObjects_[index].getNonconstObj();
}
template <class T>
RCP<const T> SimpleObjectDB<T>::getConstObjRCP(const int index) const
{
validateIndex(index);
return tableOfObjects_[index].getConstObj();
}
template <class T>
Ptr<T> SimpleObjectDB<T>::getNonconstObjPtr(const int index)
{
validateIndex(index);
return tableOfObjects_[index].getNonconstObj().ptr();
}
template <class T>
Ptr<const T> SimpleObjectDB<T>::getConstObjPtr(const int index) const
{
validateIndex(index);
return tableOfObjects_[index].getConstObj().ptr();
}
template <class T>
void SimpleObjectDB<T>::purge()
{
// Wipe out all memory (see Item 82 in "C++ Coding Standards")
tableOfObjects_t().swap(tableOfObjects_);
freedIndices_t().swap(freedIndices_);
}
// private
template <class T>
void SimpleObjectDB<T>::validateIndex(const int index) const
{
using Teuchos::as;
TEUCHOS_TEST_FOR_EXCEPTION(
!(0 <= index && index < as<int>(tableOfObjects_.size())),
RangeError,
"Error, the object index = " << index << " falls outside of the range"
<< " of valid objects [0,"<<tableOfObjects_.size()<<"]");
const RCP<const T> &obj = tableOfObjects_[index].getConstObj();
TEUCHOS_TEST_FOR_EXCEPTION(is_null(obj), NullReferenceError,
"Error, the object at index "<<index<<" of type "
<<TypeNameTraits<T>::name()<<" has already been deleted!");
}
template <class T>
template <class T2>
int SimpleObjectDB<T>::storeObjectImpl(const RCP<T2> & robj)
{
robj.assert_not_null();
int index = -1;
if (freedIndices_.size() != 0) {
index = freedIndices_.back();
freedIndices_.pop_back();
tableOfObjects_[index].initialize(robj);
} else {
tableOfObjects_.push_back(robj);
index = tableOfObjects_.size() - 1;
}
return index;
}
template <class T>
void SimpleObjectDB<T>::removeObjImpl(const int index)
{
tableOfObjects_[index] = null;
freedIndices_.push_back(index);
}
} // end namespace Teuchos
#endif // TEUCHOS_SIMPLE_OBJECT_DB_HPP
|