deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+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\}}\)
Loading...
Searching...
No Matches
general_data_storage.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2019 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_algorithms_general_data_storage_h
14#define dealii_algorithms_general_data_storage_h
15
16#include <deal.II/base/config.h>
17
20
21#include <boost/core/demangle.hpp>
22
23#include <algorithm>
24#include <any>
25#include <map>
26#include <ostream>
27#include <string>
28#include <typeinfo>
29
31
32
41{
42public:
46 GeneralDataStorage() = default;
47
52
57
61 std::size_t
62 size() const;
63
67 void
68 merge(const GeneralDataStorage &other_data);
69
78 template <class Stream>
79 void
80 print_info(Stream &stream);
81
161 void
162 reset();
163
177 template <typename Type>
178 void
179 add_unique_copy(const std::string &name, const Type &entry);
180
190 template <typename Type>
191 void
192 add_or_overwrite_copy(const std::string &name, const Type &entry);
193
203 template <typename Type>
204 void
205 add_unique_reference(const std::string &name, Type &entry);
206
217 template <typename Type>
218 void
219 add_or_overwrite_reference(const std::string &name, Type &entry);
220
231 template <typename Type, typename Arg, typename... Args>
232 Type &
233 get_or_add_object_with_name(const std::string &name,
234 Arg &argument,
235 Args &...arguments);
236
244 template <typename Type, typename Arg>
245 Type &
246 get_or_add_object_with_name(const std::string &name, Arg &argument);
247
258 template <typename Type, typename Arg, typename... Args>
259 Type &
260 get_or_add_object_with_name(const std::string &name,
261 Arg &&argument,
262 Args &&...arguments);
263
271 template <typename Type, typename Arg>
272 Type &
273 get_or_add_object_with_name(const std::string &name, Arg &&argument);
274
278 template <typename Type>
279 Type &
280 get_or_add_object_with_name(const std::string &name);
281
289 template <typename Type>
290 Type &
291 get_object_with_name(const std::string &name);
292
300 template <typename Type>
301 const Type &
302 get_object_with_name(const std::string &name) const;
303
307 bool
308 stores_object_with_name(const std::string &name) const;
309
313 void
314 remove_object_with_name(const std::string &name);
315
322 std::string,
323 << "No entry with the name " << arg1 << " exists.");
324
329 std::string,
330 << "An entry with the name " << arg1 << " already exists.");
331
336 std::string,
337 const char *,
338 const char *,
339 << "The stored type for entry with name \"" << arg1 << "\" is "
340 << arg2 << " but you requested type " << arg3 << '.');
341
342private:
346 std::map<std::string, std::any> any_data;
347};
348
349
350/*----------------- Inline and template methods -----------------*/
351
352
353#ifndef DOXYGEN
354
355
356template <class Stream>
357void
359{
360 for (const auto &it : any_data)
361 {
362 os << it.first << '\t' << '\t'
363 << boost::core::demangle(it.second.type().name()) << std::endl;
364 }
365}
366
367
368template <typename Type>
369void
370GeneralDataStorage::add_unique_copy(const std::string &name, const Type &entry)
371{
373 add_or_overwrite_copy(name, entry);
374}
375
376
377template <typename Type>
378void
379GeneralDataStorage::add_or_overwrite_copy(const std::string &name,
380 const Type &entry)
381{
382 any_data[name] = entry;
383}
384
385
386template <typename Type>
387void
388GeneralDataStorage::add_unique_reference(const std::string &name, Type &entry)
389{
391 add_or_overwrite_reference(name, entry);
392}
393
394
395template <typename Type>
396void
398 Type &entry)
399{
400 Type *ptr = &entry;
401 any_data[name] = ptr;
402}
403
404
405template <typename Type>
406Type &
407GeneralDataStorage::get_object_with_name(const std::string &name)
408{
410
411 Type *p = nullptr;
412
413 if (any_data[name].type() == typeid(Type *))
414 {
415 p = std::any_cast<Type *>(any_data[name]);
416 }
417 else if (any_data[name].type() == typeid(Type))
418 {
419 p = std::any_cast<Type>(&any_data[name]);
420 }
421 else
422 {
423 AssertThrow(false,
424 ExcTypeMismatch(name,
425 any_data[name].type().name(),
426 typeid(Type).name()));
427 }
428
429 return *p;
430}
431
432
433template <typename Type>
434const Type &
435GeneralDataStorage::get_object_with_name(const std::string &name) const
436{
438
439 const auto it = any_data.find(name);
440
441 if (it->second.type() == typeid(Type *))
442 {
443 const Type *p = std::any_cast<Type *>(it->second);
444 return *p;
445 }
446 else if (it->second.type() == typeid(Type))
447 {
448 const Type *p = std::any_cast<Type>(&it->second);
449 return *p;
450 }
451 else
452 {
453 AssertThrow(false,
454 ExcTypeMismatch(name,
455 it->second.type().name(),
456 typeid(Type).name()));
457 const Type *p = nullptr;
458 return *p;
459 }
460}
461
462
463
464template <typename Type, typename Arg>
465Type &
467 Arg &argument)
468{
469 if (!stores_object_with_name(name))
470 add_unique_copy(name, Type(argument));
471
472 return get_object_with_name<Type>(name);
473}
474
475
476
477template <typename Type, typename Arg, typename... Args>
478Type &
480 Arg &argument,
481 Args &...arguments)
482{
483 if (!stores_object_with_name(name))
484 add_unique_copy(name, Type(argument, arguments...));
485
486 return get_object_with_name<Type>(name);
487}
488
489
490
491template <typename Type, typename Arg>
492Type &
494 Arg &&argument)
495{
496 if (!stores_object_with_name(name))
497 add_unique_copy(name, Type(std::forward<Arg>(argument)));
498
499 return get_object_with_name<Type>(name);
500}
501
502
503
504template <typename Type, typename Arg, typename... Args>
505Type &
507 Arg &&argument,
508 Args &&...arguments)
509{
510 if (!stores_object_with_name(name))
511 add_unique_copy(name,
512 Type(std::forward<Arg>(argument),
513 std::forward<Args>(arguments)...));
514
515 return get_object_with_name<Type>(name);
516}
517
518
519template <typename Type>
520Type &
522{
523 if (!stores_object_with_name(name))
524 add_unique_copy(name, Type());
525
526 return get_object_with_name<Type>(name);
527}
528
529
530#endif // DOXYGEN
531
532
534
535#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:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
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)