source: finroc_core/port/tConnectOptions.h @ 425:c7a7cd7e096b

17.03
Last change on this file since 425:c7a7cd7e096b was 425:c7a7cd7e096b, checked in by Max Reichardt <mreichardt@…>, 3 years ago

Makes big Finroc 17.03 update: Introduces connector classes with diverse options such as type conversion. Adapts/refactors respective code (e.g. runtime listener interface is adapted for connectors) - and furthemore tidies other minor functionality. Renames callback methods to more fitting/intuitive names (On*).

File size: 6.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    core/port/tConnectOptions.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2016-07-12
27 *
28 * \brief   Contains tConnectOptions
29 *
30 * \b tConnectOptions
31 *
32 * Bundles available options for connecting ports (bundled for convenience and locality of changes)
33 *
34 */
35//----------------------------------------------------------------------
36#ifndef __core__port__tConnectOptions_h__
37#define __core__port__tConnectOptions_h__
38
39//----------------------------------------------------------------------
40// External includes (system with <>, local with "")
41//----------------------------------------------------------------------
42#include "rrlib/util/tEnumBasedFlags.h"
43#include "rrlib/rtti/rtti.h"
44#include "rrlib/rtti_conversion/tConversionOperationSequence.h"
45
46//----------------------------------------------------------------------
47// Internal includes with ""
48//----------------------------------------------------------------------
49
50//----------------------------------------------------------------------
51// Namespace declaration
52//----------------------------------------------------------------------
53namespace finroc
54{
55namespace core
56{
57
58//----------------------------------------------------------------------
59// Forward declarations / typedefs / enums
60//----------------------------------------------------------------------
61/*!
62 * Flags that can be set for connections/connectors
63 */
64enum class tConnectionFlag
65{
66  // constant flags that are always transferred to finstruct
67  FINSTRUCTED,           //!< Was this connector created using finstruct (or loaded from XML file)?
68  RECONNECT,             //!< Recreate connection when connected port was deleted and a port with the same qualified name appears (Reconnecting is automatically done for network connections and connections to VOLATILE-flagged ports)
69  OPTIONAL,              //!< Flags optional connections. They are handled and reported as non-critical if they cannot be established.
70  SCHEDULING_NEUTRAL,    //!< Flags connections that should not have any effect on scheduling order
71
72  // Connect direction
73  // If none is set, connection direction is determined automatically. This should be appropriate for almost any situation. However, another direction may be enforced)
74  // Both flags must not be set at the same time (typically causes an invalid_argument exception)
75  DIRECTION_TO_DESTINATION, //!< Specified port is destination port (port passed as argument to ConnectTo member function; or second port passed to static method)
76  DIRECTION_TO_SOURCE,      //!< Specified port is destination port (port passed as argument to ConnectTo member function; or second port passed to static method)
77
78  // internal flag
79  NON_PRIMARY_CONNECTOR, //!< Connector was created and is represented by higher-level connector (that still exists) (-> e.g. not necessary to transfer this connector to finstruct)
80
81  // these flags are automatically set
82  PUBLISHED,             //!< Only relevant for URI connectors: Has been published
83  DISCONNECTED,          //!< Connector has been removed
84  CONVERSION             //!< Connector has type conversion operations attached that must be applied on publishing values
85};
86
87/*!
88 * Set of framework element flags.
89 */
90typedef rrlib::util::tEnumBasedFlags<tConnectionFlag, uint32_t> tConnectionFlags;
91
92//----------------------------------------------------------------------
93// Class declaration
94//----------------------------------------------------------------------
95//! Connect Options
96/*!
97 * Bundles available options for connecting ports (bundled for convenience and locality of changes)
98 */
99struct tConnectOptions
100{
101  /*! Any explicit conversion operations to apply (implicit type conversions are applied automatically) */
102  rrlib::rtti::conversion::tConversionOperationSequence conversion_operations;
103
104  /*! Flags for connection */
105  tConnectionFlags flags;
106
107
108  tConnectOptions() : conversion_operations(), flags() {}
109
110  /*!
111   * Convenience constructor so that e.g. single flags can also be used in ConnectTo methods
112   *
113   * \param option1 Any valid option (valid parameter for Set() method)
114   */
115  template <typename TOption1>
116  tConnectOptions(const TOption1& option1) : tConnectOptions()
117  {
118    Set(option1);
119  }
120  /*!
121   * \param conversion_operations Any explicit conversion operations to apply (implicit type conversions are applied automatically)
122   * \param flags Flags for connection
123   */
124  tConnectOptions(const rrlib::rtti::conversion::tConversionOperationSequence& conversion_operations, const tConnectionFlags& flags) :
125    conversion_operations(conversion_operations),
126    flags(flags)
127  {}
128
129  void Set(const rrlib::rtti::conversion::tConversionOperationSequence& conversion_operations)
130  {
131    this->conversion_operations = conversion_operations;
132  }
133  void Set(const tConnectionFlags& flags)
134  {
135    this->flags = flags;
136  }
137};
138
139
140constexpr inline tConnectionFlags operator | (const tConnectionFlags& flags1, const tConnectionFlags& flags2)
141{
142  return tConnectionFlags(flags1.Raw() | flags2.Raw());
143}
144
145constexpr inline tConnectionFlags operator | (tConnectionFlag flag1, tConnectionFlag flag2)
146{
147  return tConnectionFlags(flag1) | tConnectionFlags(flag2);
148}
149
150constexpr inline tConnectionFlags operator & (const tConnectionFlags& flags1, const tConnectionFlags& flags2)
151{
152  return tConnectionFlags(flags1.Raw() & flags2.Raw());
153}
154
155constexpr inline bool operator == (const tConnectionFlags& flags1, const tConnectionFlags& flags2)
156{
157  return flags1.Raw() == flags2.Raw();
158}
159
160inline rrlib::serialization::tOutputStream& operator << (rrlib::serialization::tOutputStream& stream, const tConnectOptions& options)
161{
162  stream << options.conversion_operations << options.flags.Raw();
163  return stream;
164}
165
166inline rrlib::serialization::tInputStream& operator >> (rrlib::serialization::tInputStream& stream, tConnectOptions& options)
167{
168  stream >> options.conversion_operations;
169  options.flags = tConnectionFlags(stream.ReadInt());
170  return stream;
171}
172
173
174//----------------------------------------------------------------------
175// End of namespace declaration
176//----------------------------------------------------------------------
177}
178}
179
180
181#endif
Note: See TracBrowser for help on using the repository browser.