22 #ifndef _aspect_plugins_h 23 #define _aspect_plugins_h 27 #include <deal.II/base/utilities.h> 28 #include <deal.II/base/parameter_handler.h> 30 #include <deal.II/base/exceptions.h> 32 #include <boost/core/demangle.hpp> 40 #include <type_traits> 69 template <
typename TestType,
typename PluginType,
70 typename =
typename std::enable_if_t<std::is_base_of<PluginType,TestType>::value>>
75 return (dynamic_cast<const TestType *> (&
object) !=
nullptr);
95 template <
typename TestType,
typename PluginType,
96 typename =
typename std::enable_if_t<std::is_base_of<PluginType,TestType>::value>>
101 AssertThrow(plugin_type_matches<TestType>(
object),
102 ExcMessage(
"You have requested to convert a plugin of type <" 103 + boost::core::demangle(
typeid(PluginType).name())
105 + boost::core::demangle(
typeid(TestType).name()) +
106 ">, but this cast cannot be performed."));
111 return *
dynamic_cast<TestType *
> (&object);
189 parse_parameters (ParameterHandler &prm);
212 template <
typename InterfaceType>
237 template <
typename PluginType,
238 typename =
typename std::enable_if_t<std::is_base_of<InterfaceType,PluginType>::value>>
240 has_matching_active_plugin ()
const;
256 template <
typename PluginType,
257 typename =
typename std::enable_if_t<std::is_base_of<InterfaceType,PluginType>::value>>
259 get_matching_active_plugin ()
const;
266 const std::list<std::unique_ptr<InterfaceType>> &
267 get_active_plugins ()
const;
275 const std::vector<std::string> &
276 get_active_plugin_names ()
const;
294 template <
typename InterfaceType>
299 #ifdef DEAL_II_HAVE_CXX17 300 if (std::uncaught_exceptions() == 0)
302 if (std::uncaught_exception() ==
false)
305 Assert (plugin_names.size() == plugin_objects.size(), ExcInternalError());
310 template <
typename InterfaceType>
314 for (
const auto &p : plugin_objects)
327 catch (std::exception &exc)
329 std::cerr << std::endl << std::endl
330 <<
"----------------------------------------------------" 332 std::cerr <<
"Exception on MPI process <" 333 << ::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD)
334 <<
"> while running plugin <" 336 <<
">: " << std::endl
337 << exc.what() << std::endl
338 <<
"Aborting!" << std::endl
339 <<
"----------------------------------------------------" 343 MPI_Abort (MPI_COMM_WORLD, 1);
347 std::cerr << std::endl << std::endl
348 <<
"----------------------------------------------------" 350 std::cerr <<
"Exception on MPI process <" 351 << ::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD)
352 <<
"> while running plugin <" 354 <<
">: " << std::endl;
355 std::cerr <<
"Unknown exception!" << std::endl
356 <<
"Aborting!" << std::endl
357 <<
"----------------------------------------------------" 361 MPI_Abort (MPI_COMM_WORLD, 1);
367 template <
typename InterfaceType>
368 template <
typename PluginType,
typename>
373 for (
const auto &p : plugin_objects)
374 if (Plugins::plugin_type_matches<PluginType>(*p))
380 template <
typename InterfaceType>
381 template <
typename PluginType,
typename>
386 AssertThrow(has_matching_active_plugin<PluginType> (),
387 ExcMessage(
"You asked the object managing a collection of plugins for a " 388 "plugin object of type <" + boost::core::demangle(
typeid(PluginType).name()) +
"> " 389 "that could not be found in the current model. You need to " 390 "activate this plugin in the input file for it to be " 393 for (
const auto &p : plugin_objects)
394 if (Plugins::plugin_type_matches<PluginType>(*p))
395 return Plugins::get_plugin_as_type<PluginType>(*p);
398 return Plugins::get_plugin_as_type<PluginType>(**(plugin_objects.begin()));
403 template <
typename InterfaceType>
404 const std::list<std::unique_ptr<InterfaceType>> &
407 return plugin_objects;
412 template <
typename InterfaceType>
413 const std::vector<std::string> &
443 template <
typename InterfaceClass,
454 void ( *)(ParameterHandler &),
455 std::unique_ptr<InterfaceClass> ( *)()),
457 const char *description)
459 register_function (name,
472 return std::make_unique<ModelClass>();
481 template <
typename InterfaceClass>
496 = std::tuple<std::string,
498 void ( *) (ParameterHandler &),
499 std::unique_ptr<InterfaceClass>( *) ()>;
526 void register_plugin (
const std::string &name,
527 const std::string &description,
528 void (*declare_parameters_function) (ParameterHandler &),
529 std::unique_ptr<InterfaceClass> (*factory_function) ());
539 std::string get_pattern_of_names ();
549 std::string get_description_string ();
567 std::unique_ptr<InterfaceClass>
568 create_plugin (
const std::string &name,
569 const std::string &documentation);
582 std::unique_ptr<InterfaceClass>
583 create_plugin (
const std::string &name,
584 const std::string &documentation,
585 ParameterHandler &prm);
611 std::ostream &output_stream,
612 const std::string &attachment_point =
"Simulator");
619 <<
"Can't create a plugin of name <" << arg1
620 <<
"> because such a plugin hasn't been declared.");
626 template <
typename InterfaceClass>
632 if (plugins !=
nullptr)
639 template <
typename InterfaceClass>
643 const std::string &description,
644 void (*declare_parameters_function) (ParameterHandler &),
645 std::unique_ptr<InterfaceClass> (*factory_function) ())
649 if (plugins ==
nullptr)
650 plugins =
new std::list<PluginInfo>();
655 for (
const auto &p : *plugins)
657 Assert (std::get<0>(p) != name,
658 ExcMessage (
"A plugin with name <" + name +
"> has " 659 "already been registered!"));
665 plugins->emplace_back (name,
667 declare_parameters_function,
673 template <
typename InterfaceClass>
678 Assert (plugins !=
nullptr,
679 ExcMessage (
"No plugins registered!?"));
683 std::set<std::string> names;
684 for (
typename std::list<PluginInfo>::const_iterator
685 p = plugins->begin();
686 p != plugins->end(); ++p)
687 names.insert (std::get<0>(*p));
690 std::string pattern_of_names;
691 for (
const auto &name : names)
693 if (pattern_of_names.size() > 0)
694 pattern_of_names +=
"|";
695 pattern_of_names += name;
698 return pattern_of_names;
703 template <
typename InterfaceClass>
708 std::string description;
712 std::map<std::string,std::string> names_and_descriptions;
713 for (
typename std::list<PluginInfo>::const_iterator
714 p = plugins->begin();
715 p != plugins->end(); ++p)
716 names_and_descriptions[std::get<0>(*p)] = std::get<1>(*p);
719 std::map<std::string,std::string>::const_iterator
720 p = names_and_descriptions.begin();
727 description += p->first;
728 description +=
"': ";
729 description += p->second;
736 if (p != names_and_descriptions.end())
737 description +=
"\n\n";
748 template <
typename InterfaceClass>
753 Assert (plugins !=
nullptr,
754 ExcMessage (
"No postprocessors registered!?"));
756 for (
typename std::list<PluginInfo>::const_iterator
757 p = plugins->begin();
758 p != plugins->end(); ++p)
759 (std::get<2>(*p))(prm);
764 template <
typename InterfaceClass>
765 std::unique_ptr<InterfaceClass>
768 const std::string &documentation)
771 Assert (plugins !=
nullptr,
772 ExcMessage (
"No postprocessors registered!?"));
773 AssertThrow (name !=
"unspecified",
774 ExcMessage(std::string(
"A plugin must have a name!\n\n" 775 "This function was asked to create a plugin but no name for the " 776 "plugin was provided. This may be due to the fact that you did not " 777 "explicitly specify a name for this plugin in your input file and " 778 "ASPECT does not provide a default for this kind of plugin, for " 779 "example because no generally useful plugin exists. An example " 780 "is that there is no default geometry: You need to explicitly " 781 "provide one in the input file, and it seems like you have not " 783 "To find out which kind of plugin this function tries to create, " 784 "take a look at the backtrace of this error message.\n\n" 785 "The place that called this function also provided as " 786 "additional information this:\n\n" 788 + documentation +
">"));
790 for (
typename std::list<PluginInfo>::const_iterator p = plugins->begin();
791 p != plugins->end(); ++p)
792 if (std::get<0>(*p) == name)
794 std::unique_ptr<InterfaceClass> i = std::get<3>(*p)();
798 AssertThrow (
false, ExcUnknownPlugin(name));
804 template <
typename InterfaceClass>
805 std::unique_ptr<InterfaceClass>
808 const std::string &documentation,
809 ParameterHandler &prm)
811 std::unique_ptr<InterfaceClass> i = create_plugin(name, documentation);
812 i->parse_parameters (prm);
818 template <
typename InterfaceClass>
822 std::ostream &output_stream,
823 const std::string &attachment_point)
831 output_stream << std::string(
typeid(InterfaceClass).name())
833 << plugin_system_name
834 <<
"\", height=.8,width=.8,shape=\"rect\",fillcolor=\"lightgreen\"]" 846 std::map<std::string, typename std::list<PluginInfo>::const_iterator>
848 for (
typename std::list<PluginInfo>::const_iterator p = plugins->begin();
849 p != plugins->end(); ++p)
850 plugin_map[std::get<0>(*p)] = p;
853 for (
typename std::map<std::string,
typename std::list<PluginInfo>::const_iterator>::const_iterator
854 p = plugin_map.begin();
855 p != plugin_map.end(); ++p)
861 const std::vector<std::string> plugin_label_parts
862 = ::Utilities::break_text_into_lines(p->first, 15);
863 Assert (plugin_label_parts.size()>0, ExcInternalError());
864 std::string plugin_name = plugin_label_parts[0];
865 for (
unsigned int i=1; i<plugin_label_parts.size(); ++i)
866 plugin_name +=
"\\n" + plugin_label_parts[i];
871 std::unique_ptr<InterfaceClass> instance (create_plugin (p->first,
""));
872 const std::string node_name =
typeid(*instance).name();
875 output_stream << node_name
878 <<
"\", height=.8,width=.8,shape=\"circle\",fillcolor=\"lightblue\"];" 883 output_stream << node_name
885 << std::string(
typeid(InterfaceClass).name())
886 <<
" [len=3, weight=50]" 896 output_stream <<
"SimulatorAccess" 899 <<
" [style=\"dotted\", arrowhead=\"empty\", constraint=false, color=\"gray\", len=20, weight=0.1];" 906 output_stream << std::string(
typeid(InterfaceClass).name())
909 <<
" [len=15, weight=50]" 915 output_stream << std::endl;
const std::vector< std::string > & get_active_plugin_names() const
const PluginType & get_matching_active_plugin() const
std::list< std::unique_ptr< InterfaceType > > plugin_objects
void write_plugin_graph(std::ostream &output_stream)
TestType & get_plugin_as_type(PluginType &object)
RegisterHelper(void(*register_function)(const std::string &, const std::string &, void(*)(ParameterHandler &), std::unique_ptr< InterfaceClass >(*)()), const char *name, const char *description)
static std::list< PluginInfo > * plugins
std::tuple< std::string, std::string, void(*)(ParameterHandler &), std::unique_ptr< InterfaceClass >(*)()> PluginInfo
void declare_parameters(ParameterHandler &prm)
static std::unique_ptr< InterfaceClass > factory()
bool has_matching_active_plugin() const
bool plugin_type_matches(const PluginType &object)
const std::list< std::unique_ptr< InterfaceType > > & get_active_plugins() const
std::vector< std::string > plugin_names
DeclException1(ProbabilityFunctionNegative, Point< dim >,<< "Your probability density function in the particle generator " "returned a negative probability density for the following position: "<< arg1<< ". Please check your function expression.")