tizen 2.4 release
[framework/web/wrt-commons.git] / modules / core / include / dpl / type_list.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        type_list.h
18  * @author      Bartosz Janiak (b.janiak@samsung.com)
19  * @version     1.0
20  * @brief       Generic type list template
21  */
22 #ifndef DPL_TYPE_LIST_H
23 #define DPL_TYPE_LIST_H
24
25 #include <cstddef>
26
27 namespace DPL {
28 class TypeListGuard
29 {
30   public:
31     template<size_t Index>
32     struct Element
33     {
34         struct ERROR_TypeListElementIndexIsOutOfBounds;
35         typedef ERROR_TypeListElementIndexIsOutOfBounds Type;
36     };
37
38     static const size_t Size = 0;
39 };
40
41 template<typename HeadType, typename TailType>
42 class TypeList
43 {
44   private:
45     class DummyClass
46     {};
47
48     template<typename List, size_t Enum>
49     struct TypeCounter : public TypeCounter<typename List::Tail, Enum + 1>
50     {};
51
52     template<size_t Enum>
53     struct TypeCounter<TypeListGuard, Enum>
54     {
55         static const size_t Size = Enum;
56     };
57
58   public:
59     typedef TailType Tail;
60     typedef HeadType Head;
61     typedef TypeList<HeadType, TailType> ThisType;
62
63     template<size_t Index, typename DummyType = DummyClass>
64     struct Element
65     {
66         typedef typename TailType::template Element<Index - 1>::Type Type;
67     };
68
69     template<typename DummyType>
70     struct Element<0, DummyType>
71     {
72         typedef HeadType Type;
73     };
74
75     template<typename Type, typename DummyType = DummyClass>
76     struct Contains
77     {
78         typedef typename TailType::template Contains<Type>::Yes Yes;
79     };
80
81     template<typename DummyType>
82     struct Contains<HeadType, DummyType>
83     {
84         typedef int Yes;
85     };
86
87     static const size_t Size = TypeCounter<ThisType, 0>::Size;
88 };
89
90 template<typename... T>
91 struct TypeListDecl;
92
93 template<typename First, typename... Rest>
94 struct TypeListDecl<First, Rest...>
95 {
96     typedef TypeList<First,
97                      typename TypeListDecl<Rest...>::Type> Type;
98 };
99
100 template<typename Last>
101 struct TypeListDecl<Last>
102 {
103     typedef TypeList<Last, TypeListGuard> Type;
104 };
105
106 template<>
107 struct TypeListDecl<TypeListGuard>
108 {
109     typedef TypeListGuard Type;
110 };
111
112 template<>
113 struct TypeListDecl<>
114 {
115     typedef TypeListGuard Type;
116 };
117
118 } // namespace DPL
119
120 #endif // DPL_TYPE_LIST_H