source: finroc_plugins_composite_ports/tInterfaceModifier.h @ 23:3c9cac2a0cc1

Last change on this file since 23:3c9cac2a0cc1 was 23:3c9cac2a0cc1, checked in by Max Reichardt <mreichardt@…>, 2 years ago

Adds 'interface modifiers' for customization of port composite interface creation (as there are diverse use cases). Refactors and tidies partial interface creation with this new feature.

File size: 5.9 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/tInterfaceModifier.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2021-03-01
27 *
28 * \brief   Contains tInterfaceModifier
29 *
30 * \b tInterfaceModifier
31 *
32 * Can be attached to components in order to customize interfaces that are instantiated
33 * with this component as parent.
34 *
35 */
36//----------------------------------------------------------------------
37#ifndef __plugins__composite_ports__tInterfaceModifier_h__
38#define __plugins__composite_ports__tInterfaceModifier_h__
39
40//----------------------------------------------------------------------
41// External includes (system with <>, local with "")
42//----------------------------------------------------------------------
43#include "core/port/tPortFactory.h"
44
45//----------------------------------------------------------------------
46// Internal includes with ""
47//----------------------------------------------------------------------
48#include "plugins/composite_ports/tInterfaceBase.h"
49
50//----------------------------------------------------------------------
51// Namespace declaration
52//----------------------------------------------------------------------
53namespace finroc
54{
55namespace composite_ports
56{
57
58//----------------------------------------------------------------------
59// Forward declarations / typedefs / enums
60//----------------------------------------------------------------------
61
62//----------------------------------------------------------------------
63// Class declaration
64//----------------------------------------------------------------------
65//! Interface Modifier
66/*!
67 * Can be attached to components in order to customize interfaces that are instantiated
68 * with this component as parent.
69 */
70class tInterfaceModifier : public core::tAnnotation
71{
72
73//----------------------------------------------------------------------
74// Public methods and typedefs
75//----------------------------------------------------------------------
76public:
77
78  /*!
79   * Base class for implementation.
80   * Separate from tInterfaceModifier, to allow tInterfaceModifier's attachment as annotation.
81   */
82  class tImplementation
83  {
84  public:
85
86    /*!
87     * \param default_component_interface Component interface below which interface element would be created by default
88     * \param primary_relation_id Primary relation ID, as used in interface classes (zero is primary port type)
89     * \return Return Component interface below which interface element should be created. Return nullptr will not create element.
90     */
91    virtual core::tPortGroup* GetComponentInterface(core::tPortGroup* default_component_interface, int primary_relation_id) const
92    {
93      return default_component_interface;
94    }
95
96    /*!
97     * Creates port.
98     * Called for all data and RPC ports to be created below port composite interface root.
99     * This function may modify any property of port - or may not create it.
100     * (The base class implementation contains the default behavior)
101     *
102     * \param creation_info Creation info that would be used in default implementation to create port
103     * \param primary_relation_id Primary relation ID, as used in interface classes (zero is primary port type)
104     */
105    virtual core::tAbstractPort* CreatePort(const core::tAbstractPortCreationInfo& creation_info, int primary_relation_id) const
106    {
107      if (data_ports::IsDataFlowType(creation_info.data_type))
108      {
109        return data_ports::tGenericPort(static_cast<const data_ports::common::tAbstractDataPortCreationInfo&>(creation_info)).GetWrapped();
110      }
111      return core::tPortFactory::CreatePort(creation_info.name, *creation_info.parent, creation_info.data_type, creation_info.flags);
112    }
113
114    /*!
115     * \param type Port composite interface type to create
116     * \return Whether to create partial interface? (CONTROLLER_PORT creates controller ports variant, SENSOR_PORT creates sensor ports variant, ANY creates partial interface with its primary port type)
117     */
118    virtual tInterfaceBase::tPrimaryPortType PartialInterface(const rrlib::rtti::tType& type)
119    {
120      return tInterfaceBase::tPrimaryPortType::ANY;
121    }
122
123  };
124
125  /*! Variant that will not delete implementation on destruction */
126  tInterfaceModifier(tImplementation& implementation) :
127    implementation(&implementation)
128  {}
129
130  /*! Variant that will delete implementation on destruction */
131  tInterfaceModifier(std::unique_ptr<tImplementation>& implementation) :
132    implementation(implementation.get()),
133    implementation_managed(std::move(implementation))
134  {}
135
136  /*!
137   * \return Wrapped implementation instance
138   */
139  tImplementation& Implementation()
140  {
141    return *implementation;
142  }
143
144//----------------------------------------------------------------------
145// Private fields and methods
146//----------------------------------------------------------------------
147private:
148
149  tImplementation* implementation;
150  std::unique_ptr<tImplementation> implementation_managed;
151};
152
153//----------------------------------------------------------------------
154// End of namespace declaration
155//----------------------------------------------------------------------
156}
157}
158
159
160#endif
Note: See TracBrowser for help on using the repository browser.