Loading [MathJax]/extensions/TeX/newcommand.js
 deal.II version GIT relicensing-3075-gc235bd4825 2025-04-15 08:10:00+00:00
\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\}}
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
general_data_storage.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2019 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_algorithms_general_data_storage_h
16#define dealii_algorithms_general_data_storage_h
17
18#include <deal.II/base/config.h>
19
22
23#include <boost/core/demangle.hpp>
24
25#include <algorithm>
26#include <any>
27#include <map>
28#include <ostream>
29#include <string>
30#include <typeinfo>
31
33
34
43{
44public:
48 GeneralDataStorage() = default;
49
54
59
63 std::size_t
64 size() const;
65
69 void
70 merge(const GeneralDataStorage &other_data);
71
80 template <class Stream>
81 void
82 print_info(Stream &stream);
83
163 void
164 reset();
165
179 template <typename Type>
180 void
181 add_unique_copy(const std::string &name, const Type &entry);
182
192 template <typename Type>
193 void
194 add_or_overwrite_copy(const std::string &name, const Type &entry);
195
205 template <typename Type>
206 void
207 add_unique_reference(const std::string &name, Type &entry);
208
219 template <typename Type>
220 void
221 add_or_overwrite_reference(const std::string &name, Type &entry);
222
233 template <typename Type, typename Arg, typename... Args>
234 Type &
235 get_or_add_object_with_name(const std::string &name,
236 Arg &argument,
237 Args &...arguments);
238
246 template <typename Type, typename Arg>
247 Type &
248 get_or_add_object_with_name(const std::string &name, Arg &argument);
249
260 template <typename Type, typename Arg, typename... Args>
261 Type &
262 get_or_add_object_with_name(const std::string &name,
263 Arg &&argument,
264 Args &&...arguments);
265
273 template <typename Type, typename Arg>
274 Type &
275 get_or_add_object_with_name(const std::string &name, Arg &&argument);
276
280 template <typename Type>
281 Type &
282 get_or_add_object_with_name(const std::string &name);
283
291 template <typename Type>
292 Type &
293 get_object_with_name(const std::string &name);
294
302 template <typename Type>
303 const Type &
304 get_object_with_name(const std::string &name) const;
305
309 bool
310 stores_object_with_name(const std::string &name) const;
311
315 void
316 remove_object_with_name(const std::string &name);
317
324 std::string,
325 << "No entry with the name " << arg1 << " exists.");
326
331 std::string,
332 << "An entry with the name " << arg1 << " already exists.");
333
338 std::string,
339 const char *,
340 const char *,
341 << "The stored type for entry with name \"" << arg1 << "\" is "
342 << arg2 << " but you requested type " << arg3 << '.');
343
344private:
348 std::map<std::string, std::any> any_data;
349};
350
351
352/*----------------- Inline and template methods -----------------*/
353
354
355#ifndef DOXYGEN
356
357
358template <class Stream>
359void
361{
362 for (const auto &it : any_data)
363 {
364 os << it.first << '\t' << '\t'
365 << boost::core::demangle(it.second.type().name()) << std::endl;
366 }
367}
368
369
370template <typename Type>
371void
372GeneralDataStorage::add_unique_copy(const std::string &name, const Type &entry)
373{
375 add_or_overwrite_copy(name, entry);
376}
377
378
379template <typename Type>
380void
381GeneralDataStorage::add_or_overwrite_copy(const std::string &name,
382 const Type &entry)
383{
384 any_data[name] = entry;
385}
386
387
388template <typename Type>
389void
390GeneralDataStorage::add_unique_reference(const std::string &name, Type &entry)
391{
393 add_or_overwrite_reference(name, entry);
394}
395
396
397template <typename Type>
398void
400 Type &entry)
401{
402 Type *ptr = &entry;
403 any_data[name] = ptr;
404}
405
406
407template <typename Type>
408Type &
409GeneralDataStorage::get_object_with_name(const std::string &name)
410{
412
413 Type *p = nullptr;
414
415 if (any_data[name].type() == typeid(Type *))
416 {
417 p = std::any_cast<Type *>(any_data[name]);
418 }
419 else if (any_data[name].type() == typeid(Type))
420 {
421 p = std::any_cast<Type>(&any_data[name]);
422 }
423 else
424 {
425 AssertThrow(false,
426 ExcTypeMismatch(name,
427 any_data[name].type().name(),
428 typeid(Type).name()));
429 }
430
431 return *p;
432}
433
434
435template <typename Type>
436const Type &
437GeneralDataStorage::get_object_with_name(const std::string &name) const
438{
440
441 const auto it = any_data.find(name);
442
443 if (it->second.type() == typeid(Type *))
444 {
445 const Type *p = std::any_cast<Type *>(it->second);
446 return *p;
447 }
448 else if (it->second.type() == typeid(Type))
449 {
450 const Type *p = std::any_cast<Type>(&it->second);
451 return *p;
452 }
453 else
454 {
455 AssertThrow(false,
456 ExcTypeMismatch(name,
457 it->second.type().name(),
458 typeid(Type).name()));
459 const Type *p = nullptr;
460 return *p;
461 }
462}
463
464
465
466template <typename Type, typename Arg>
467Type &
469 Arg &argument)
470{
471 if (!stores_object_with_name(name))
472 add_unique_copy(name, Type(argument));
473
474 return get_object_with_name<Type>(name);
475}
476
477
478
479template <typename Type, typename Arg, typename... Args>
480Type &
482 Arg &argument,
483 Args &...arguments)
484{
485 if (!stores_object_with_name(name))
486 add_unique_copy(name, Type(argument, arguments...));
487
488 return get_object_with_name<Type>(name);
489}
490
491
492
493template <typename Type, typename Arg>
494Type &
496 Arg &&argument)
497{
498 if (!stores_object_with_name(name))
499 add_unique_copy(name, Type(std::forward<Arg>(argument)));
500
501 return get_object_with_name<Type>(name);
502}
503
504
505
506template <typename Type, typename Arg, typename... Args>
507Type &
509 Arg &&argument,
510 Args &&...arguments)
511{
512 if (!stores_object_with_name(name))
513 add_unique_copy(name,
514 Type(std::forward<Arg>(argument),
515 std::forward<Args>(arguments)...));
516
517 return get_object_with_name<Type>(name);
518}
519
520
521template <typename Type>
522Type &
524{
525 if (!stores_object_with_name(name))
526 add_unique_copy(name, Type());
527
528 return get_object_with_name<Type>(name);
529}
530
531
532#endif // DOXYGEN
533
534
536
537#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)
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
std::map< std::string, std::any > any_data
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:35
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:36
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)
#define DeclException1(Exception1, type1, outsequence)
#define AssertThrow(cond, exc)