/usr/include/deal.II/algorithms/any_data.h is in libdeal.ii-dev 8.4.2-2+b1.
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 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | // ---------------------------------------------------------------------
//
// Copyright (C) 2000 - 2015 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------
#ifndef dealii__any_data_h
#define dealii__any_data_h
#include <deal.II/base/config.h>
#include <deal.II/base/exceptions.h>
#include <deal.II/base/subscriptor.h>
#include <boost/any.hpp>
#include <vector>
#include <algorithm>
#include <typeinfo>
DEAL_II_NAMESPACE_OPEN
/**
* Store any amount of any type of data accessible by an identifier string.
*
* @todo GK: Deprecate access to AnyData by index and change to a map.
*/
class AnyData :
public Subscriptor
{
public:
/// Default constructor for empty object
AnyData();
/// Number of stored data objects.
unsigned int size() const;
/// Add a new data object
template <typename type>
void add(type entry, const std::string &name);
/**
* @brief Merge the data of another AnyData to the end of this object.
*/
void merge(const AnyData &other);
/**
* @brief Access to stored data object by name.
*
* Find the object with given name, try to convert it to <tt>type</tt> and
* return it. This function throws an exception if either the name does not
* exist or if the conversion fails. If such an exception is not desired,
* use try_read() instead.
*/
template <typename type>
type entry (const std::string &name);
/**
* @brief Read-only access to stored data object by name.
*
* Find the object with given name, try to convert it to <tt>type</tt> and
* return it. This function throws an exception if either the name does not
* exist or if the conversion fails. If such an exception is not desired,
* use try_read() instead.
*/
template <typename type>
const type entry (const std::string &name) const;
/**
* @brief Dedicated read only access by name.
*
* For a constant object, this function equals entry(). For a non-const
* object, it forces read only access to the data. In particular, it throws
* an exception if the object is not found or cannot be converted to type.
* If such an exception is not desired, use try_read() instead.
*
* @warning Do not use this function for stored objects which are pointers.
* Use read_ptr() instead!
*/
template <typename type>
const type read (const std::string &name) const;
/**
* @brief Dedicated read only access by name for pointer data.
*
* If the stored data object is a pointer to a constant object, the logic of
* access becomes fairly complicated. Namely, the standard read function may
* fail, depending on whether it was a const pointer or a regular pointer.
* This function fixes the logic and ascertains that the object does not
* become mutable by accident.
*/
template <typename type>
const type *read_ptr (const std::string &name) const;
/**
* Perform the same action as read_ptr(), but do not throw an exception if
* the pointer does not exist. Return a null pointer instead.
*/
template <typename type>
const type *try_read_ptr (const std::string &name) const;
/**
* @brief Dedicated read only access by name without exceptions.
*
* This function tries to find the name in the list and return a pointer to
* the associated object. If either the name is not found or the object
* cannot be converted to the return type, a null pointer is returned.
*/
template <typename type>
const type *try_read (const std::string &name) const;
/**
* Access to stored data object by index.
*/
template <typename type>
type entry (const unsigned int i);
/// Read-only access to stored data object by index.
template <typename type>
const type entry (const unsigned int i) const;
/// Dedicated read only access.
template <typename type>
const type read (const unsigned int i) const;
/// Dedicated read only access to pointer object.
template <typename type>
const type *read_ptr (const unsigned int i) const;
/// Dedicated read only access to pointer object without exception.
template <typename type>
const type *try_read_ptr (const unsigned int i) const;
/// Dedicated read only access without exception.
template <typename type>
const type *try_read (const unsigned int i) const;
/// Name of object at index.
const std::string &name(const unsigned int i) const;
/**
* @brief Find index of a named object
*
* Try to find the object and return its index in the list. Throw an
* exception if the object has not been found.
*/
unsigned int find(const std::string &name) const;
/**
* @brief Try to find index of a named object
*
* Try to find the object and return its index in the list. returns
* numbers::invalid_unsigned_int if the name was not found.
*/
unsigned int try_find(const std::string &name) const;
/// Find out if object is of a certain type
template <typename type>
bool is_type(const unsigned int i) const;
/// List the contents to a stream
template <class StreamType>
void list (StreamType &os) const;
/// An entry with this name does not exist in the AnyData object.
DeclException1(ExcNameNotFound, std::string,
<< "No entry with the name " << arg1 << " exists.");
/// The requested type and the stored type are different
DeclException2(ExcTypeMismatch,
char *, char *,
<< "The requested type " << arg1
<< " and the stored type " << arg2
<< " must coincide");
/**
* Exception indicating that a function expected a vector to have a certain
* name, but we store a different name in that position.
*/
DeclException2(ExcNameMismatch, int, std::string,
<< "Name at position " << arg1 << " is not equal to " << arg2);
private:
/// The stored data
std::vector<boost::any> data;
/// The names of the stored data
std::vector<std::string> names;
};
inline
AnyData::AnyData()
{}
unsigned int
inline
AnyData::size () const
{
AssertDimension(data.size(), names.size());
return data.size();
}
template <typename type>
inline
type
AnyData::entry (const unsigned int i)
{
AssertIndexRange(i, size());
type *p = boost::any_cast<type>(&data[i]);
Assert(p != 0,
ExcTypeMismatch(typeid(type).name(),data[i].type().name()));
return *p;
}
template <typename type>
inline
const type
AnyData::entry (const unsigned int i) const
{
AssertIndexRange(i, size());
const type *p = boost::any_cast<type>(&data[i]);
if (p==0 )
p = boost::any_cast<const type>(&data[i]);
Assert(p != 0,
ExcTypeMismatch(typeid(type).name(),data[i].type().name()));
return *p;
}
template <typename type>
inline
const type
AnyData::read(const unsigned int i) const
{
AssertIndexRange(i, size());
const type *p = boost::any_cast<type>(&data[i]);
if (p==0)
p = boost::any_cast<const type>(&data[i]);
Assert(p != 0,
ExcTypeMismatch(typeid(type).name(),data[i].type().name()));
return *p;
}
template <typename type>
inline
const type *
AnyData::read_ptr(const unsigned int i) const
{
AssertIndexRange(i, size());
const type *const *p = boost::any_cast<type *>(&data[i]);
if (p==0)
p = boost::any_cast<const type *>(&data[i]);
Assert(p != 0,
ExcTypeMismatch(typeid(type *).name(),data[i].type().name()));
return *p;
}
template <typename type>
inline
const type *
AnyData::try_read_ptr(const unsigned int i) const
{
AssertIndexRange(i, size());
const type *const *p = boost::any_cast<type *>(&data[i]);
if (p==0)
p = boost::any_cast<const type *>(&data[i]);
return *p;
}
template <typename type>
inline
const type *
AnyData::try_read(const unsigned int i) const
{
AssertIndexRange(i, size());
const type *p = boost::any_cast<type>(&data[i]);
if (p==0)
p = boost::any_cast<const type>(&data[i]);
return p;
}
inline
const std::string &
AnyData::name(const unsigned int i) const
{
AssertIndexRange(i, size());
return names[i];
}
inline
unsigned int
AnyData::try_find(const std::string &n) const
{
std::vector<std::string>::const_iterator it =
std::find(names.begin(), names.end(), n);
if (it == names.end())
return numbers::invalid_unsigned_int;
return it - names.begin();
}
inline
unsigned int
AnyData::find(const std::string &n) const
{
const unsigned int i = try_find(n);
Assert(i != numbers::invalid_unsigned_int, ExcNameNotFound(n));
return i;
}
template <typename type>
inline
bool
AnyData::is_type(const unsigned int i) const
{
return data[i].type() == typeid(type);
}
template <typename type>
inline
type
AnyData::entry (const std::string &n)
{
const unsigned int i = find(n);
type *p = boost::any_cast<type>(&data[i]);
Assert(p != 0,
ExcTypeMismatch(typeid(type).name(),data[i].type().name()));
return *p;
}
template <typename type>
inline
const type
AnyData::entry (const std::string &n) const
{
const unsigned int i = find(n);
const type *p = boost::any_cast<type>(&data[i]);
Assert(p != 0,
ExcTypeMismatch(typeid(type).name(),data[i].type().name()));
return *p;
}
template <typename type>
inline
const type
AnyData::read(const std::string &n) const
{
const unsigned int i = find(n);
const type *p = boost::any_cast<type>(&data[i]);
Assert(p != 0,
ExcTypeMismatch(typeid(type).name(),data[i].type().name()));
return *p;
}
template <typename type>
inline
const type *
AnyData::read_ptr(const std::string &n) const
{
const unsigned int i = find(n);
const type *const *p = boost::any_cast<type *>(&data[i]);
if (p==0)
p = boost::any_cast<const type *>(&data[i]);
Assert(p != 0,
ExcTypeMismatch(typeid(type).name(),data[i].type().name()));
return *p;
}
template <typename type>
inline
const type *
AnyData::try_read_ptr(const std::string &n) const
{
const unsigned int i = try_find(n);
if (i == numbers::invalid_unsigned_int)
return 0;
const type *const *p = boost::any_cast<type *>(&data[i]);
if (p==0)
p = boost::any_cast<const type *>(&data[i]);
return *p;
}
template <typename type>
inline
const type *
AnyData::try_read(const std::string &n) const
{
// Try to find name
std::vector<std::string>::const_iterator it =
std::find(names.begin(), names.end(), n);
// Return null pointer if not found
if (it == names.end())
return 0;
// Compute index and return casted pointer
unsigned int i=it-names.begin();
const type *p = boost::any_cast<type>(&data[i]);
return p;
}
template <typename type>
inline
void
AnyData::add(type ent, const std::string &n)
{
boost::any e = ent;
data.push_back(e);
names.push_back(n);
}
inline
void
AnyData::merge(const AnyData &other)
{
for (unsigned int i=0; i<other.size(); ++i)
{
names.push_back(other.names[i]);
data.push_back(other.data[i]);
}
}
template <class StreamType>
inline
void AnyData::list(StreamType &os) const
{
for (unsigned int i=0; i<names.size(); ++i)
{
os << i
<< '\t' << names[i]
<< '\t' << data[i].type().name()
<< std::endl;
}
}
//----------------------------------------------------------------------//
DEAL_II_NAMESPACE_CLOSE
#endif
|