source: finroc_core/port/tConnectOptions.h @ 427:0a7e413412e9

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

Unifies and optimizes serialization of tConnectOptions and tUriConnectOptions (for uniform handling on Java side). Makes sure that auto-set flags erroneously set by user are ignored.

File size: 7.8 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 * First 16 Bits are sent to Finstruct
64 */
65enum class tConnectionFlag
66{
67  // constant flags that are always transferred to finstruct
68  FINSTRUCTED,           //!< Was this connector created using finstruct (or loaded from XML file)?
69  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)
70  OPTIONAL,              //!< Flags optional connections. They are handled and reported as non-critical if they cannot be established.
71  SCHEDULING_NEUTRAL,    //!< Flags connections that should not have any effect on scheduling order
72
73  // Connect direction
74  // If none is set, connection direction is determined automatically. This should be appropriate for almost any situation. However, another direction may be enforced)
75  // Both flags must not be set at the same time (typically causes an invalid_argument exception)
76  DIRECTION_TO_DESTINATION, //!< Specified port is destination port (port passed as argument to ConnectTo member function; or second port passed to static method)
77  DIRECTION_TO_SOURCE,      //!< Specified port is destination port (port passed as argument to ConnectTo member function; or second port passed to static method)
78
79  // internal flag
80  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)
81
82  // unused flags for future use
83  UNUSED_8, UNUSED_9, UNUSED_10, UNUSED_11, UNUSED_12, UNUSED_13, UNUSED_14,
84
85  // these flags are automatically set
86  CONVERSION,            //!< Connector has type conversion operations attached that must be applied on publishing values
87  NAMED_PARAMETERS,      //!< Indicates whether connector options include named parameters (relevant mainly for uniform serialization of ConnectOptions and UriConnectorOptions (only the latter supports named parameters))
88  PUBLISHED,             //!< Only relevant for URI connectors: Has been published
89  DISCONNECTED,          //!< Connector has been removed
90};
91
92/*!
93 * Set of framework element flags.
94 */
95typedef rrlib::util::tEnumBasedFlags<tConnectionFlag, uint32_t> tConnectionFlags;
96
97//----------------------------------------------------------------------
98// Class declaration
99//----------------------------------------------------------------------
100//! Connect Options
101/*!
102 * Bundles available options for connecting ports (bundled for convenience and locality of changes)
103 */
104struct tConnectOptions
105{
106  /*! Any explicit conversion operations to apply (implicit type conversions are applied automatically) */
107  rrlib::rtti::conversion::tConversionOperationSequence conversion_operations;
108
109  /*! Flags for connection */
110  tConnectionFlags flags;
111
112
113  tConnectOptions() : conversion_operations(), flags() {}
114
115  /*!
116   * Convenience constructor so that e.g. single flags can also be used in ConnectTo methods
117   *
118   * \param option1 Any valid option (valid parameter for Set() method)
119   */
120  template <typename TOption1>
121  tConnectOptions(const TOption1& option1) : tConnectOptions()
122  {
123    Set(option1);
124  }
125  /*!
126   * \param conversion_operations Any explicit conversion operations to apply (implicit type conversions are applied automatically)
127   * \param flags Flags for connection
128   */
129  tConnectOptions(const rrlib::rtti::conversion::tConversionOperationSequence& conversion_operations, const tConnectionFlags& flags) :
130    conversion_operations(conversion_operations),
131    flags(flags)
132  {}
133
134  /**
135   * Serialize URI connect options
136   *
137   * \param stream Stream to write to
138   * \param auto_set_flags Whether to auto-adjust flags
139   */
140  void Serialize(rrlib::serialization::tOutputStream& stream, bool auto_set_flags) const;
141
142  /**
143   * \param conversion_operations New conversion operations
144   */
145  void Set(const rrlib::rtti::conversion::tConversionOperationSequence& conversion_operations)
146  {
147    this->conversion_operations = conversion_operations;
148  }
149  /**
150   * \param conversion_operations New flags
151   */
152  void Set(const tConnectionFlags& flags)
153  {
154    this->flags = flags;
155  }
156
157  /*!
158   * Unsets all flags that are automatically set by Finroc core.
159   * (Used for protection against misuse/undefined behavior)
160   *
161   * \param flags Flags to clear from auto-flags
162   * \return Flags with all auto-flags unset
163   */
164  static tConnectionFlags UnsetAutoFlags(const tConnectionFlags& flags);
165};
166
167
168constexpr inline tConnectionFlags operator | (const tConnectionFlags& flags1, const tConnectionFlags& flags2)
169{
170  return tConnectionFlags(flags1.Raw() | flags2.Raw());
171}
172
173constexpr inline tConnectionFlags operator | (tConnectionFlag flag1, tConnectionFlag flag2)
174{
175  return tConnectionFlags(flag1) | tConnectionFlags(flag2);
176}
177
178constexpr inline tConnectionFlags operator & (const tConnectionFlags& flags1, const tConnectionFlags& flags2)
179{
180  return tConnectionFlags(flags1.Raw() & flags2.Raw());
181}
182
183constexpr inline bool operator == (const tConnectionFlags& flags1, const tConnectionFlags& flags2)
184{
185  return flags1.Raw() == flags2.Raw();
186}
187
188
189inline rrlib::serialization::tOutputStream& operator << (rrlib::serialization::tOutputStream& stream, const tConnectOptions& options)
190{
191  options.Serialize(stream, true);
192  return stream;
193}
194rrlib::serialization::tInputStream& operator >> (rrlib::serialization::tInputStream& stream, tConnectOptions& options);
195
196//----------------------------------------------------------------------
197// End of namespace declaration
198//----------------------------------------------------------------------
199}
200}
201
202
203#endif
Note: See TracBrowser for help on using the repository browser.