source: finroc_core/port/tConnector.cpp @ 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: 5.1 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/tConnector.cpp
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2015-12-15
27 *
28 */
29//----------------------------------------------------------------------
30#include "core/port/tConnector.h"
31
32//----------------------------------------------------------------------
33// External includes (system with <>, local with "")
34//----------------------------------------------------------------------
35
36//----------------------------------------------------------------------
37// Internal includes with ""
38//----------------------------------------------------------------------
39#include "core/tRuntimeEnvironment.h"
40#include "core/port/tAbstractPort.h"
41
42//----------------------------------------------------------------------
43// Debugging
44//----------------------------------------------------------------------
45#include <cassert>
46
47//----------------------------------------------------------------------
48// Namespace usage
49//----------------------------------------------------------------------
50
51//----------------------------------------------------------------------
52// Namespace declaration
53//----------------------------------------------------------------------
54namespace finroc
55{
56namespace core
57{
58
59//----------------------------------------------------------------------
60// Forward declarations / typedefs / enums
61//----------------------------------------------------------------------
62
63//----------------------------------------------------------------------
64// Const values
65//----------------------------------------------------------------------
66
67//----------------------------------------------------------------------
68// Implementation
69//----------------------------------------------------------------------
70
71namespace internal
72{
73
74//! Annotation to link tConnector and tUriConnector classes
75/*!
76 * Annotation for tConnector objects to attach tUriConnector objects.
77 * They are notified if tConnector is removed.
78 *
79 * This class was introduced as it saves memory not to have an additional pointer
80 * in every tConnector that is rarely used.
81 */
82class tUriConnectorOwnerAnnotation : public tAnnotation
83{
84
85//----------------------------------------------------------------------
86// Public methods and typedefs
87//----------------------------------------------------------------------
88public:
89
90  /*!
91   * \param connector Reference to tUriConnector
92   */
93  tUriConnectorOwnerAnnotation(tUriConnector& connector) :
94    connector(connector)
95  {}
96
97  /*!
98   * \return Reference to tUriConnector
99   */
100  tUriConnector& Get()
101  {
102    return connector;
103  }
104
105//----------------------------------------------------------------------
106// Private fields and methods
107//----------------------------------------------------------------------
108private:
109
110  /*! Reference to tUriConnector */
111  tUriConnector& connector;
112
113};
114}
115
116tConnector::tConnector(tAbstractPort& source_port, tAbstractPort& destination_port, const tConnectOptions& connect_options, const rrlib::rtti::conversion::tConversionOperationSequence& conversion_sequence_storage) :
117  source_port(source_port),
118  destination_port(destination_port),
119  flags(tConnectOptions::UnsetAutoFlags(connect_options.flags) | (&conversion_sequence_storage != &rrlib::rtti::conversion::tConversionOperationSequence::cNONE ? tFlags(tFlag::CONVERSION) : tFlags()))
120{
121  if (flags.Get(tFlag::CONVERSION))
122  {
123    assert(&conversion_sequence_storage == &ConversionOperations() && "Conversion operations must be first member in subclass");
124  }
125}
126
127tConnector::~tConnector()
128{
129}
130
131void tConnector::Disconnect()
132{
133  rrlib::thread::tLock lock(tRuntimeEnvironment::GetInstance().GetStructureMutex());
134  tAbstractPort::DisconnectImplementation(*this, true);
135}
136
137void tConnector::NotifyOnDisconnect(tUriConnector& owner)
138{
139  this->EmplaceAnnotation<internal::tUriConnectorOwnerAnnotation>(owner);
140}
141
142void tConnector::OnDisconnect(bool stop_any_reconnecting)
143{
144  flags.Set(tConnectionFlag::DISCONNECTED, true);
145
146  internal::tUriConnectorOwnerAnnotation* annotation = this->GetAnnotation<internal::tUriConnectorOwnerAnnotation>();
147  if (annotation)
148  {
149    annotation->Get().OnConnectorDisconnect(stop_any_reconnecting);
150  }
151}
152
153
154//----------------------------------------------------------------------
155// End of namespace declaration
156//----------------------------------------------------------------------
157}
158}
Note: See TracBrowser for help on using the repository browser.