Library of Assembled Shared Sources
tuple.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#ifndef LASS_GUARDIAN_OF_INCLUSION_META_TUPLE_H
44#define LASS_GUARDIAN_OF_INCLUSION_META_TUPLE_H
45
46#include "meta_common.h"
47#include "type_list.h"
48#include "empty_type.h"
49
50namespace lass
51{
52namespace meta
53{
54
55template <typename TList> class Tuple;
56
57template <typename H, typename Ts>
58class Tuple< TypeList<H, Ts> >: public Tuple<Ts>
59{
60public:
61 typedef H TValue;
62 typedef Tuple<Ts> TTail;
63 typedef TypeList<H, Ts> TList;
64 Tuple(): value_() {}
65 const H& value() const { return value_; }
66 H& value() { return value_; }
67private:
68 H value_;
69};
70
71template <typename Ts>
72class Tuple< TypeList<meta::EmptyType, Ts> >: public Tuple<Ts>
73{
74public:
75 typedef meta::EmptyType TValue;
76 typedef Tuple<Ts> TTail;
77 typedef TypeList<meta::EmptyType, Ts> TList;
78 const meta::EmptyType& value() const { return meta::EmptyType::instance(); }
79 meta::EmptyType& value() { return meta::EmptyType::instance(); }
80};
81
82template <>
83class Tuple<meta::NullType>
84{
85public:
86 typedef meta::NullType TValue;
87 typedef meta::NullType TTail;
88 typedef meta::NullType TList;
89};
90
91
92
93namespace tuple
94{
95
96template <typename TupleType, size_t index>
97struct Field
98{
99 typedef typename type_list::At<typename TupleType::TList, index>::Type Type;
100};
101
102template <typename TupleType, size_t index>
103struct Field<const TupleType, index>
104{
105 typedef const typename type_list::At<typename TupleType::TList, index>::Type Type;
106};
107
108
109
110template <typename TupleType, size_t index> struct SubType;
111
112template <typename H, typename Ts, size_t index>
113struct SubType< Tuple< TypeList<H, Ts> >, index >: public SubType< Tuple<Ts>, index - 1 >
114{
115};
116
117template <typename H, typename Ts>
118struct SubType< Tuple< TypeList<H, Ts> >, 0 >
119{
120 typedef Tuple< TypeList<H, Ts> > Type;
121};
122
123template <typename H, typename Ts, size_t index>
124struct SubType< const Tuple< TypeList<H, Ts> >, index >: public SubType< const Tuple<Ts>, index - 1 >
125{
126};
127
128template <typename H, typename Ts>
129struct SubType< const Tuple< TypeList<H, Ts> >, 0 >
130{
131 typedef const Tuple< TypeList<H, Ts> > Type;
132};
133
134
135
136template <size_t index, typename TupleType>
137typename Field<TupleType, index>::Type& field(TupleType& tuple)
138{
139 typedef typename SubType<TupleType, index>::Type TSubType;
140 return static_cast<TSubType&>(tuple).value();
141}
142
143
144
145namespace impl
146{
147 template <typename TupleType> struct ForEach;
148 template <typename H, typename Ts>
149 struct ForEach< Tuple< TypeList<H, Ts> > >
150 {
151 template <typename Functor> static void call(Tuple< TypeList<H, Ts> >& tuple, Functor& fun)
152 {
153 fun(tuple.value());
154 ForEach< Tuple<Ts> >::call(tuple, fun);
155 }
156 template <typename Functor> static void call(const Tuple< TypeList<H, Ts> >& tuple, Functor& fun)
157 {
158 fun(tuple.value());
159 ForEach< Tuple<Ts> >::call(tuple, fun);
160 }
161 };
162 template <typename Ts>
163 struct ForEach< Tuple< TypeList<meta::EmptyType, Ts> > >
164 {
165 template <typename Functor> static void call(Tuple< TypeList<meta::EmptyType, Ts> >& tuple, Functor& fun)
166 {
167 ForEach< Tuple<Ts> >::call(tuple, fun);
168 }
169 template <typename Functor> static void call(const Tuple< TypeList<meta::EmptyType, Ts> >& tuple, Functor& fun)
170 {
171 ForEach< Tuple<Ts> >::call(tuple, fun);
172 }
173 };
174 template <>
175 struct ForEach< Tuple< meta::NullType> >
176 {
177 template <typename Functor> static void call(Tuple<meta::NullType>&, Functor&)
178 {
179 }
180 template <typename Functor> static void call(const Tuple<meta::NullType>&, Functor&)
181 {
182 }
183 };
184}
185
186template <typename TupleType, typename Functor>
187void forEach(TupleType& tuple, Functor& fun)
188{
189 impl::ForEach<TupleType>::call(tuple, fun);
190}
191
192template <typename TupleType, typename Functor>
193void forEach(const TupleType& tuple, Functor& fun)
194{
195 impl::ForEach<TupleType>::call(tuple, fun);
196}
197
198
199
200}
201
202}
203}
204
205#endif
206
207// EOF
library for template meta programming
Definition bool.h:76
Library for Assembled Shared Sources.
Definition config.h:53