source: finroc_plugins_data_ports/tPortPack.h @ 124:ca459cc011c8

Last change on this file since 124:ca459cc011c8 was 124:ca459cc011c8, checked in by Tobias Föhst <foehst@…>, 2 years ago

Merge with 14.08

File size: 5.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    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//----------------------------------------------------------------------
52namespace finroc
53{
54namespace 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 */
75template <template <typename> class TPort, typename TTypeList, size_t Tsize = rrlib::util::type_list::tSizeOf<TTypeList>::cVALUE>
76class 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//----------------------------------------------------------------------
85public:
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 void ManagedDelete()
126  {
127    this->port.GetWrapped()->ManagedDelete();
128    tPortPack < TPort, TTypeList, Tsize - 1 >::ManagedDelete();
129  }
130
131//----------------------------------------------------------------------
132// Private fields and methods
133//----------------------------------------------------------------------
134
135private:
136
137  TPort < typename TTypeList::template tAt < Tsize - 1 >::tResult > port;
138
139};
140
141//! The partial specialization of tPortPack to terminate recursion
142template <template <typename> class TPort, typename TTypeList>
143struct tPortPack <TPort, TTypeList, 0>
144{
145  inline tPortPack(core::tFrameworkElement *parent, const std::string &name_prefix)
146  {}
147
148  template <typename TIterator>
149  inline tPortPack(core::tFrameworkElement *parent, TIterator names_begin, TIterator names_end)
150  {}
151
152  inline core::tPortWrapperBase &GetPort(size_t index)
153  {
154    return *reinterpret_cast<core::tPortWrapperBase *>(0);
155  };
156  inline void ManagedDelete()
157  {}
158};
159
160//----------------------------------------------------------------------
161// End of namespace declaration
162//----------------------------------------------------------------------
163}
164}
165
166#endif
Note: See TracBrowser for help on using the repository browser.