source: finroc_plugins_composite_ports/tInterface.h @ 10:475d7a686fd6

Last change on this file since 10:475d7a686fd6 was 10:475d7a686fd6, checked in by Max Reichardt <mreichardt@…>, 2 years ago

Adds support for inheritance among port composite interfaces (sub and superclass interfaces can be connected). Change also includes that connecting secondary backends (of port composite interfaces with multiple port types) will not connect the primary. This adds more flexibility/convenience w.r.t. connecting only parts of port composite interfaces.

File size: 8.1 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/tInterface.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2020-01-02
27 *
28 * \brief   Contains tInterface
29 *
30 * \b tInterface
31 *
32 * Base class for port composite interfaces (interfaces consisting of a set of ports)
33 *
34 */
35//----------------------------------------------------------------------
36#ifndef __plugins__structure__tInterface_h__
37#define __plugins__structure__tInterface_h__
38
39//----------------------------------------------------------------------
40// External includes (system with <>, local with "")
41//----------------------------------------------------------------------
42#include "rrlib/util/tTypeList.h"
43#include "core/port/tPortGroup.h"
44
45//----------------------------------------------------------------------
46// Internal includes with ""
47//----------------------------------------------------------------------
48#include "plugins/composite_ports/internal/type_traits.h"
49#include "plugins/composite_ports/tInterfaceType.h"
50#include "plugins/composite_ports/internal/tGenericPortType.h"
51
52//----------------------------------------------------------------------
53// Namespace declaration
54//----------------------------------------------------------------------
55namespace finroc
56{
57
58//----------------------------------------------------------------------
59// Forward declarations / typedefs / enums
60//----------------------------------------------------------------------
61
62namespace structure
63{
64class tComponent;
65}
66
67namespace composite_ports
68{
69
70namespace port_relation
71{
72
73template <template <typename T> class TPortType>
74using tInput = typename internal::PrimaryPortRelation<internal::tPortTypeRelation::ePTR_INPUT, TPortType, internal::MakeInputPort>;
75template <template <typename> class TPortType>
76using tOutput = typename internal::PrimaryPortRelation<internal::tPortTypeRelation::ePTR_OUTPUT, TPortType, internal::MakeOutputPort>;
77template <template <typename> class TPortType>
78using tParameter = typename internal::PrimaryPortRelation<internal::tPortTypeRelation::ePTR_PARAMETER, TPortType, internal::MakeParameter>;
79template <template <typename> class TPortType>
80using tCounterPart = typename internal::PrimaryPortRelation<internal::tPortTypeRelation::ePTR_COUNTER_PART, TPortType, internal::MakeCounterPart>;
81template <template <typename> class TPortType>
82using tCounterPartParameterInputs = typename internal::PrimaryPortRelation<internal::tPortTypeRelation::ePTR_COUNTER_PART_PARAMETER_IF_INPUT, TPortType, internal::MakeCounterPartParameterIfInput>; //::template base_port;
83
84}
85
86//----------------------------------------------------------------------
87// Class declaration
88//----------------------------------------------------------------------
89//! Port composite interface template base class
90/*!
91 * Base class for port composite interfaces (interfaces consisting of a set of ports)
92 */
93template <template <typename> class TPrimaryPort>
94class tInterface : public tInterfaceBase
95{
96  enum { cCONVENIENCE_PORT = std::is_base_of<structure::tConveniencePortBase, TPrimaryPort<double>>::value };
97  //enum { cGENERIC_INSTANCE = std::is_same<TPrimaryPort<double>, internal::tGenericPortType<0, double, false>>::value };
98  enum { cPRIMARY_RELATION_ID = internal::GetRelationId<TPrimaryPort<double>>::value };
99
100//----------------------------------------------------------------------
101// Public methods and typedefs
102//----------------------------------------------------------------------
103public:
104
105  typedef typename std::conditional<cCONVENIENCE_PORT, structure::tComponent*, core::tPortGroup*>::type tParent;
106  typedef tInterfaceBase tBase;
107
108//----------------------------------------------------------------------
109// Protected methods
110//----------------------------------------------------------------------
111protected:
112
113  tInterface() :
114    tInterfaceBase(nullptr)
115  {}
116
117  /*!
118   * \param interface_type Interface type
119   * \param parent Parent (component for convenience ports as template arguments - and port group(s) otherwise)
120   * \param name Name of interface (must be unique in parent)
121   * \param port_type_relation If this port composite interface has multiple data port types, specifies how they are typically related (relevant for generic instantiation)
122   * \param custom_connect_function Custom connect function for this interface
123   */
124  tInterface(const rrlib::rtti::tType& interface_type, tParent parent, const std::string& name, const tConnectFunction& custom_connect_function = tConnectFunction()) :
125    tInterfaceBase(new tInterfaceBase::tBackend(interface_type, parent, GetPrimaryPortTypeParent(parent), cPRIMARY_RELATION_ID, name, cCONVENIENCE_PORT, custom_connect_function, GetDefaultCreateMissingInterfaceFunction(parent)))
126  {
127  }
128
129  /*!
130   * (Constructor for other port composite interface as parent)
131   *
132   * \param interface_type Interface type
133   * \param parent Parent port composite interface
134   * \param name Name of interface (must be unique in parent)
135   * \param custom_connect_function Custom connect function for this interface
136   */
137  template <typename TParent, bool Tenable = std::is_base_of<tInterfaceBase, typename std::decay<TParent>::type>::value>
138  tInterface(typename std::enable_if<Tenable, const rrlib::rtti::tType&>::type interface_type, TParent* parent, const std::string& name, const tConnectFunction& custom_connect_function = tConnectFunction()) :
139    tInterfaceBase(parent->Backend() ? new tInterfaceBase::tBackend(interface_type, cCONVENIENCE_PORT ? parent->ParentComponent() : parent->Backend(), parent->Backend()->GetBackend(cPRIMARY_RELATION_ID), cPRIMARY_RELATION_ID, name, cCONVENIENCE_PORT, custom_connect_function) : nullptr)
140  {
141  }
142
143  /*! Helper constructor to extract interface type */
144  tInterface(const rrlib::rtti::tType& interface_type, tInterfaceTypeData* parent, const std::string& name, const tConnectFunction& custom_connect_function = tConnectFunction()) :
145    tInterfaceBase()
146  {
147    parent->type = interface_type;
148  }
149
150//----------------------------------------------------------------------
151// Private fields and methods
152//----------------------------------------------------------------------
153private:
154
155  template <template <typename> class UPrimaryPort>
156  friend class tInterface;
157
158  template <int Tconvenience_port = cCONVENIENCE_PORT>
159  static core::tFrameworkElement* GetPrimaryPortTypeParent(typename std::enable_if<Tconvenience_port, tParent>::type parent)
160  {
161    return &TPrimaryPort<double>::GetParentInterface(static_cast<typename TPrimaryPort<double>::tComponent&>(*parent));
162  }
163
164  static core::tFrameworkElement* GetPrimaryPortTypeParent(core::tPortGroup* parent)
165  {
166    return parent;
167  }
168
169  template <int Tconvenience_port = cCONVENIENCE_PORT>
170  static constexpr tCreateMissingComponentInterfaceFunction GetDefaultCreateMissingInterfaceFunction(typename std::enable_if<Tconvenience_port, tParent>::type parent)
171  {
172    return tInterfaceBase::GetDefaultCreateMissingInterfaceFunction<typename TPrimaryPort<double>::tComponent>();
173  }
174  static constexpr tCreateMissingComponentInterfaceFunction GetDefaultCreateMissingInterfaceFunction(core::tPortGroup* parent)
175  {
176    return nullptr;
177  }
178
179};
180
181//----------------------------------------------------------------------
182// End of namespace declaration
183//----------------------------------------------------------------------
184}
185}
186
187
188#endif
Note: See TracBrowser for help on using the repository browser.