source: finroc_plugins_composite_ports/auto_connect.hpp @ 28:0d9e9fd957de

tip
Last change on this file since 28:0d9e9fd957de was 21:76b50c739756, checked in by Max Reichardt <mreichardt@…>, 14 months ago

Fixes clang compiler warning

File size: 5.0 KB
Line 
1//
2// You received this file as part of Finroc
3// A framework for intelligent robot control
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    plugins/composite_ports/auto_connect.hpp
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2020-02-18
27 *
28 */
29//----------------------------------------------------------------------
30
31//----------------------------------------------------------------------
32// External includes (system with <>, local with "")
33//----------------------------------------------------------------------
34
35//----------------------------------------------------------------------
36// Internal includes with ""
37//----------------------------------------------------------------------
38#include "plugins/composite_ports/tInterfaceBase.h"
39#include "plugins/composite_ports/internal/tInterfaceTypeInfo.h"
40
41//----------------------------------------------------------------------
42// Debugging
43//----------------------------------------------------------------------
44#include <cassert>
45
46//----------------------------------------------------------------------
47// Namespace declaration
48//----------------------------------------------------------------------
49namespace finroc
50{
51namespace composite_ports
52{
53
54//----------------------------------------------------------------------
55// Forward declarations / typedefs / enums
56//----------------------------------------------------------------------
57
58//----------------------------------------------------------------------
59// Const values
60//----------------------------------------------------------------------
61
62//----------------------------------------------------------------------
63// Implementation
64//----------------------------------------------------------------------
65
66namespace internal
67{
68
69/*!
70 * (Helper function for ConnectChildComponentInterfaces and ConnectFirstChildComponentInterface)
71 *
72 * \param composite_component Composite component whose child components to check
73 * \param interface_type Type of interfaces of interest
74 * \param unconnected_only Whether to only return instances that are not already connected to a port composite interface in composite component's interfaces
75 * \return All interfaces in child components with specified type id
76 */
77std::vector<tInterfaceBase> GetChildComponentInterfaces(core::tFrameworkElement& composite_component, const rrlib::rtti::tType& interface_type, bool unconnected_only);
78
79}
80
81template <typename TComponent, typename TInterface>
82std::string ConnectChildComponentInterfaces(TComponent& composite_component, std::vector<TInterface>& interfaces_variable, const std::string& common_prefix, size_t start_index, const std::string& common_postfix)
83{
84  // Get child component interfaces
85  std::vector<tInterfaceBase> unconnected_child_component_interfaces = internal::GetChildComponentInterfaces(composite_component, tInterfaceBase::GetType<TInterface>(), true);
86
87  // Add and connect new interfaces
88  for (auto & child_component_interface : unconnected_child_component_interfaces)
89  {
90    composite_component.ResizePortVector(interfaces_variable, interfaces_variable.size() + 1, common_prefix, start_index, common_postfix);
91    interfaces_variable.back().ConnectTo(child_component_interface);
92    if (!interfaces_variable.back().GetWrapped()->IsConnectedTo(*child_component_interface.GetWrapped()))
93    {
94      composite_component.ResizePortVector(interfaces_variable, interfaces_variable.size() - 1, common_prefix, start_index, common_postfix);
95      std::stringstream result;
96      result << "Failed to connect '" << *interfaces_variable.back().Backend() << "' and '" << *child_component_interface.Backend() << "'";
97      return result.str();
98    }
99  }
100  return "";
101}
102
103template <typename TComponent, typename TInterface>
104void ConnectFirstChildComponentInterface(TComponent& composite_component, TInterface& interface)
105{
106  std::vector<tInterfaceBase> child_component_interfaces = internal::GetChildComponentInterfaces(composite_component, tInterfaceBase::GetType<TInterface>(), false);
107  if (child_component_interfaces.size())
108  {
109    interface.ConnectTo(child_component_interfaces[0]);
110  }
111}
112
113//----------------------------------------------------------------------
114// End of namespace declaration
115//----------------------------------------------------------------------
116}
117}
Note: See TracBrowser for help on using the repository browser.