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/data_ports/tPortPack.h |
---|
23 | * |
---|
24 | * \author Tobias Föhst |
---|
25 | * |
---|
26 | * \date 2012-10-18 |
---|
27 | * |
---|
28 | * \brief Contains tPortPack |
---|
29 | * |
---|
30 | * \b tPortPack |
---|
31 | * |
---|
32 | * A group of several ports with different types. |
---|
33 | * |
---|
34 | */ |
---|
35 | //---------------------------------------------------------------------- |
---|
36 | #ifndef __plugins__data_ports__tPortPack_h__ |
---|
37 | #define __plugins__data_ports__tPortPack_h__ |
---|
38 | |
---|
39 | //---------------------------------------------------------------------- |
---|
40 | // External includes (system with <>, local with "") |
---|
41 | //---------------------------------------------------------------------- |
---|
42 | #include "rrlib/util/tTypeList.h" |
---|
43 | #include "core/tFrameworkElement.h" |
---|
44 | |
---|
45 | //---------------------------------------------------------------------- |
---|
46 | // Internal includes with "" |
---|
47 | //---------------------------------------------------------------------- |
---|
48 | |
---|
49 | //---------------------------------------------------------------------- |
---|
50 | // Namespace declaration |
---|
51 | //---------------------------------------------------------------------- |
---|
52 | namespace finroc |
---|
53 | { |
---|
54 | namespace data_ports |
---|
55 | { |
---|
56 | |
---|
57 | //---------------------------------------------------------------------- |
---|
58 | // Forward declarations / typedefs / enums |
---|
59 | //---------------------------------------------------------------------- |
---|
60 | |
---|
61 | //---------------------------------------------------------------------- |
---|
62 | // Class declaration |
---|
63 | //---------------------------------------------------------------------- |
---|
64 | //! A group of several ports with different types. |
---|
65 | /*! |
---|
66 | * This class creates a list of instances of the given port template |
---|
67 | * inserting several types from a list. It therefore creates a nested |
---|
68 | * structure of inherited classes and provides a method to access the |
---|
69 | * port on a specific layer at runtime. |
---|
70 | * |
---|
71 | * \param TPort A port class template to use for every packed port |
---|
72 | * \param TTypeList A list of the data types used in the ports. e.g. rrlib::util::tTypeList |
---|
73 | * \param Tsize The pack creates ports using TTypeList[0] to TTypeList[Tsize - 1]. This parameter must not be greater than TTypeList::cSIZE - 1 and is typically inferred and not set by the user. |
---|
74 | */ |
---|
75 | template <template <typename> class TPort, typename TTypeList, size_t Tsize = rrlib::util::type_list::tSizeOf<TTypeList>::cVALUE> |
---|
76 | class tPortPack : private tPortPack < TPort, TTypeList, Tsize - 1 > |
---|
77 | { |
---|
78 | |
---|
79 | template <bool value, typename> |
---|
80 | using CheckIterators = std::integral_constant<bool, value>; |
---|
81 | |
---|
82 | //---------------------------------------------------------------------- |
---|
83 | // Public methods and typedefs |
---|
84 | //---------------------------------------------------------------------- |
---|
85 | public: |
---|
86 | |
---|
87 | inline tPortPack(core::tFrameworkElement *parent, const std::string &name_prefix) : |
---|
88 | tPortPack < TPort, TTypeList, Tsize - 1 > (parent, name_prefix), |
---|
89 | port(name_prefix + std::to_string(Tsize), parent) |
---|
90 | { |
---|
91 | this->port.Init(); |
---|
92 | } |
---|
93 | |
---|
94 | template <typename TIterator, typename std::enable_if<CheckIterators<(Tsize> 1), TIterator>::value, int>::type = 0 > |
---|
95 | inline tPortPack(core::tFrameworkElement *parent, TIterator names_begin, TIterator names_end) : |
---|
96 | tPortPack < TPort, TTypeList, Tsize - 1 > (parent, names_begin, names_end - 1), |
---|
97 | port(*(names_end - 1), parent) |
---|
98 | { |
---|
99 | this->port.Init(); |
---|
100 | } |
---|
101 | |
---|
102 | template <typename TIterator, typename std::enable_if<CheckIterators<(Tsize == 1), TIterator>::value, int>::type = 0> |
---|
103 | inline tPortPack(core::tFrameworkElement *parent, TIterator names_begin, TIterator names_end) : |
---|
104 | tPortPack < TPort, TTypeList, Tsize - 1 > (parent, *names_begin), |
---|
105 | port(*names_begin, parent) |
---|
106 | { |
---|
107 | this->port.Init(); |
---|
108 | } |
---|
109 | |
---|
110 | inline size_t NumberOfPorts() const |
---|
111 | { |
---|
112 | return Tsize; |
---|
113 | } |
---|
114 | |
---|
115 | inline core::tPortWrapperBase &GetPort(size_t index) |
---|
116 | { |
---|
117 | assert(index < this->NumberOfPorts()); |
---|
118 | if (index == Tsize - 1) |
---|
119 | { |
---|
120 | return this->port; |
---|
121 | } |
---|
122 | return tPortPack < TPort, TTypeList, Tsize - 1 >::GetPort(index); |
---|
123 | } |
---|
124 | |
---|
125 | inline bool HasChanged(size_t index) |
---|
126 | { |
---|
127 | assert(index < this->NumberOfPorts()); |
---|
128 | if (index == Tsize - 1) |
---|
129 | { |
---|
130 | return this->port.HasChanged(); |
---|
131 | } |
---|
132 | return tPortPack < TPort, TTypeList, Tsize - 1 >::HasChanged(index); |
---|
133 | } |
---|
134 | |
---|
135 | inline void ManagedDelete() |
---|
136 | { |
---|
137 | this->port.GetWrapped()->ManagedDelete(); |
---|
138 | tPortPack < TPort, TTypeList, Tsize - 1 >::ManagedDelete(); |
---|
139 | } |
---|
140 | |
---|
141 | //---------------------------------------------------------------------- |
---|
142 | // Private fields and methods |
---|
143 | //---------------------------------------------------------------------- |
---|
144 | |
---|
145 | private: |
---|
146 | |
---|
147 | TPort < typename TTypeList::template tAt < Tsize - 1 >::tResult > port; |
---|
148 | |
---|
149 | }; |
---|
150 | |
---|
151 | //! The partial specialization of tPortPack to terminate recursion |
---|
152 | template <template <typename> class TPort, typename TTypeList> |
---|
153 | struct tPortPack <TPort, TTypeList, 0> |
---|
154 | { |
---|
155 | inline tPortPack(core::tFrameworkElement *parent, const std::string &name_prefix) |
---|
156 | {} |
---|
157 | |
---|
158 | template <typename TIterator> |
---|
159 | inline tPortPack(core::tFrameworkElement *parent, TIterator names_begin, TIterator names_end) |
---|
160 | {} |
---|
161 | |
---|
162 | inline core::tPortWrapperBase &GetPort(size_t index) |
---|
163 | { |
---|
164 | return *reinterpret_cast<core::tPortWrapperBase *>(0); |
---|
165 | }; |
---|
166 | |
---|
167 | inline bool HasChanged(size_t index) |
---|
168 | { |
---|
169 | return false; |
---|
170 | }; |
---|
171 | |
---|
172 | inline void ManagedDelete() |
---|
173 | {} |
---|
174 | }; |
---|
175 | |
---|
176 | //---------------------------------------------------------------------- |
---|
177 | // End of namespace declaration |
---|
178 | //---------------------------------------------------------------------- |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | #endif |
---|