Reference documentation for deal.II version GIT relicensing-660-g9ae06c0eb4 2024-05-17 13:10:02+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: LGPL-2.1-or-later
4// Copyright (C) 2019 - 2023 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 <string>
29#include <typeinfo>
30
32
33
54{
55public:
59 GeneralDataStorage() = default;
60
65
70
74 std::size_t
75 size() const;
76
80 void
82
91 template <class Stream>
92 void
94
174 void
175 reset();
176
190 template <typename Type>
191 void
192 add_unique_copy(const std::string &name, const Type &entry);
193
203 template <typename Type>
204 void
205 add_or_overwrite_copy(const std::string &name, const Type &entry);
206
216 template <typename Type>
217 void
218 add_unique_reference(const std::string &name, Type &entry);
219
230 template <typename Type>
231 void
232 add_or_overwrite_reference(const std::string &name, Type &entry);
233
244 template <typename Type, typename Arg, typename... Args>
245 Type &
246 get_or_add_object_with_name(const std::string &name,
247 Arg &argument,
248 Args &...arguments);
249
257 template <typename Type, typename Arg>
258 Type &
259 get_or_add_object_with_name(const std::string &name, Arg &argument);
260
271 template <typename Type, typename Arg, typename... Args>
272 Type &
273 get_or_add_object_with_name(const std::string &name,
274 Arg &&argument,
275 Args &&...arguments);
276
284 template <typename Type, typename Arg>
285 Type &
286 get_or_add_object_with_name(const std::string &name, Arg &&argument);
287
291 template <typename Type>
292 Type &
293 get_or_add_object_with_name(const std::string &name);
294
302 template <typename Type>
303 Type &
304 get_object_with_name(const std::string &name);
305
313 template <typename Type>
314 const Type &
315 get_object_with_name(const std::string &name) const;
316
320 bool
321 stores_object_with_name(const std::string &name) const;
322
326 void
327 remove_object_with_name(const std::string &name);
328
335 std::string,
336 << "No entry with the name " << arg1 << " exists.");
337
342 std::string,
343 << "An entry with the name " << arg1 << " already exists.");
344
349 std::string,
350 const char *,
351 const char *,
352 << "The stored type for entry with name \"" << arg1 << "\" is "
353 << arg2 << " but you requested type " << arg3 << '.');
354
355private:
359 std::map<std::string, std::any> any_data;
360};
361
362
363/*----------------- Inline and template methods -----------------*/
364
365
366#ifndef DOXYGEN
367
368
369template <class Stream>
370void
372{
373 for (const auto &it : any_data)
374 {
375 os << it.first << '\t' << '\t'
376 << boost::core::demangle(it.second.type().name()) << std::endl;
377 }
378}
379
380
381template <typename Type>
382void
383GeneralDataStorage::add_unique_copy(const std::string &name, const Type &entry)
384{
386 add_or_overwrite_copy(name, entry);
387}
388
389
390template <typename Type>
391void
392GeneralDataStorage::add_or_overwrite_copy(const std::string &name,
393 const Type &entry)
394{
395 any_data[name] = entry;
396}
397
398
399template <typename Type>
400void
401GeneralDataStorage::add_unique_reference(const std::string &name, Type &entry)
402{
404 add_or_overwrite_reference(name, entry);
405}
406
407
408template <typename Type>
409void
411 Type &entry)
412{
413 Type *ptr = &entry;
414 any_data[name] = ptr;
415}
416
417
418template <typename Type>
419Type &
420GeneralDataStorage::get_object_with_name(const std::string &name)
421{
423
424 Type *p = nullptr;
425
426 if (any_data[name].type() == typeid(Type *))
427 {
428 p = std::any_cast<Type *>(any_data[name]);
429 }
430 else if (any_data[name].type() == typeid(Type))
431 {
432 p = std::any_cast<Type>(&any_data[name]);
433 }
434 else
435 {
436 AssertThrow(false,
437 ExcTypeMismatch(name,
438 any_data[name].type().name(),
439 typeid(Type).name()));
440 }
441
442 return *p;
443}
444
445
446template <typename Type>
447const Type &
448GeneralDataStorage::get_object_with_name(const std::string &name) const
449{
451
452 const auto it = any_data.find(name);
453
454 if (it->second.type() == typeid(Type *))
455 {
456 const Type *p = std::any_cast<Type *>(it->second);
457 return *p;
458 }
459 else if (it->second.type() == typeid(Type))
460 {
461 const Type *p = std::any_cast<Type>(&it->second);
462 return *p;
463 }
464 else
465 {
466 AssertThrow(false,
467 ExcTypeMismatch(name,
468 it->second.type().name(),
469 typeid(Type).name()));
470 const Type *p = nullptr;
471 return *p;
472 }
473}
474
475
476
477template <typename Type, typename Arg>
478Type &
480 Arg &argument)
481{
482 if (!stores_object_with_name(name))
484
485 return get_object_with_name<Type>(name);
486}
487
488
489
490template <typename Type, typename Arg, typename... Args>
491Type &
493 Arg &argument,
494 Args &...arguments)
495{
496 if (!stores_object_with_name(name))
497 add_unique_copy(name, Type(argument, arguments...));
498
499 return get_object_with_name<Type>(name);
500}
501
502
503
504template <typename Type, typename Arg>
505Type &
507 Arg &&argument)
508{
509 if (!stores_object_with_name(name))
510 add_unique_copy(name, Type(std::forward<Arg>(argument)));
511
512 return get_object_with_name<Type>(name);
513}
514
515
516
517template <typename Type, typename Arg, typename... Args>
518Type &
520 Arg &&argument,
521 Args &&...arguments)
522{
523 if (!stores_object_with_name(name))
524 add_unique_copy(name,
525 Type(std::forward<Arg>(argument),
526 std::forward<Args>(arguments)...));
527
528 return get_object_with_name<Type>(name);
529}
530
531
532template <typename Type>
533Type &
535{
536 if (!stores_object_with_name(name))
537 add_unique_copy(name, Type());
538
539 return get_object_with_name<Type>(name);
540}
541
542
543#endif // DOXYGEN
544
545
547
548#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:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
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:562
#define DeclException1(Exception1, type1, outsequence)
Definition exceptions.h:516
#define AssertThrow(cond, exc)
spacedim const Point< spacedim > & p
Definition grid_tools.h:990