~ubuntu-branches/ubuntu/wily/tora/wily-proposed

« back to all changes in this revision

Viewing changes to ext/loki/loki-0.1.4/include/loki/Typelist.h

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2007-10-21 21:17:23 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071021211723-pq817gwdkt8hwetj
Tags: 1.3.22-1ubuntu1
* Merge from Debian unstable (LP: #149343). Remaining Ubuntu changes:
  - debian/rules: call dh_icons
  - Remove g++ build dependency
  - Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
////////////////////////////////////////////////////////////////////////////////
2
 
// The Loki Library
3
 
// Copyright (c) 2001 by Andrei Alexandrescu
4
 
// This code accompanies the book:
5
 
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design 
6
 
//     Patterns Applied". Copyright (c) 2001. Addison-Wesley.
7
 
// Permission to use, copy, modify, distribute and sell this software for any 
8
 
//     purpose is hereby granted without fee, provided that the above copyright 
9
 
//     notice appear in all copies and that both that copyright notice and this 
10
 
//     permission notice appear in supporting documentation.
11
 
// The author or Addison-Welsey Longman make no representations about the 
12
 
//     suitability of this software for any purpose. It is provided "as is" 
13
 
//     without express or implied warranty.
14
 
////////////////////////////////////////////////////////////////////////////////
15
 
 
16
 
// Last update: October 10, 2002
17
 
//Reference
18
 
 
19
 
// $Header: /cvsroot/loki-lib/loki/include/loki/Typelist.h,v 1.5 2006/01/16 19:05:09 rich_sposato Exp $
20
 
 
21
 
#ifndef LOKI_TYPELIST_INC_
22
 
#define LOKI_TYPELIST_INC_
23
 
 
24
 
#include "NullType.h"
25
 
#include "TypeManip.h"
26
 
#include "TypelistMacros.h"
27
 
 
28
 
 
29
 
namespace Loki
30
 
{
31
 
////////////////////////////////////////////////////////////////////////////////
32
 
// class template Typelist
33
 
// The building block of typelists of any length
34
 
// Use it through the LOKI_TYPELIST_NN macros
35
 
// Defines nested types:
36
 
//     Head (first element, a non-typelist type by convention)
37
 
//     Tail (second element, can be another typelist)
38
 
////////////////////////////////////////////////////////////////////////////////
39
 
 
40
 
    template <class T, class U>
41
 
    struct Typelist
42
 
    {
43
 
       typedef T Head;
44
 
       typedef U Tail;
45
 
    };
46
 
 
47
 
// Typelist utility algorithms
48
 
 
49
 
    namespace TL
50
 
    {
51
 
 
52
 
////////////////////////////////////////////////////////////////////////////////
53
 
// class template MakeTypelist
54
 
// Takes a number of arguments equal to its numeric suffix
55
 
// The arguments are type names.
56
 
// MakeTypelist<T1, T2, ...>::Result
57
 
// returns a typelist that is of T1, T2, ...
58
 
////////////////////////////////////////////////////////////////////////////////
59
 
 
60
 
        template
61
 
        <
62
 
            typename T1  = NullType, typename T2  = NullType, typename T3  = NullType,
63
 
            typename T4  = NullType, typename T5  = NullType, typename T6  = NullType,
64
 
            typename T7  = NullType, typename T8  = NullType, typename T9  = NullType,
65
 
            typename T10 = NullType, typename T11 = NullType, typename T12 = NullType,
66
 
            typename T13 = NullType, typename T14 = NullType, typename T15 = NullType,
67
 
            typename T16 = NullType, typename T17 = NullType, typename T18 = NullType
68
 
        > 
69
 
        struct MakeTypelist
70
 
        {
71
 
        private:
72
 
            typedef typename MakeTypelist
73
 
            <
74
 
                T2 , T3 , T4 , 
75
 
                T5 , T6 , T7 , 
76
 
                T8 , T9 , T10, 
77
 
                T11, T12, T13,
78
 
                T14, T15, T16, 
79
 
                T17, T18
80
 
            >
81
 
            ::Result TailResult;
82
 
 
83
 
        public:
84
 
            typedef Typelist<T1, TailResult> Result;
85
 
        };
86
 
 
87
 
        template<>
88
 
        struct MakeTypelist<>
89
 
        {
90
 
            typedef NullType Result;
91
 
        };
92
 
 
93
 
////////////////////////////////////////////////////////////////////////////////
94
 
// class template Length
95
 
// Computes the length of a typelist
96
 
// Invocation (TList is a typelist):
97
 
// Length<TList>::value
98
 
// returns a compile-time constant containing the length of TList, not counting
99
 
//     the end terminator (which by convention is NullType)
100
 
////////////////////////////////////////////////////////////////////////////////
101
 
 
102
 
        template <class TList> struct Length;
103
 
        template <> struct Length<NullType>
104
 
        {
105
 
            enum { value = 0 };
106
 
        };
107
 
        
108
 
        template <class T, class U>
109
 
        struct Length< Typelist<T, U> >
110
 
        {
111
 
            enum { value = 1 + Length<U>::value };
112
 
        };
113
 
 
114
 
////////////////////////////////////////////////////////////////////////////////
115
 
// class template TypeAt
116
 
// Finds the type at a given index in a typelist
117
 
// Invocation (TList is a typelist and index is a compile-time integral 
118
 
//     constant):
119
 
// TypeAt<TList, index>::Result
120
 
// returns the type in position 'index' in TList
121
 
// If you pass an out-of-bounds index, the result is a compile-time error
122
 
////////////////////////////////////////////////////////////////////////////////
123
 
 
124
 
        template <class TList, unsigned int index> struct TypeAt;
125
 
        
126
 
        template <class Head, class Tail>
127
 
        struct TypeAt<Typelist<Head, Tail>, 0>
128
 
        {
129
 
            typedef Head Result;
130
 
        };
131
 
 
132
 
        template <class Head, class Tail, unsigned int i>
133
 
        struct TypeAt<Typelist<Head, Tail>, i>
134
 
        {
135
 
            typedef typename TypeAt<Tail, i - 1>::Result Result;
136
 
        };
137
 
 
138
 
////////////////////////////////////////////////////////////////////////////////
139
 
// class template TypeAtNonStrict
140
 
// Finds the type at a given index in a typelist
141
 
// Invocations (TList is a typelist and index is a compile-time integral 
142
 
//     constant):
143
 
// a) TypeAt<TList, index>::Result
144
 
// returns the type in position 'index' in TList, or NullType if index is 
145
 
//     out-of-bounds
146
 
// b) TypeAt<TList, index, D>::Result
147
 
// returns the type in position 'index' in TList, or D if index is out-of-bounds
148
 
////////////////////////////////////////////////////////////////////////////////
149
 
 
150
 
        template <class TList, unsigned int index,
151
 
            typename DefaultType = NullType>
152
 
        struct TypeAtNonStrict
153
 
        {
154
 
            typedef DefaultType Result;
155
 
        };
156
 
        
157
 
        template <class Head, class Tail, typename DefaultType>
158
 
        struct TypeAtNonStrict<Typelist<Head, Tail>, 0, DefaultType>
159
 
        {
160
 
            typedef Head Result;
161
 
        };
162
 
        
163
 
        template <class Head, class Tail, unsigned int i, typename DefaultType>
164
 
        struct TypeAtNonStrict<Typelist<Head, Tail>, i, DefaultType>
165
 
        {
166
 
            typedef typename 
167
 
                TypeAtNonStrict<Tail, i - 1, DefaultType>::Result Result;
168
 
        };
169
 
 
170
 
////////////////////////////////////////////////////////////////////////////////
171
 
// class template IndexOf
172
 
// Finds the index of a type in a typelist
173
 
// Invocation (TList is a typelist and T is a type):
174
 
// IndexOf<TList, T>::value
175
 
// returns the position of T in TList, or NullType if T is not found in TList
176
 
////////////////////////////////////////////////////////////////////////////////
177
 
 
178
 
        template <class TList, class T> struct IndexOf;
179
 
        
180
 
        template <class T>
181
 
        struct IndexOf<NullType, T>
182
 
        {
183
 
            enum { value = -1 };
184
 
        };
185
 
        
186
 
        template <class T, class Tail>
187
 
        struct IndexOf<Typelist<T, Tail>, T>
188
 
        {
189
 
            enum { value = 0 };
190
 
        };
191
 
        
192
 
        template <class Head, class Tail, class T>
193
 
        struct IndexOf<Typelist<Head, Tail>, T>
194
 
        {
195
 
        private:
196
 
            enum { temp = IndexOf<Tail, T>::value };
197
 
        public:
198
 
            enum { value = (temp == -1 ? -1 : 1 + temp) };
199
 
        };
200
 
 
201
 
////////////////////////////////////////////////////////////////////////////////
202
 
// class template Append
203
 
// Appends a type or a typelist to another
204
 
// Invocation (TList is a typelist and T is either a type or a typelist):
205
 
// Append<TList, T>::Result
206
 
// returns a typelist that is TList followed by T and NullType-terminated
207
 
////////////////////////////////////////////////////////////////////////////////
208
 
 
209
 
        template <class TList, class T> struct Append;
210
 
        
211
 
        template <> struct Append<NullType, NullType>
212
 
        {
213
 
            typedef NullType Result;
214
 
        };
215
 
        
216
 
        template <class T> struct Append<NullType, T>
217
 
        {
218
 
            typedef Typelist<T,NullType> Result;
219
 
        };
220
 
 
221
 
        template <class Head, class Tail>
222
 
        struct Append<NullType, Typelist<Head, Tail> >
223
 
        {
224
 
            typedef Typelist<Head, Tail> Result;
225
 
        };
226
 
        
227
 
        template <class Head, class Tail, class T>
228
 
        struct Append<Typelist<Head, Tail>, T>
229
 
        {
230
 
            typedef Typelist<Head, 
231
 
                    typename Append<Tail, T>::Result>
232
 
                Result;
233
 
        };
234
 
        
235
 
////////////////////////////////////////////////////////////////////////////////
236
 
// class template Erase
237
 
// Erases the first occurence, if any, of a type in a typelist
238
 
// Invocation (TList is a typelist and T is a type):
239
 
// Erase<TList, T>::Result
240
 
// returns a typelist that is TList without the first occurence of T
241
 
////////////////////////////////////////////////////////////////////////////////
242
 
 
243
 
        template <class TList, class T> struct Erase;
244
 
        
245
 
        template <class T>                         // Specialization 1
246
 
        struct Erase<NullType, T>
247
 
        {
248
 
            typedef NullType Result;
249
 
        };
250
 
 
251
 
        template <class T, class Tail>             // Specialization 2
252
 
        struct Erase<Typelist<T, Tail>, T>
253
 
        {
254
 
            typedef Tail Result;
255
 
        };
256
 
 
257
 
        template <class Head, class Tail, class T> // Specialization 3
258
 
        struct Erase<Typelist<Head, Tail>, T>
259
 
        {
260
 
            typedef Typelist<Head, 
261
 
                    typename Erase<Tail, T>::Result>
262
 
                Result;
263
 
        };
264
 
 
265
 
////////////////////////////////////////////////////////////////////////////////
266
 
// class template EraseAll
267
 
// Erases all first occurences, if any, of a type in a typelist
268
 
// Invocation (TList is a typelist and T is a type):
269
 
// EraseAll<TList, T>::Result
270
 
// returns a typelist that is TList without any occurence of T
271
 
////////////////////////////////////////////////////////////////////////////////
272
 
 
273
 
        template <class TList, class T> struct EraseAll;
274
 
        template <class T>
275
 
        struct EraseAll<NullType, T>
276
 
        {
277
 
            typedef NullType Result;
278
 
        };
279
 
        template <class T, class Tail>
280
 
        struct EraseAll<Typelist<T, Tail>, T>
281
 
        {
282
 
            // Go all the way down the list removing the type
283
 
            typedef typename EraseAll<Tail, T>::Result Result;
284
 
        };
285
 
        template <class Head, class Tail, class T>
286
 
        struct EraseAll<Typelist<Head, Tail>, T>
287
 
        {
288
 
            // Go all the way down the list removing the type
289
 
            typedef Typelist<Head, 
290
 
                    typename EraseAll<Tail, T>::Result>
291
 
                Result;
292
 
        };
293
 
 
294
 
////////////////////////////////////////////////////////////////////////////////
295
 
// class template NoDuplicates
296
 
// Removes all duplicate types in a typelist
297
 
// Invocation (TList is a typelist):
298
 
// NoDuplicates<TList, T>::Result
299
 
////////////////////////////////////////////////////////////////////////////////
300
 
 
301
 
        template <class TList> struct NoDuplicates;
302
 
        
303
 
        template <> struct NoDuplicates<NullType>
304
 
        {
305
 
            typedef NullType Result;
306
 
        };
307
 
 
308
 
        template <class Head, class Tail>
309
 
        struct NoDuplicates< Typelist<Head, Tail> >
310
 
        {
311
 
        private:
312
 
            typedef typename NoDuplicates<Tail>::Result L1;
313
 
            typedef typename Erase<L1, Head>::Result L2;
314
 
        public:
315
 
            typedef Typelist<Head, L2> Result;
316
 
        };
317
 
 
318
 
////////////////////////////////////////////////////////////////////////////////
319
 
// class template Replace
320
 
// Replaces the first occurence of a type in a typelist, with another type
321
 
// Invocation (TList is a typelist, T, U are types):
322
 
// Replace<TList, T, U>::Result
323
 
// returns a typelist in which the first occurence of T is replaced with U
324
 
////////////////////////////////////////////////////////////////////////////////
325
 
 
326
 
        template <class TList, class T, class U> struct Replace;
327
 
        
328
 
        template <class T, class U>
329
 
        struct Replace<NullType, T, U>
330
 
        {
331
 
            typedef NullType Result;
332
 
        };
333
 
 
334
 
        template <class T, class Tail, class U>
335
 
        struct Replace<Typelist<T, Tail>, T, U>
336
 
        {
337
 
            typedef Typelist<U, Tail> Result;
338
 
        };
339
 
 
340
 
        template <class Head, class Tail, class T, class U>
341
 
        struct Replace<Typelist<Head, Tail>, T, U>
342
 
        {
343
 
            typedef Typelist<Head,
344
 
                    typename Replace<Tail, T, U>::Result>
345
 
                Result;
346
 
        };
347
 
 
348
 
////////////////////////////////////////////////////////////////////////////////
349
 
// class template ReplaceAll
350
 
// Replaces all occurences of a type in a typelist, with another type
351
 
// Invocation (TList is a typelist, T, U are types):
352
 
// Replace<TList, T, U>::Result
353
 
// returns a typelist in which all occurences of T is replaced with U
354
 
////////////////////////////////////////////////////////////////////////////////
355
 
 
356
 
        template <class TList, class T, class U> struct ReplaceAll;
357
 
        
358
 
        template <class T, class U>
359
 
        struct ReplaceAll<NullType, T, U>
360
 
        {
361
 
            typedef NullType Result;
362
 
        };
363
 
        
364
 
        template <class T, class Tail, class U>
365
 
        struct ReplaceAll<Typelist<T, Tail>, T, U>
366
 
        {
367
 
            typedef Typelist<U, typename ReplaceAll<Tail, T, U>::Result> Result;
368
 
        };
369
 
        
370
 
        template <class Head, class Tail, class T, class U>
371
 
        struct ReplaceAll<Typelist<Head, Tail>, T, U>
372
 
        {
373
 
            typedef Typelist<Head,
374
 
                    typename ReplaceAll<Tail, T, U>::Result>
375
 
                Result;
376
 
        };
377
 
 
378
 
////////////////////////////////////////////////////////////////////////////////
379
 
// class template Reverse
380
 
// Reverses a typelist
381
 
// Invocation (TList is a typelist):
382
 
// Reverse<TList>::Result
383
 
// returns a typelist that is TList reversed
384
 
////////////////////////////////////////////////////////////////////////////////
385
 
 
386
 
        template <class TList> struct Reverse;
387
 
        
388
 
        template <>
389
 
        struct Reverse<NullType>
390
 
        {
391
 
            typedef NullType Result;
392
 
        };
393
 
        
394
 
        template <class Head, class Tail>
395
 
        struct Reverse< Typelist<Head, Tail> >
396
 
        {
397
 
            typedef typename Append<
398
 
                typename Reverse<Tail>::Result, Head>::Result Result;
399
 
        };
400
 
 
401
 
////////////////////////////////////////////////////////////////////////////////
402
 
// class template MostDerived
403
 
// Finds the type in a typelist that is the most derived from a given type
404
 
// Invocation (TList is a typelist, T is a type):
405
 
// MostDerived<TList, T>::Result
406
 
// returns the type in TList that's the most derived from T
407
 
////////////////////////////////////////////////////////////////////////////////
408
 
 
409
 
        template <class TList, class T> struct MostDerived;
410
 
        
411
 
        template <class T>
412
 
        struct MostDerived<NullType, T>
413
 
        {
414
 
            typedef T Result;
415
 
        };
416
 
        
417
 
        template <class Head, class Tail, class T>
418
 
        struct MostDerived<Typelist<Head, Tail>, T>
419
 
        {
420
 
        private:
421
 
            typedef typename MostDerived<Tail, T>::Result Candidate;
422
 
        public:
423
 
            typedef typename Select<
424
 
                SuperSubclass<Candidate,Head>::value,
425
 
                    Head, Candidate>::Result Result;
426
 
        };
427
 
 
428
 
////////////////////////////////////////////////////////////////////////////////
429
 
// class template DerivedToFront
430
 
// Arranges the types in a typelist so that the most derived types appear first
431
 
// Invocation (TList is a typelist):
432
 
// DerivedToFront<TList>::Result
433
 
// returns the reordered TList 
434
 
////////////////////////////////////////////////////////////////////////////////
435
 
 
436
 
        template <class TList> struct DerivedToFront;
437
 
        
438
 
        template <>
439
 
        struct DerivedToFront<NullType>
440
 
        {
441
 
            typedef NullType Result;
442
 
        };
443
 
        
444
 
        template <class Head, class Tail>
445
 
        struct DerivedToFront< Typelist<Head, Tail> >
446
 
        {
447
 
        private:
448
 
            typedef typename MostDerived<Tail, Head>::Result
449
 
                TheMostDerived;
450
 
            typedef typename Replace<Tail,
451
 
                TheMostDerived, Head>::Result Temp;
452
 
            typedef typename DerivedToFront<Temp>::Result L;
453
 
        public:
454
 
            typedef Typelist<TheMostDerived, L> Result;
455
 
        };
456
 
        
457
 
    }   // namespace TL
458
 
}   // namespace Loki
459
 
 
460
 
////////////////////////////////////////////////////////////////////////////////
461
 
// Change log:
462
 
// June 09, 2001: Fix bug in parameter list of macros LOKI_TYPELIST_23 to LOKI_TYPELIST_27
463
 
//      (credit due to Dave Taylor)
464
 
// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
465
 
// November 22, 2001: fixed bug in DerivedToFront
466
 
//      (credit due to Gianni Luciani who noticed the bug first;
467
 
//          Adam Wilkshire;
468
 
//          Friedrik Hedman who fixed the bug but didn't send the fix;
469
 
//          Kevin Cline who sent the first actual fix)
470
 
// May 13, 2002: LOKI_TYPELIST_46 called LOKI_TYPELIST_45 with only 44 parameters.
471
 
//      Credit due to Robert Minsk     
472
 
// September 16, 2002: Changed MostDerived to use the new SuperSubclass template
473
 
//     (rather than the SUPERSUBCLASS macro).
474
 
//     Minor fix in Reverse, adding support for empty lists, like all the other
475
 
//     algorithms.
476
 
//     Fixed DerivedToFront, to use Replace, rather than ReplaceAll. T.S.
477
 
// Oct  10, 2002: added MakeTypelist (SGB/MKH)
478
 
////////////////////////////////////////////////////////////////////////////////
479
 
 
480
 
#endif // LOKI_TYPELIST_INC_
481
 
 
482
 
// $Log: Typelist.h,v $
483
 
// Revision 1.5  2006/01/16 19:05:09  rich_sposato
484
 
// Added cvs keywords.
485
 
//