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>
297 Assert (plugin_names.size() == plugin_objects.size(), ExcInternalError());
301 template <
typename InterfaceType>
305 for (
const auto &p : plugin_objects)
318 catch (std::exception &exc)
320 std::cerr << std::endl << std::endl
321 <<
"----------------------------------------------------" 323 std::cerr <<
"Exception on MPI process <" 324 << ::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD)
325 <<
"> while running plugin <" 327 <<
">: " << std::endl
328 << exc.what() << std::endl
329 <<
"Aborting!" << std::endl
330 <<
"----------------------------------------------------" 334 MPI_Abort (MPI_COMM_WORLD, 1);
338 std::cerr << std::endl << std::endl
339 <<
"----------------------------------------------------" 341 std::cerr <<
"Exception on MPI process <" 342 << ::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD)
343 <<
"> while running plugin <" 345 <<
">: " << std::endl;
346 std::cerr <<
"Unknown exception!" << std::endl
347 <<
"Aborting!" << std::endl
348 <<
"----------------------------------------------------" 352 MPI_Abort (MPI_COMM_WORLD, 1);
358 template <
typename InterfaceType>
359 template <
typename PluginType,
typename>
364 for (
const auto &p : plugin_objects)
365 if (Plugins::plugin_type_matches<PluginType>(*p))
371 template <
typename InterfaceType>
372 template <
typename PluginType,
typename>
377 AssertThrow(has_matching_active_plugin<PluginType> (),
378 ExcMessage(
"You asked the object managing a collection of plugins for a " 379 "plugin object of type <" + boost::core::demangle(
typeid(PluginType).name()) +
"> " 380 "that could not be found in the current model. You need to " 381 "activate this plugin in the input file for it to be " 384 for (
const auto &p : plugin_objects)
385 if (Plugins::plugin_type_matches<PluginType>(*p))
386 return Plugins::get_plugin_as_type<PluginType>(*p);
389 return Plugins::get_plugin_as_type<PluginType>(**(plugin_objects.begin()));
394 template <
typename InterfaceType>
395 const std::list<std::unique_ptr<InterfaceType>> &
398 return plugin_objects;
403 template <
typename InterfaceType>
404 const std::vector<std::string> &
434 template <
typename InterfaceClass,
445 void ( *)(ParameterHandler &),
446 std::unique_ptr<InterfaceClass> ( *)()),
448 const char *description)
450 register_function (name,
463 return std::make_unique<ModelClass>();
472 template <
typename InterfaceClass>
487 = std::tuple<std::string,
489 void ( *) (ParameterHandler &),
490 std::unique_ptr<InterfaceClass>( *) ()>;
517 void register_plugin (
const std::string &name,
518 const std::string &description,
519 void (*declare_parameters_function) (ParameterHandler &),
520 std::unique_ptr<InterfaceClass> (*factory_function) ());
530 std::string get_pattern_of_names ();
540 std::string get_description_string ();
558 std::unique_ptr<InterfaceClass>
559 create_plugin (
const std::string &name,
560 const std::string &documentation);
573 std::unique_ptr<InterfaceClass>
574 create_plugin (
const std::string &name,
575 const std::string &documentation,
576 ParameterHandler &prm);
602 std::ostream &output_stream,
603 const std::string &attachment_point =
"Simulator");
610 <<
"Can't create a plugin of name <" << arg1
611 <<
"> because such a plugin hasn't been declared.");
617 template <
typename InterfaceClass>
623 if (plugins !=
nullptr)
630 template <
typename InterfaceClass>
634 const std::string &description,
635 void (*declare_parameters_function) (ParameterHandler &),
636 std::unique_ptr<InterfaceClass> (*factory_function) ())
640 if (plugins ==
nullptr)
641 plugins =
new std::list<PluginInfo>();
646 for (
const auto &p : *plugins)
648 Assert (std::get<0>(p) != name,
649 ExcMessage (
"A plugin with name <" + name +
"> has " 650 "already been registered!"));
656 plugins->emplace_back (name,
658 declare_parameters_function,
664 template <
typename InterfaceClass>
669 Assert (plugins !=
nullptr,
670 ExcMessage (
"No plugins registered!?"));
674 std::set<std::string> names;
675 for (
typename std::list<PluginInfo>::const_iterator
676 p = plugins->begin();
677 p != plugins->end(); ++p)
678 names.insert (std::get<0>(*p));
681 std::string pattern_of_names;
682 for (
const auto &name : names)
684 if (pattern_of_names.size() > 0)
685 pattern_of_names +=
"|";
686 pattern_of_names += name;
689 return pattern_of_names;
694 template <
typename InterfaceClass>
699 std::string description;
703 std::map<std::string,std::string> names_and_descriptions;
704 for (
typename std::list<PluginInfo>::const_iterator
705 p = plugins->begin();
706 p != plugins->end(); ++p)
707 names_and_descriptions[std::get<0>(*p)] = std::get<1>(*p);
710 std::map<std::string,std::string>::const_iterator
711 p = names_and_descriptions.begin();
718 description += p->first;
719 description +=
"': ";
720 description += p->second;
727 if (p != names_and_descriptions.end())
728 description +=
"\n\n";
739 template <
typename InterfaceClass>
744 Assert (plugins !=
nullptr,
745 ExcMessage (
"No postprocessors registered!?"));
747 for (
typename std::list<PluginInfo>::const_iterator
748 p = plugins->begin();
749 p != plugins->end(); ++p)
750 (std::get<2>(*p))(prm);
755 template <
typename InterfaceClass>
756 std::unique_ptr<InterfaceClass>
759 const std::string &documentation)
762 Assert (plugins !=
nullptr,
763 ExcMessage (
"No postprocessors registered!?"));
764 AssertThrow (name !=
"unspecified",
765 ExcMessage(std::string(
"A plugin must have a name!\n\n" 766 "This function was asked to create a plugin but no name for the " 767 "plugin was provided. This may be due to the fact that you did not " 768 "explicitly specify a name for this plugin in your input file and " 769 "ASPECT does not provide a default for this kind of plugin, for " 770 "example because no generally useful plugin exists. An example " 771 "is that there is no default geometry: You need to explicitly " 772 "provide one in the input file, and it seems like you have not " 774 "To find out which kind of plugin this function tries to create, " 775 "take a look at the backtrace of this error message.\n\n" 776 "The place that called this function also provided as " 777 "additional information this:\n\n" 779 + documentation +
">"));
781 for (
typename std::list<PluginInfo>::const_iterator p = plugins->begin();
782 p != plugins->end(); ++p)
783 if (std::get<0>(*p) == name)
785 std::unique_ptr<InterfaceClass> i = std::get<3>(*p)();
789 AssertThrow (
false, ExcUnknownPlugin(name));
795 template <
typename InterfaceClass>
796 std::unique_ptr<InterfaceClass>
799 const std::string &documentation,
800 ParameterHandler &prm)
802 std::unique_ptr<InterfaceClass> i = create_plugin(name, documentation);
803 i->parse_parameters (prm);
809 template <
typename InterfaceClass>
813 std::ostream &output_stream,
814 const std::string &attachment_point)
822 output_stream << std::string(
typeid(InterfaceClass).name())
824 << plugin_system_name
825 <<
"\", height=.8,width=.8,shape=\"rect\",fillcolor=\"lightgreen\"]" 837 std::map<std::string, typename std::list<PluginInfo>::const_iterator>
839 for (
typename std::list<PluginInfo>::const_iterator p = plugins->begin();
840 p != plugins->end(); ++p)
841 plugin_map[std::get<0>(*p)] = p;
844 for (
typename std::map<std::string,
typename std::list<PluginInfo>::const_iterator>::const_iterator
845 p = plugin_map.begin();
846 p != plugin_map.end(); ++p)
852 const std::vector<std::string> plugin_label_parts
853 = ::Utilities::break_text_into_lines(p->first, 15);
854 Assert (plugin_label_parts.size()>0, ExcInternalError());
855 std::string plugin_name = plugin_label_parts[0];
856 for (
unsigned int i=1; i<plugin_label_parts.size(); ++i)
857 plugin_name +=
"\\n" + plugin_label_parts[i];
862 std::unique_ptr<InterfaceClass> instance (create_plugin (p->first,
""));
863 const std::string node_name =
typeid(*instance).name();
866 output_stream << node_name
869 <<
"\", height=.8,width=.8,shape=\"circle\",fillcolor=\"lightblue\"];" 874 output_stream << node_name
876 << std::string(
typeid(InterfaceClass).name())
877 <<
" [len=3, weight=50]" 887 output_stream <<
"SimulatorAccess" 890 <<
" [style=\"dotted\", arrowhead=\"empty\", constraint=false, color=\"gray\", len=20, weight=0.1];" 897 output_stream << std::string(
typeid(InterfaceClass).name())
900 <<
" [len=15, weight=50]" 906 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.")