source: finroc_plugins_composite_ports/tInterface.h @ 8:1cdb6fac7784

Last change on this file since 8:1cdb6fac7784 was 8:1cdb6fac7784, checked in by Max Reichardt <mreichardt@…>, 2 years ago

Makes port composite interface auto-create component interfaces that have not-yet been created (currently, this is a semi-pretty workaround that supports all components defined in finroc_plugins_structure - notably without a depedency to this plugin and also memory-efficient)

File size: 8.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/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
107//----------------------------------------------------------------------
108// Protected methods
109//----------------------------------------------------------------------
110protected:
111
112  tInterface() :
113    tInterfaceBase(nullptr)
114  {}
115
116  /*!
117   * \param interface_type Interface type
118   * \param parent Parent (component for convenience ports as template arguments - and port group(s) otherwise)
119   * \param name Name of interface (must be unique in parent)
120   * \param port_type_relation If this port composite interface has multiple data port types, specifies how they are typically related (relevant for generic instantiation)
121   * \param custom_connect_function Custom connect function for this interface
122   */
123  tInterface(const rrlib::rtti::tType& interface_type, tParent parent, const std::string& name, const tConnectFunction& custom_connect_function = tConnectFunction()) :
124    tInterfaceBase(new tInterfaceBase::tBackend(interface_type, parent, GetPrimaryPortTypeParent(parent), cPRIMARY_RELATION_ID, name, cCONVENIENCE_PORT, custom_connect_function, GetDefaultCreateMissingInterfaceFunction(parent)))
125  {
126  }
127
128  /*!
129   * (Constructor for other port composite interface as parent)
130   *
131   * \param interface_type Interface type
132   * \param parent Parent port composite interface
133   * \param name Name of interface (must be unique in parent)
134   * \param custom_connect_function Custom connect function for this interface
135   */
136  template <typename TParent, bool Tenable = std::is_base_of<tInterfaceBase, typename std::decay<TParent>::type>::value>
137  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()) :
138    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)
139  {
140  }
141
142  /*! Helper constructor to extract interface type */
143  tInterface(const rrlib::rtti::tType& interface_type, tInterfaceTypeData* parent, const std::string& name, const tConnectFunction& custom_connect_function = tConnectFunction()) :
144    tInterfaceBase()
145  {
146    parent->type = interface_type;
147  }
148
149//----------------------------------------------------------------------
150// Private fields and methods
151//----------------------------------------------------------------------
152private:
153
154  template <template <typename> class UPrimaryPort>
155  friend class tInterface;
156
157  template <int Tconvenience_port = cCONVENIENCE_PORT>
158  static core::tFrameworkElement* GetPrimaryPortTypeParent(typename std::enable_if<Tconvenience_port, tParent>::type parent)
159  {
160    return &TPrimaryPort<double>::GetParentInterface(static_cast<typename TPrimaryPort<double>::tComponent&>(*parent));
161  }
162
163  static core::tFrameworkElement* GetPrimaryPortTypeParent(core::tPortGroup* parent)
164  {
165    return parent;
166  }
167
168  template <int Tconvenience_port = cCONVENIENCE_PORT>
169  static constexpr tCreateMissingComponentInterfaceFunction GetDefaultCreateMissingInterfaceFunction(typename std::enable_if<Tconvenience_port, tParent>::type parent)
170  {
171    return tInterfaceBase::GetDefaultCreateMissingInterfaceFunction<typename TPrimaryPort<double>::tComponent>();
172  }
173  static constexpr tCreateMissingComponentInterfaceFunction GetDefaultCreateMissingInterfaceFunction(core::tPortGroup* parent)
174  {
175    return nullptr;
176  }
177
178};
179
180//----------------------------------------------------------------------
181// End of namespace declaration
182//----------------------------------------------------------------------
183}
184}
185
186
187#endif
Note: See TracBrowser for help on using the repository browser.