source: finroc_plugins_composite_ports/tInterfaceModifier.h @ 26:f783224f355c

Last change on this file since 26:f783224f355c was 26:f783224f355c, checked in by Max Reichardt <mreichardt@…>, 15 months ago

Optimizes skill modifier integration (in particular, modifiers may now be attached to any node in the framework element tree)

File size: 6.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/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. Returning 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     * \return Created port. nullptr if no port is to be created.
105     */
106    virtual core::tAbstractPort* CreatePort(const core::tAbstractPortCreationInfo& creation_info, int primary_relation_id) const
107    {
108      if (data_ports::IsDataFlowType(creation_info.data_type))
109      {
110        return data_ports::tGenericPort(static_cast<const data_ports::common::tAbstractDataPortCreationInfo&>(creation_info)).GetWrapped();
111      }
112      return core::tPortFactory::CreatePort(creation_info.name, *creation_info.parent, creation_info.data_type, creation_info.flags);
113    }
114
115    /*!
116     * \param type Port composite interface type to create
117     * \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)
118     */
119    virtual tInterfaceBase::tPrimaryPortType PartialInterface(const rrlib::rtti::tType& type)
120    {
121      return tInterfaceBase::tPrimaryPortType::ANY;
122    }
123
124  };
125
126  /*! Variant that will not delete implementation on destruction */
127  tInterfaceModifier(tImplementation& implementation) :
128    implementation(&implementation)
129  {}
130
131  /*! Variant that will delete implementation on destruction */
132  tInterfaceModifier(std::unique_ptr<tImplementation>& implementation) :
133    implementation(implementation.get()),
134    implementation_managed(std::move(implementation))
135  {}
136
137  /*!
138   * \return Wrapped implementation instance
139   */
140  tImplementation& Implementation()
141  {
142    return *implementation;
143  }
144
145//----------------------------------------------------------------------
146// Private fields and methods
147//----------------------------------------------------------------------
148private:
149
150  tImplementation* implementation;
151  std::unique_ptr<tImplementation> implementation_managed;
152};
153
154//----------------------------------------------------------------------
155// End of namespace declaration
156//----------------------------------------------------------------------
157}
158}
159
160
161#endif
Note: See TracBrowser for help on using the repository browser.