Reference documentation for deal.II version 9.4.1
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
general_data_storage.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (C) 2019 - 2022 by the deal.II authors
4//
5// This file is part of the deal.II library.
6//
7// The deal.II library is free software; you can use it, redistribute
8// it, and/or modify it under the terms of the GNU Lesser General
9// Public License as published by the Free Software Foundation; either
10// version 2.1 of the License, or (at your option) any later version.
11// The full text of the license can be found in the file LICENSE.md at
12// the top level directory of deal.II.
13//
14// ---------------------------------------------------------------------
15
16#ifndef dealii_algorithms_general_data_storage_h
17#define dealii_algorithms_general_data_storage_h
18
19#include <deal.II/base/config.h>
20
23
24#include <boost/any.hpp>
25#include <boost/core/demangle.hpp>
26
27#include <algorithm>
28#include <map>
29#include <string>
30#include <typeinfo>
31
33
34
55{
56public:
60 GeneralDataStorage() = default;
61
66
71
75 std::size_t
76 size() const;
77
81 void
82 merge(const GeneralDataStorage &other_data);
83
92 template <class Stream>
93 void
94 print_info(Stream &stream);
95
175 void
176 reset();
177
182
191 template <typename Type>
192 void
193 add_unique_copy(const std::string &name, const Type &entry);
194
204 template <typename Type>
205 void
206 add_or_overwrite_copy(const std::string &name, const Type &entry);
207
217 template <typename Type>
218 void
219 add_unique_reference(const std::string &name, Type &entry);
220
231 template <typename Type>
232 void
233 add_or_overwrite_reference(const std::string &name, Type &entry);
234
245 template <typename Type, typename Arg, typename... Args>
246 Type &
247 get_or_add_object_with_name(const std::string &name,
248 Arg & argument,
249 Args &...arguments);
250
258 template <typename Type, typename Arg>
259 Type &
260 get_or_add_object_with_name(const std::string &name, Arg &argument);
261
272 template <typename Type, typename Arg, typename... Args>
273 Type &
274 get_or_add_object_with_name(const std::string &name,
275 Arg && argument,
276 Args &&...arguments);
277
285 template <typename Type, typename Arg>
286 Type &
287 get_or_add_object_with_name(const std::string &name, Arg &&argument);
288
292 template <typename Type>
293 Type &
294 get_or_add_object_with_name(const std::string &name);
295
303 template <typename Type>
304 Type &
305 get_object_with_name(const std::string &name);
306
314 template <typename Type>
315 const Type &
316 get_object_with_name(const std::string &name) const;
317
321 bool
322 stores_object_with_name(const std::string &name) const;
323
327 void
328 remove_object_with_name(const std::string &name);
329
331
336 std::string,
337 << "No entry with the name " << arg1 << " exists.");
338
343 std::string,
344 << "An entry with the name " << arg1 << " already exists.");
345
350 std::string,
351 const char *,
352 const char *,
353 << "The stored type for entry with name \"" << arg1 << "\" is "
354 << arg2 << " but you requested type " << arg3 << '.');
355
356private:
360 std::map<std::string, boost::any> any_data;
361};
362
363
364/*----------------- Inline and template methods -----------------*/
365
366
367#ifndef DOXYGEN
368
369
370template <class Stream>
371void
373{
374 for (const auto &it : any_data)
375 {
376 os << it.first << '\t' << '\t'
377 << boost::core::demangle(it.second.type().name()) << std::endl;
378 }
379}
380
381
382template <typename Type>
383void
384GeneralDataStorage::add_unique_copy(const std::string &name, const Type &entry)
385{
387 add_or_overwrite_copy(name, entry);
388}
389
390
391template <typename Type>
392void
393GeneralDataStorage::add_or_overwrite_copy(const std::string &name,
394 const Type & entry)
395{
396 any_data[name] = entry;
397}
398
399
400template <typename Type>
401void
402GeneralDataStorage::add_unique_reference(const std::string &name, Type &entry)
403{
405 add_or_overwrite_reference(name, entry);
406}
407
408
409template <typename Type>
410void
412 Type & entry)
413{
414 Type *ptr = &entry;
415 any_data[name] = ptr;
416}
417
418
419template <typename Type>
420Type &
421GeneralDataStorage::get_object_with_name(const std::string &name)
422{
424
425 Type *p = nullptr;
426
427 if (any_data[name].type() == typeid(Type *))
428 {
429 p = boost::any_cast<Type *>(any_data[name]);
430 }
431 else if (any_data[name].type() == typeid(Type))
432 {
433 p = boost::any_cast<Type>(&any_data[name]);
434 }
435 else
436 {
437 AssertThrow(false,
438 ExcTypeMismatch(name,
439 any_data[name].type().name(),
440 typeid(Type).name()));
441 }
442
443 return *p;
444}
445
446
447template <typename Type>
448const Type &
449GeneralDataStorage::get_object_with_name(const std::string &name) const
450{
452
453 const auto it = any_data.find(name);
454
455 if (it->second.type() == typeid(Type *))
456 {
457 const Type *p = boost::any_cast<Type *>(it->second);
458 return *p;
459 }
460 else if (it->second.type() == typeid(Type))
461 {
462 const Type *p = boost::any_cast<Type>(&it->second);
463 return *p;
464 }
465 else
466 {
467 AssertThrow(false,
468 ExcTypeMismatch(name,
469 it->second.type().name(),
470 typeid(Type).name()));
471 const Type *p = nullptr;
472 return *p;
473 }
474}
475
476
477
478template <typename Type, typename Arg>
479Type &
481 Arg & argument)
482{
483 if (!stores_object_with_name(name))
484 add_unique_copy(name, Type(argument));
485
486 return get_object_with_name<Type>(name);
487}
488
489
490
491template <typename Type, typename Arg, typename... Args>
492Type &
494 Arg & argument,
495 Args &...arguments)
496{
497 if (!stores_object_with_name(name))
498 add_unique_copy(name, Type(argument, arguments...));
499
500 return get_object_with_name<Type>(name);
501}
502
503
504
505template <typename Type, typename Arg>
506Type &
508 Arg && argument)
509{
510 if (!stores_object_with_name(name))
511 add_unique_copy(name, Type(std::forward<Arg>(argument)));
512
513 return get_object_with_name<Type>(name);
514}
515
516
517
518template <typename Type, typename Arg, typename... Args>
519Type &
521 Arg && argument,
522 Args &&...arguments)
523{
524 if (!stores_object_with_name(name))
525 add_unique_copy(name,
526 Type(std::forward<Arg>(argument),
527 std::forward<Args>(arguments)...));
528
529 return get_object_with_name<Type>(name);
530}
531
532
533template <typename Type>
534Type &
536{
537 if (!stores_object_with_name(name))
538 add_unique_copy(name, Type());
539
540 return get_object_with_name<Type>(name);
541}
542
543
544#endif // DOXYGEN
545
546
548
549#endif // dealii_algorithms_general_data_storage_h
GeneralDataStorage(GeneralDataStorage &&)=default
std::size_t size() const
void merge(const GeneralDataStorage &other_data)
Type & get_or_add_object_with_name(const std::string &name, Arg &argument)
void add_unique_reference(const std::string &name, Type &entry)
void add_unique_copy(const std::string &name, const Type &entry)
GeneralDataStorage()=default
void remove_object_with_name(const std::string &name)
void add_or_overwrite_copy(const std::string &name, const Type &entry)
const Type & get_object_with_name(const std::string &name) const
bool stores_object_with_name(const std::string &name) const
void print_info(Stream &stream)
std::map< std::string, boost::any > any_data
Type & get_or_add_object_with_name(const std::string &name, Arg &&argument, Args &&...arguments)
void add_or_overwrite_reference(const std::string &name, Type &entry)
Type & get_or_add_object_with_name(const std::string &name)
GeneralDataStorage(const GeneralDataStorage &)=default
Type & get_or_add_object_with_name(const std::string &name, Arg &argument, Args &...arguments)
Type & get_or_add_object_with_name(const std::string &name, Arg &&argument)
Type & get_object_with_name(const std::string &name)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
static ::ExceptionBase & ExcNameNotFound(std::string arg1)
static ::ExceptionBase & ExcNameHasBeenFound(std::string arg1)
static ::ExceptionBase & ExcTypeMismatch(std::string arg1, const char *arg2, const char *arg3)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition: exceptions.h:555
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:509
#define AssertThrow(cond, exc)
Definition: exceptions.h:1583