@@ -430,28 +430,126 @@ class ResourceStorage
430430 return result;
431431 }
432432
433+ void insert_state_interface (std::shared_ptr<StateInterface> state_interface)
434+ {
435+ const auto [it, success] =
436+ state_interface_map_.insert (std::make_pair (state_interface->get_name (), state_interface));
437+ if (!success)
438+ {
439+ std::string msg (
440+ " ResourceStorage: Tried to insert StateInterface with already existing key. Insert[" +
441+ state_interface->get_name () + " ]" );
442+ throw std::runtime_error (msg);
443+ }
444+ }
445+
446+ // TODO(Manuel) BEGIN: for backward compatibility, can be removed if export_state_interfaces()
447+ // method is removed
448+ void insert_state_interface (const StateInterface & state_interface)
449+ {
450+ const auto [it, success] = state_interface_map_.emplace (std::make_pair (
451+ state_interface.get_name (), std::make_shared<StateInterface>(state_interface)));
452+ if (!success)
453+ {
454+ std::string msg (
455+ " ResourceStorage: Tried to insert StateInterface with already existing key. Insert[" +
456+ state_interface.get_name () + " ]" );
457+ throw std::runtime_error (msg);
458+ }
459+ }
460+ // TODO(Manuel) END: for backward compatibility
461+
433462 template <class HardwareT >
434463 void import_state_interfaces (HardwareT & hardware)
435464 {
436- auto interfaces = hardware.export_state_interfaces ();
437465 std::vector<std::string> interface_names;
438- interface_names.reserve (interfaces.size ());
439- for (auto & interface : interfaces)
466+ std::vector<StateInterface> interfaces = hardware.export_state_interfaces ();
467+ // If no StateInterfaces has been exported, this could mean:
468+ // a) there is nothing to export -> on_export_state_interfaces() does return nothing as well
469+ // b) default implementation for export_state_interfaces() is used -> new functionality ->
470+ // Framework exports and creates everything
471+ if (interfaces.size () == 0 )
440472 {
441- auto key = interface.get_name ();
442- state_interface_map_.emplace (std::make_pair (key, std::move (interface)));
443- interface_names.push_back (key);
473+ std::vector<std::shared_ptr<StateInterface>> interface_ptrs =
474+ hardware.on_export_state_interfaces ();
475+ interface_names.reserve (interface_ptrs.size ());
476+ for (auto const & interface : interface_ptrs)
477+ {
478+ insert_state_interface (interface);
479+ interface_names.push_back (interface->get_name ());
480+ }
444481 }
482+ // TODO(Manuel) BEGIN: for backward compatibility, can be removed if export_state_interfaces()
483+ // method is removed
484+ else
485+ {
486+ interface_names.reserve (interfaces.size ());
487+ for (auto const & interface : interfaces)
488+ {
489+ insert_state_interface (interface);
490+ interface_names.push_back (interface.get_name ());
491+ }
492+ }
493+ // TODO(Manuel) END: for backward compatibility
494+
445495 hardware_info_map_[hardware.get_name ()].state_interfaces = interface_names;
446496 available_state_interfaces_.reserve (
447497 available_state_interfaces_.capacity () + interface_names.size ());
448498 }
449499
500+ void insert_command_interface (std::shared_ptr<CommandInterface> command_interface)
501+ {
502+ const auto [it, success] = command_interface_map_.insert (
503+ std::make_pair (command_interface->get_name (), command_interface));
504+ if (!success)
505+ {
506+ std::string msg (
507+ " ResourceStorage: Tried to insert CommandInterface with already existing key. Insert[" +
508+ command_interface->get_name () + " ]" );
509+ throw std::runtime_error (msg);
510+ }
511+ }
512+
513+ // TODO(Manuel) BEGIN: for backward compatibility, can be removed if export_command_interfaces()
514+ // method is removed
515+ void insert_command_interface (CommandInterface && command_interface)
516+ {
517+ std::string key = command_interface.get_name ();
518+ const auto [it, success] = command_interface_map_.emplace (
519+ std::make_pair (key, std::make_shared<CommandInterface>(std::move (command_interface))));
520+ if (!success)
521+ {
522+ std::string msg (
523+ " ResourceStorage: Tried to insert CommandInterface with already existing key. Insert[" +
524+ key + " ]" );
525+ throw std::runtime_error (msg);
526+ }
527+ }
528+ // TODO(Manuel) END: for backward compatibility
529+
450530 template <class HardwareT >
451531 void import_command_interfaces (HardwareT & hardware)
452532 {
453533 auto interfaces = hardware.export_command_interfaces ();
454- hardware_info_map_[hardware.get_name ()].command_interfaces = add_command_interfaces (interfaces);
534+ // If no CommandInterface has been exported, this could mean:
535+ // a) there is nothing to export -> on_export_command_interfaces() does return nothing as well
536+ // b) default implementation for export_command_interfaces() is used -> new functionality ->
537+ // Framework exports and creates everything
538+ if (interfaces.size () == 0 )
539+ {
540+ std::vector<std::shared_ptr<CommandInterface>> interface_ptrs =
541+ hardware.on_export_command_interfaces ();
542+ hardware_info_map_[hardware.get_name ()].command_interfaces =
543+ add_command_interfaces (interface_ptrs);
544+ }
545+ // TODO(Manuel) BEGIN: for backward compatibility, can be removed if export_command_interfaces()
546+ // method is removed
547+ else
548+ {
549+ hardware_info_map_[hardware.get_name ()].command_interfaces =
550+ add_command_interfaces (interfaces);
551+ }
552+ // TODO(Manuel) END: for backward compatibility
455553 }
456554
457555 // / Adds exported command interfaces into internal storage.
@@ -465,14 +563,35 @@ class ResourceStorage
465563 * \returns list of interface names that are added into internal storage. The output is used to
466564 * avoid additional iterations to cache interface names, e.g., for initializing info structures.
467565 */
566+ // TODO(Manuel) BEGIN: for backward compatibility, can be removed if export_command_interfaces()
567+ // method is removed
468568 std::vector<std::string> add_command_interfaces (std::vector<CommandInterface> & interfaces)
469569 {
470570 std::vector<std::string> interface_names;
471571 interface_names.reserve (interfaces.size ());
472572 for (auto & interface : interfaces)
473573 {
474574 auto key = interface.get_name ();
475- command_interface_map_.emplace (std::make_pair (key, std::move (interface)));
575+ insert_command_interface (std::move (interface));
576+ claimed_command_interface_map_.emplace (std::make_pair (key, false ));
577+ interface_names.push_back (key);
578+ }
579+ available_command_interfaces_.reserve (
580+ available_command_interfaces_.capacity () + interface_names.size ());
581+
582+ return interface_names;
583+ }
584+ // TODO(Manuel) END: for backward compatibility
585+
586+ std::vector<std::string> add_command_interfaces (
587+ std::vector<std::shared_ptr<CommandInterface>> & interfaces)
588+ {
589+ std::vector<std::string> interface_names;
590+ interface_names.reserve (interfaces.size ());
591+ for (auto & interface : interfaces)
592+ {
593+ auto key = interface->get_name ();
594+ insert_command_interface (interface);
476595 claimed_command_interface_map_.emplace (std::make_pair (key, false ));
477596 interface_names.push_back (key);
478597 }
@@ -709,9 +828,9 @@ class ResourceStorage
709828 std::unordered_map<std::string, std::vector<std::string>> controllers_reference_interfaces_map_;
710829
711830 // / Storage of all available state interfaces
712- std::map<std::string, StateInterface> state_interface_map_;
831+ std::map<std::string, std::shared_ptr< StateInterface> > state_interface_map_;
713832 // / Storage of all available command interfaces
714- std::map<std::string, CommandInterface> command_interface_map_;
833+ std::map<std::string, std::shared_ptr< CommandInterface> > command_interface_map_;
715834
716835 // / Vectors with interfaces available to controllers (depending on hardware component state)
717836 std::vector<std::string> available_state_interfaces_;
@@ -810,7 +929,7 @@ LoanedStateInterface ResourceManager::claim_state_interface(const std::string &
810929 }
811930
812931 std::lock_guard<std::recursive_mutex> guard (resource_interfaces_lock_);
813- return LoanedStateInterface (resource_storage_->state_interface_map_ .at (key));
932+ return LoanedStateInterface (*( resource_storage_->state_interface_map_ .at (key) ));
814933}
815934
816935// CM API: Called in "callback/slow"-thread
@@ -981,7 +1100,7 @@ LoanedCommandInterface ResourceManager::claim_command_interface(const std::strin
9811100 resource_storage_->claimed_command_interface_map_ [key] = true ;
9821101 std::lock_guard<std::recursive_mutex> guard (resource_interfaces_lock_);
9831102 return LoanedCommandInterface (
984- resource_storage_->command_interface_map_ .at (key),
1103+ *( resource_storage_->command_interface_map_ .at (key) ),
9851104 std::bind (&ResourceManager::release_command_interface, this , key));
9861105}
9871106
0 commit comments