BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmf_integersequence.h
Go to the documentation of this file.
1/// @file bslmf_integersequence.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_integersequence.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_INTEGERSEQUENCE
9#define INCLUDED_BSLMF_INTEGERSEQUENCE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_integersequence bslmf_integersequence
15/// @brief Provide a template parameter pack of integers.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_integersequence
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_integersequence-purpose"> Purpose</a>
25/// * <a href="#bslmf_integersequence-classes"> Classes </a>
26/// * <a href="#bslmf_integersequence-description"> Description </a>
27/// * <a href="#bslmf_integersequence-usage"> Usage </a>
28/// * <a href="#bslmf_integersequence-example-1-pass-c-array-as-a-parameter-to-a-function-with-variadic-template"> Example 1: Pass C-array as a parameter to a function with variadic template </a>
29///
30/// # Purpose {#bslmf_integersequence-purpose}
31/// Provide a template parameter pack of integers.
32///
33/// # Classes {#bslmf_integersequence-classes}
34///
35/// - bslmf::IntegerSequence: a compile-time sequence of integers
36///
37/// # Description {#bslmf_integersequence-description}
38/// This component defines a class template,
39/// `bslmf::IntegerSequence`, which can be used in parameter pack expansions.
40///
41/// `bslmf::IntegerSequence` meets the requirements of the @ref integer_sequence
42/// template introduced in the C++14 standard [intseq.intseq] and can be used in
43/// a client's code if the compiler supports the C++11 standard.
44///
45/// Note, that this component has no (emulated) support for pre-standard C++11
46/// compilers, as its only purpose is to be used in deduction with template
47/// parameter packs. There is no language support to emulate in earlier
48/// compilers.
49///
50/// ## Usage {#bslmf_integersequence-usage}
51///
52///
53/// In this section we show intended use of this component.
54///
55/// ### Example 1: Pass C-array as a parameter to a function with variadic template {#bslmf_integersequence-example-1-pass-c-array-as-a-parameter-to-a-function-with-variadic-template}
56///
57///
58/// Suppose we want to initialize a C-Array of known size `t_N` with data read
59/// from a data source using a library class that provides a variadic template
60/// interface that loads a data of variable length into the supplied parameter
61/// pack.
62///
63/// First, define a class template `DataReader`,
64/// @code
65/// template <std::size_t t_N>
66/// class DataReader {
67/// public:
68/// @endcode
69/// Then, implement a method that loads the specified parameter pack `args` with
70/// data read from a data source.
71/// @code
72/// template <class ...t_T>
73/// void read(t_T*... args) const
74/// {
75/// static_assert(sizeof...(args) == t_N, "");
76/// read_impl(args...);
77/// }
78/// @endcode
79/// Next, for the test purpose provide simple implementation of the recursive
80/// variadic @ref read_impl function that streams the index of the C-Array's
81/// element to `stdout`.
82/// @code
83/// private:
84/// template <class t_U, class ...t_T>
85/// void read_impl(t_U*, t_T*... args) const
86/// {
87/// printf("read element #%i\n",
88/// static_cast<int>(t_N - 1 - sizeof...(args)));
89/// read_impl(args...);
90/// }
91/// @endcode
92/// Then, implement the recursion break condition.
93/// @code
94/// void read_impl() const
95/// {
96/// }
97/// };
98/// @endcode
99/// Next, define a helper function template `readData` that expands the
100/// parameter pack of indices `t_I` and invokes the variadic template `read`
101/// method of the specified `reader` object.
102/// @code
103/// namespace {
104/// template<class t_R, class t_T, std::size_t... t_I>
105/// void readData(const t_R& reader,
106/// t_T *data,
107/// bslmf::IntegerSequence<std::size_t, t_I...>)
108/// {
109/// reader.read(&data[t_I]...);
110/// // In pseudocode, this is equivalent to:
111/// // reader.read(&data[0],
112/// // &data[1],
113/// // &data[2],
114/// // ...
115/// // &data[t_N-1]);
116/// }
117/// }
118/// @endcode
119/// Finally, define a `data` C-Array and `reader` variables and pass them to the
120/// `readData` function as parameters.
121/// @code
122/// constexpr int k_SIZE = 5;
123/// DataReader<k_SIZE> reader;
124/// int data[k_SIZE] = {0};
125///
126/// readData(reader,
127/// data,
128/// bslmf::IntegerSequence<size_t, 0, 1, 2, 3, 4>());
129/// @endcode
130/// Note that using a direct call to the `bslmf::IntegerSequence` constructor
131/// looks a bit clumsy here. The better approach is to use alias template
132/// `bslmf::MakeIntegerSequence`, that creates a collection of increasing
133/// integer values, having the specified t_N-value length. The usage example in
134/// that component shows this method more clearly. But we can not afford its
135/// presence here to avoid a cycle/levelization violation.
136///
137/// The streaming operator produces output in the following format on `stdout`:
138/// @code
139/// read element #0
140/// read element #1
141/// read element #2
142/// read element #3
143/// read element #4
144/// @endcode
145/// @}
146/** @} */
147/** @} */
148
149/** @addtogroup bsl
150 * @{
151 */
152/** @addtogroup bslmf
153 * @{
154 */
155/** @addtogroup bslmf_integersequence
156 * @{
157 */
158
159#include <bslscm_version.h>
160
162
163#include <bsls_libraryfeatures.h>
164
165#include <cstddef> // 'std::size_t'
166
167#ifdef BSLS_LIBRARYFEATURES_HAS_CPP14_INTEGER_SEQUENCE
168
169
170namespace bslmf {
171 // ======================
172 // struct IntegerSequence
173 // ======================
174
175/// This class template represents a compile-time sequence of integers.
176/// When passed as an argument to a function template, the specified
177/// parameter pack `t_INTS` can be deduced and used a in pack expansion.
178template <class t_T, t_T ...t_INTS>
179struct IntegerSequence {
180
181 // TYPES
182
183 /// `value_type` is an alias to the template parameter `t_T` that
184 /// represents an integer type to use for the elements of the sequence.
185 typedef t_T value_type;
186
187 // CLASS METHODS
188
189 /// Return the number of elements in `t_INTS`.
190 static constexpr std::size_t size() noexcept;
191};
192
193} // close package namespace
194
195// ============================================================================
196// INLINE FUNCTION DEFINITIONS
197// ============================================================================
198
199 // ----------------------
200 // struct IntegerSequence
201 // ----------------------
202
203template <class t_T, t_T... t_INTS>
204inline
205constexpr std::size_t bslmf::IntegerSequence<t_T, t_INTS...>::size() noexcept
206{
207 return sizeof...(t_INTS);
208}
209
210
211
212#endif // BSLS_LIBRARYFEATURES_HAS_CPP14_INTEGER_SEQUENCE
213
214#endif // INCLUDED_BSLMF_INTEGERSEQUENCE
215
216// ----------------------------------------------------------------------------
217// Copyright 2018 Bloomberg Finance L.P.
218//
219// Licensed under the Apache License, Version 2.0 (the "License");
220// you may not use this file except in compliance with the License.
221// You may obtain a copy of the License at
222//
223// http://www.apache.org/licenses/LICENSE-2.0
224//
225// Unless required by applicable law or agreed to in writing, software
226// distributed under the License is distributed on an "AS IS" BASIS,
227// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
228// See the License for the specific language governing permissions and
229// limitations under the License.
230// ----------------------------- END-OF-FILE ----------------------------------
231
232/** @} */
233/** @} */
234/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
bsl::size_t size(const TYPE &array)
Return the number of elements in the specified array.
Definition bdlbb_blob.h:576
Definition bdldfp_decimal.h:5188