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/rpc_ports/tProxyPort.h |
---|
23 | * |
---|
24 | * \author Max Reichardt |
---|
25 | * |
---|
26 | * \date 2012-12-31 |
---|
27 | * |
---|
28 | * \brief Contains tProxyPort |
---|
29 | * |
---|
30 | * \b tProxyPort |
---|
31 | * |
---|
32 | * Proxy (or "routing") RPC port. |
---|
33 | * |
---|
34 | */ |
---|
35 | //---------------------------------------------------------------------- |
---|
36 | #ifndef __plugins__rpc_ports__tProxyPort_h__ |
---|
37 | #define __plugins__rpc_ports__tProxyPort_h__ |
---|
38 | |
---|
39 | //---------------------------------------------------------------------- |
---|
40 | // External includes (system with <>, local with "") |
---|
41 | //---------------------------------------------------------------------- |
---|
42 | #include "core/port/tPortWrapperBase.h" |
---|
43 | |
---|
44 | //---------------------------------------------------------------------- |
---|
45 | // Internal includes with "" |
---|
46 | //---------------------------------------------------------------------- |
---|
47 | #include "plugins/rpc_ports/tRPCInterfaceType.h" |
---|
48 | #include "plugins/rpc_ports/internal/tRPCPort.h" |
---|
49 | |
---|
50 | //---------------------------------------------------------------------- |
---|
51 | // Namespace declaration |
---|
52 | //---------------------------------------------------------------------- |
---|
53 | namespace finroc |
---|
54 | { |
---|
55 | namespace rpc_ports |
---|
56 | { |
---|
57 | |
---|
58 | //---------------------------------------------------------------------- |
---|
59 | // Forward declarations / typedefs / enums |
---|
60 | //---------------------------------------------------------------------- |
---|
61 | |
---|
62 | //---------------------------------------------------------------------- |
---|
63 | // Class declaration |
---|
64 | //---------------------------------------------------------------------- |
---|
65 | //! Proxy RPC port |
---|
66 | /*! |
---|
67 | * Proxy (or "routing") RPC port (similar to tProxyPort in data_ports plugin) |
---|
68 | */ |
---|
69 | template <typename T, bool SERVER_PORT> |
---|
70 | class tProxyPort : public core::tPortWrapperBase |
---|
71 | { |
---|
72 | |
---|
73 | //---------------------------------------------------------------------- |
---|
74 | // Public methods and typedefs |
---|
75 | //---------------------------------------------------------------------- |
---|
76 | public: |
---|
77 | |
---|
78 | /*! Creates no wrapped port */ |
---|
79 | tProxyPort() {} |
---|
80 | |
---|
81 | /*! |
---|
82 | * Constructor takes variadic argument list... just any properties you want to assign to port. |
---|
83 | * |
---|
84 | * The first string is interpreted as port name, the second possibly as config entry (relevant for parameters only). |
---|
85 | * A framework element pointer is interpreted as parent. |
---|
86 | * tFrameworkElementFlag arguments are interpreted as flags. |
---|
87 | * tAbstractPortCreationInfo argument is copied. This is only allowed as first argument. |
---|
88 | */ |
---|
89 | template <typename TArg1, typename TArg2, typename ... TRest> |
---|
90 | tProxyPort(const TArg1& arg1, const TArg2& arg2, const TRest&... args) |
---|
91 | { |
---|
92 | tConstructorArguments<core::tAbstractPortCreationInfo> creation_info(arg1, arg2, args...); |
---|
93 | creation_info.data_type = tRPCInterfaceType<T>(); |
---|
94 | creation_info.flags |= core::tFrameworkElement::tFlag::ACCEPTS_DATA | core::tFrameworkElement::tFlag::EMITS_DATA; |
---|
95 | if (!SERVER_PORT) |
---|
96 | { |
---|
97 | creation_info.flags |= core::tFrameworkElement::tFlag::OUTPUT_PORT; |
---|
98 | } |
---|
99 | if (!creation_info.flags.Get(core::tFrameworkElementFlag::DELETED)) // do not create port, if deleted flag is set |
---|
100 | { |
---|
101 | this->SetWrapped(new internal::tRPCPort(creation_info, nullptr)); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | // with a single argument, do not catch calls for copy construction |
---|
106 | template < typename TArgument, typename = typename std::enable_if < !std::is_base_of<core::tPortWrapperBase, typename std::decay<TArgument>::type>::value, int >::type > |
---|
107 | explicit tProxyPort(TArgument && argument) : |
---|
108 | tProxyPort(std::forward<TArgument>(argument), tFlags()) |
---|
109 | {} |
---|
110 | |
---|
111 | /*! |
---|
112 | * Wraps raw port |
---|
113 | * Throws std::runtime_error if port to wrap has invalid type or flags. |
---|
114 | * |
---|
115 | * \param wrap Type-less port to wrap as tPort<T> |
---|
116 | */ |
---|
117 | static tProxyPort Wrap(core::tAbstractPort& wrap) |
---|
118 | { |
---|
119 | if (wrap.GetDataType().GetRttiName() != typeid(T).name()) |
---|
120 | { |
---|
121 | throw std::runtime_error("Port to wrap has invalid type"); |
---|
122 | } |
---|
123 | if ((!wrap.GetFlag(core::tFrameworkElement::tFlag::ACCEPTS_DATA)) || (!wrap.GetFlag(core::tFrameworkElement::tFlag::EMITS_DATA))) |
---|
124 | { |
---|
125 | throw std::runtime_error("Port to wrap has invalid flags"); |
---|
126 | } |
---|
127 | tProxyPort port; |
---|
128 | port.SetWrapped(&wrap); |
---|
129 | return port; |
---|
130 | } |
---|
131 | |
---|
132 | //---------------------------------------------------------------------- |
---|
133 | // Private fields and methods |
---|
134 | //---------------------------------------------------------------------- |
---|
135 | private: |
---|
136 | |
---|
137 | }; |
---|
138 | |
---|
139 | //---------------------------------------------------------------------- |
---|
140 | // End of namespace declaration |
---|
141 | //---------------------------------------------------------------------- |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | #endif |
---|