source: rrlib_util/tIntegerSequence.h @ 75:b9fae3e74063

Last change on this file since 75:b9fae3e74063 was 75:b9fae3e74063, checked in by Max Reichardt <mreichardt@…>, 7 years ago

Added tIntegerSequence utility class

File size: 3.7 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License
9// as published by the Free Software Foundation; either version 2
10// of the License, or (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
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/util/tIntegerSequence.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2012-12-08
27 *
28 * \brief   Contains tIntegerSequence
29 *
30 * \b tIntegerSequence
31 *
32 * Struct to store a parameter pack with a sequence of n integers,
33 * which can be pretty useful when dealing with variadic templates - see
34 * http://stackoverflow.com/questions/7858817/unpacking-a-tuple-to-call-a-matching-function-pointer
35 */
36//----------------------------------------------------------------------
37#ifndef __rrlib__util__tIntegerSequence_h__
38#define __rrlib__util__tIntegerSequence_h__
39
40//----------------------------------------------------------------------
41// External includes (system with <>, local with "")
42//----------------------------------------------------------------------
43
44//----------------------------------------------------------------------
45// Internal includes with ""
46//----------------------------------------------------------------------
47
48//----------------------------------------------------------------------
49// Namespace declaration
50//----------------------------------------------------------------------
51namespace rrlib
52{
53namespace util
54{
55
56//----------------------------------------------------------------------
57// Forward declarations / typedefs / enums
58//----------------------------------------------------------------------
59namespace internal
60{
61template <int ... SEQUENCE>
62struct tIntegerSequenceGeneratorHelper;
63}
64
65//----------------------------------------------------------------------
66// Class declaration
67//----------------------------------------------------------------------
68//! Integer sequence
69/*!
70 * Struct to store a parameter pack with a sequence of n integers,
71 * which can be pretty useful when dealing with variadic templates - see
72 * http://stackoverflow.com/questions/7858817/unpacking-a-tuple-to-call-a-matching-function-pointer
73 */
74template <int ... SEQUENCE>
75struct tIntegerSequence
76{};
77
78/*!
79 * Class to build parameter pack of n integers:
80 * 0, 1, 2 ... (n-1)
81 *
82 * \tparam SIZE Size n of parameter pack
83 */
84template <int SIZE>
85struct tIntegerSequenceGenerator
86{
87  /*! typedef on tIntegerSequence<0, 1, ... (SIZE-1)> */
88  typedef typename internal::tIntegerSequenceGeneratorHelper<SIZE>::type type;
89};
90
91
92
93
94// Internal helper struct
95namespace internal
96{
97template <int ... SEQUENCE>
98struct tIntegerSequenceGeneratorHelper;
99
100template <int SIZE, int ... SEQUENCE>
101struct tIntegerSequenceGeneratorHelper<SIZE, SEQUENCE...> : tIntegerSequenceGeneratorHelper < SIZE - 1, SIZE - 1, SEQUENCE... >
102{
103};
104
105template <int ... SEQUENCE>
106struct tIntegerSequenceGeneratorHelper<0, SEQUENCE...>
107{
108  typedef tIntegerSequence<SEQUENCE...> type;
109};
110}
111
112//----------------------------------------------------------------------
113// End of namespace declaration
114//----------------------------------------------------------------------
115}
116}
117
118
119#endif
Note: See TracBrowser for help on using the repository browser.