source: finroc_plugins_composite_ports/tInterface.h @ 28:0d9e9fd957de

tip
Last change on this file since 28:0d9e9fd957de was 19:d53a682e5e97, checked in by Max Reichardt <max.reichardt@…>, 17 months ago

Adds option to instantiate only one half of a nested interface

File size: 9.4 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
84template <template <typename T> class TPortType>
85using tServerIfInput = typename internal::PrimaryPortRelation<internal::tPortTypeRelation::ePTR_SERVER_IF_INPUT, TPortType, internal::MakeServerIfInput>;
86template <template <typename T> class TPortType>
87using tClientIfInput = typename internal::PrimaryPortRelation<internal::tPortTypeRelation::ePTR_CLIENT_IF_INPUT, TPortType, internal::MakeClientIfInput>;
88
89}
90
91//----------------------------------------------------------------------
92// Class declaration
93//----------------------------------------------------------------------
94//! Port composite interface template base class
95/*!
96 * Base class for port composite interfaces (interfaces consisting of a set of ports)
97 */
98template <template <typename> class TPrimaryPort>
99class tInterface : public tInterfaceBase
100{
101  enum { cCONVENIENCE_PORT = std::is_base_of<structure::tConveniencePortBase, TPrimaryPort<double>>::value };
102  //enum { cGENERIC_INSTANCE = std::is_same<TPrimaryPort<double>, internal::tGenericPortType<0, double, false>>::value };
103  enum { cPRIMARY_RELATION_ID = internal::GetRelationId<TPrimaryPort<double>>::value };
104
105//----------------------------------------------------------------------
106// Public methods and typedefs
107//----------------------------------------------------------------------
108public:
109
110  typedef tInterfaceBase tBase;
111
112//----------------------------------------------------------------------
113// Protected methods
114//----------------------------------------------------------------------
115protected:
116
117  tInterface() :
118    tInterfaceBase(nullptr)
119  {}
120
121  /*!
122   * \param interface_type Interface type
123   * \param parent Parent port group (if TPrimaryPort is a convenience port, this will create a partial interface)
124   * \param name Name of interface (must be unique in parent)
125   * \param primary_port_type Type of ports with relation id zero
126   * \param custom_connect_function Custom connect function for this interface
127   */
128  tInterface(const rrlib::rtti::tType& interface_type, core::tPortGroup* parent, const std::string& name, tPrimaryPortType primary_port_type = tPrimaryPortType::ANY, const tConnectFunction& custom_connect_function = tConnectFunction()) :
129    tInterfaceBase(new tInterfaceBase::tBackend(interface_type, parent, parent, cPRIMARY_RELATION_ID, name, cCONVENIENCE_PORT, custom_connect_function, GetDefaultCreateMissingInterfaceFunction(parent), primary_port_type, cCONVENIENCE_PORT))
130  {
131  }
132
133  /*!
134   * \param interface_type Interface type
135   * \param parent Parent component
136   * \param name Name of interface (must be unique in parent)
137   * \param primary_port_type Type of ports with relation id zero
138   * \param custom_connect_function Custom connect function for this interface
139   */
140  template <bool Tenable = cCONVENIENCE_PORT>
141  tInterface(typename std::enable_if<Tenable, const rrlib::rtti::tType&>::type interface_type, structure::tComponent* parent, const std::string& name, tPrimaryPortType primary_port_type = tPrimaryPortType::ANY, const tConnectFunction& custom_connect_function = tConnectFunction()) :
142    tInterfaceBase(new tInterfaceBase::tBackend(interface_type, parent, GetPrimaryPortTypeParent(parent), cPRIMARY_RELATION_ID, name, cCONVENIENCE_PORT, custom_connect_function, GetDefaultCreateMissingInterfaceFunction(parent), primary_port_type, false))
143  {
144  }
145
146  /*!
147   * (Constructor for other port composite interface as parent)
148   *
149   * \param interface_type Interface type
150   * \param parent Parent port composite interface
151   * \param name Name of interface (must be unique in parent)
152   * \param primary_port_type Type of ports with relation id zero
153   * \param custom_connect_function Custom connect function for this interface
154   */
155  template < typename TParent, bool Tenable = std::is_base_of<tInterfaceBase, typename std::decay<TParent>::type>::value || std::is_base_of<tInterfaceBase::tBackend, typename std::decay<TParent>::type>::value >
156  tInterface(typename std::enable_if<Tenable, const rrlib::rtti::tType&>::type interface_type, TParent* parent, const std::string& name, tPrimaryPortType primary_port_type = tPrimaryPortType::ANY, const tConnectFunction& custom_connect_function = tConnectFunction()) :
157    tInterfaceBase(tBase::CreateBackend(interface_type, parent, name, primary_port_type, custom_connect_function, cPRIMARY_RELATION_ID, cCONVENIENCE_PORT))
158  {
159  }
160
161  /*! Helper constructor to extract interface type */
162  tInterface(const rrlib::rtti::tType& interface_type, tInterfaceTypeData* parent, const std::string& name, tPrimaryPortType primary_port_type = tPrimaryPortType::ANY, const tConnectFunction& custom_connect_function = tConnectFunction()) :
163    tInterfaceBase()
164  {
165    parent->type = interface_type;
166    parent->primary_port_type = primary_port_type;
167  }
168
169//----------------------------------------------------------------------
170// Private fields and methods
171//----------------------------------------------------------------------
172private:
173
174  template <template <typename> class UPrimaryPort>
175  friend class tInterface;
176
177  template <int Tconvenience_port = cCONVENIENCE_PORT>
178  static core::tFrameworkElement* GetPrimaryPortTypeParent(typename std::enable_if<Tconvenience_port, structure::tComponent*>::type parent)
179  {
180    return &TPrimaryPort<double>::GetParentInterface(static_cast<typename TPrimaryPort<double>::tComponent&>(*parent));
181  }
182
183  template <int Tconvenience_port = cCONVENIENCE_PORT>
184  static constexpr tCreateMissingComponentInterfaceFunction GetDefaultCreateMissingInterfaceFunction(typename std::enable_if<Tconvenience_port, core::tFrameworkElement*>::type parent)
185  {
186    return tInterfaceBase::GetDefaultCreateMissingInterfaceFunction<typename TPrimaryPort<double>::tComponent>();
187  }
188  template <int Tconvenience_port = cCONVENIENCE_PORT>
189  static constexpr tCreateMissingComponentInterfaceFunction GetDefaultCreateMissingInterfaceFunction(typename std::enable_if < !Tconvenience_port, core::tFrameworkElement* >::type parent)
190  {
191    return nullptr;
192  }
193
194};
195
196//----------------------------------------------------------------------
197// End of namespace declaration
198//----------------------------------------------------------------------
199}
200}
201
202
203#endif
Note: See TracBrowser for help on using the repository browser.