deal.II version GIT relicensing-1927-g3de9220933 2024-10-03 08:40: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\}}\)
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 - 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 <string>
29#include <typeinfo>
30
32
33
42{
43public:
47 GeneralDataStorage() = default;
48
53
58
62 std::size_t
63 size() const;
64
68 void
69 merge(const GeneralDataStorage &other_data);
70
79 template <class Stream>
80 void
81 print_info(Stream &stream);
82
162 void
163 reset();
164
178 template <typename Type>
179 void
180 add_unique_copy(const std::string &name, const Type &entry);
181
191 template <typename Type>
192 void
193 add_or_overwrite_copy(const std::string &name, const Type &entry);
194
204 template <typename Type>
205 void
206 add_unique_reference(const std::string &name, Type &entry);
207
218 template <typename Type>
219 void
220 add_or_overwrite_reference(const std::string &name, Type &entry);
221
232 template <typename Type, typename Arg, typename... Args>
233 Type &
234 get_or_add_object_with_name(const std::string &name,
235 Arg &argument,
236 Args &...arguments);
237
245 template <typename Type, typename Arg>
246 Type &
247 get_or_add_object_with_name(const std::string &name, Arg &argument);
248
259 template <typename Type, typename Arg, typename... Args>
260 Type &
261 get_or_add_object_with_name(const std::string &name,
262 Arg &&argument,
263 Args &&...arguments);
264
272 template <typename Type, typename Arg>
273 Type &
274 get_or_add_object_with_name(const std::string &name, Arg &&argument);
275
279 template <typename Type>
280 Type &
281 get_or_add_object_with_name(const std::string &name);
282
290 template <typename Type>
291 Type &
292 get_object_with_name(const std::string &name);
293
301 template <typename Type>
302 const Type &
303 get_object_with_name(const std::string &name) const;
304
308 bool
309 stores_object_with_name(const std::string &name) const;
310
314 void
315 remove_object_with_name(const std::string &name);
316
323 std::string,
324 << "No entry with the name " << arg1 << " exists.");
325
330 std::string,
331 << "An entry with the name " << arg1 << " already exists.");
332
337 std::string,
338 const char *,
339 const char *,
340 << "The stored type for entry with name \"" << arg1 << "\" is "
341 << arg2 << " but you requested type " << arg3 << '.');
342
343private:
347 std::map<std::string, std::any> any_data;
348};
349
350
351/*----------------- Inline and template methods -----------------*/
352
353
354#ifndef DOXYGEN
355
356
357template <class Stream>
358void
360{
361 for (const auto &it : any_data)
362 {
363 os << it.first << '\t' << '\t'
364 << boost::core::demangle(it.second.type().name()) << std::endl;
365 }
366}
367
368
369template <typename Type>
370void
371GeneralDataStorage::add_unique_copy(const std::string &name, const Type &entry)
372{
374 add_or_overwrite_copy(name, entry);
375}
376
377
378template <typename Type>
379void
380GeneralDataStorage::add_or_overwrite_copy(const std::string &name,
381 const Type &entry)
382{
383 any_data[name] = entry;
384}
385
386
387template <typename Type>
388void
389GeneralDataStorage::add_unique_reference(const std::string &name, Type &entry)
390{
392 add_or_overwrite_reference(name, entry);
393}
394
395
396template <typename Type>
397void
399 Type &entry)
400{
401 Type *ptr = &entry;
402 any_data[name] = ptr;
403}
404
405
406template <typename Type>
407Type &
408GeneralDataStorage::get_object_with_name(const std::string &name)
409{
411
412 Type *p = nullptr;
413
414 if (any_data[name].type() == typeid(Type *))
415 {
416 p = std::any_cast<Type *>(any_data[name]);
417 }
418 else if (any_data[name].type() == typeid(Type))
419 {
420 p = std::any_cast<Type>(&any_data[name]);
421 }
422 else
423 {
424 AssertThrow(false,
425 ExcTypeMismatch(name,
426 any_data[name].type().name(),
427 typeid(Type).name()));
428 }
429
430 return *p;
431}
432
433
434template <typename Type>
435const Type &
436GeneralDataStorage::get_object_with_name(const std::string &name) const
437{
439
440 const auto it = any_data.find(name);
441
442 if (it->second.type() == typeid(Type *))
443 {
444 const Type *p = std::any_cast<Type *>(it->second);
445 return *p;
446 }
447 else if (it->second.type() == typeid(Type))
448 {
449 const Type *p = std::any_cast<Type>(&it->second);
450 return *p;
451 }
452 else
453 {
454 AssertThrow(false,
455 ExcTypeMismatch(name,
456 it->second.type().name(),
457 typeid(Type).name()));
458 const Type *p = nullptr;
459 return *p;
460 }
461}
462
463
464
465template <typename Type, typename Arg>
466Type &
468 Arg &argument)
469{
470 if (!stores_object_with_name(name))
471 add_unique_copy(name, Type(argument));
472
473 return get_object_with_name<Type>(name);
474}
475
476
477
478template <typename Type, typename Arg, typename... Args>
479Type &
481 Arg &argument,
482 Args &...arguments)
483{
484 if (!stores_object_with_name(name))
485 add_unique_copy(name, Type(argument, arguments...));
486
487 return get_object_with_name<Type>(name);
488}
489
490
491
492template <typename Type, typename Arg>
493Type &
495 Arg &&argument)
496{
497 if (!stores_object_with_name(name))
498 add_unique_copy(name, Type(std::forward<Arg>(argument)));
499
500 return get_object_with_name<Type>(name);
501}
502
503
504
505template <typename Type, typename Arg, typename... Args>
506Type &
508 Arg &&argument,
509 Args &&...arguments)
510{
511 if (!stores_object_with_name(name))
512 add_unique_copy(name,
513 Type(std::forward<Arg>(argument),
514 std::forward<Args>(arguments)...));
515
516 return get_object_with_name<Type>(name);
517}
518
519
520template <typename Type>
521Type &
523{
524 if (!stores_object_with_name(name))
525 add_unique_copy(name, Type());
526
527 return get_object_with_name<Type>(name);
528}
529
530
531#endif // DOXYGEN
532
533
535
536#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:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
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:557
#define DeclException1(Exception1, type1, outsequence)
Definition exceptions.h:511
#define AssertThrow(cond, exc)