library of assembled shared sources

http://lass.cocamware.com

tuple.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #ifndef LASS_GUARDIAN_OF_INCLUSION_META_TUPLE_H
00044 #define LASS_GUARDIAN_OF_INCLUSION_META_TUPLE_H
00045 
00046 #include "meta_common.h"
00047 #include "type_list.h"
00048 #include "empty_type.h"
00049 
00050 namespace lass
00051 {
00052 namespace meta
00053 {
00054 
00055 template <typename TList> class Tuple;
00056 
00057 template <typename H, typename Ts>
00058 class Tuple< TypeList<H, Ts> >: public Tuple<Ts>
00059 {
00060 public:
00061     typedef H TValue;
00062     typedef Tuple<Ts> TTail;
00063     typedef TypeList<H, Ts> TList;
00064     Tuple(): value_() {}
00065     const H& value() const { return value_; }
00066     H& value() { return value_; }
00067 private:
00068     H value_;
00069 };
00070 
00071 template <typename Ts>
00072 class Tuple< TypeList<meta::EmptyType, Ts> >: public Tuple<Ts>
00073 {
00074 public:
00075     typedef meta::EmptyType TValue;
00076     typedef Tuple<Ts> TTail;
00077     typedef TypeList<meta::EmptyType, Ts> TList;
00078     const meta::EmptyType& value() const { return meta::EmptyType::instance(); }
00079     meta::EmptyType& value() { return meta::EmptyType::instance(); }
00080 };
00081 
00082 template <>
00083 class Tuple<meta::NullType>
00084 {
00085 public:
00086     typedef meta::NullType TValue;
00087     typedef meta::NullType TTail;
00088     typedef meta::NullType TList;
00089 };
00090 
00091 
00092 
00093 namespace tuple
00094 {
00095 
00096 template <typename TupleType, size_t index> 
00097 struct Field
00098 {
00099     typedef typename type_list::At<typename TupleType::TList, index>::Type Type;
00100 };
00101 
00102 template <typename TupleType, size_t index> 
00103 struct Field<const TupleType, index>
00104 {
00105     typedef const typename type_list::At<typename TupleType::TList, index>::Type Type;
00106 };
00107 
00108 
00109 
00110 template <typename TupleType, size_t index> struct SubType;
00111 
00112 template <typename H, typename Ts, size_t index> 
00113 struct SubType< Tuple< TypeList<H, Ts> >, index >: public SubType< Tuple<Ts>, index - 1 >
00114 {
00115 };
00116 
00117 template <typename H, typename Ts> 
00118 struct SubType< Tuple< TypeList<H, Ts> >, 0 >
00119 {
00120     typedef Tuple< TypeList<H, Ts> > Type;
00121 };
00122 
00123 template <typename H, typename Ts, size_t index> 
00124 struct SubType< const Tuple< TypeList<H, Ts> >, index >: public SubType< const Tuple<Ts>, index - 1 >
00125 {
00126 };
00127 
00128 template <typename H, typename Ts> 
00129 struct SubType< const Tuple< TypeList<H, Ts> >, 0 >
00130 {
00131     typedef const Tuple< TypeList<H, Ts> > Type;
00132 };
00133 
00134 
00135 
00136 template <size_t index, typename TupleType> 
00137 typename Field<TupleType, index>::Type& field(TupleType& tuple)
00138 {
00139     typedef typename SubType<TupleType, index>::Type TSubType;
00140     return static_cast<TSubType&>(tuple).value();
00141 }
00142 
00143 
00144 
00145 namespace impl
00146 {
00147     template <typename TupleType> struct ForEach;
00148     template <typename H, typename Ts>
00149     struct ForEach< Tuple< TypeList<H, Ts> > >
00150     {
00151         template <typename Functor> static void call(Tuple< TypeList<H, Ts> >& tuple, Functor& fun)
00152         {
00153             fun(tuple.value());
00154             ForEach< Tuple<Ts> >::call(tuple, fun);
00155         }
00156     };
00157     template <typename Ts>
00158     struct ForEach< Tuple< TypeList<meta::EmptyType, Ts> > >
00159     {
00160         template <typename Functor> static void call(Tuple< TypeList<meta::EmptyType, Ts> >& tuple, Functor& fun)
00161         {
00162             ForEach< Tuple<Ts> >::call(tuple, fun);
00163         }
00164     };
00165     template <>
00166     struct ForEach< Tuple< meta::NullType> >
00167     {
00168         template <typename Functor> static void call(Tuple<meta::NullType>& tuple, Functor& fun)
00169         {
00170         }
00171     };
00172 }
00173 
00174 template <typename TupleType, typename Functor>
00175 void forEach(TupleType& tuple, Functor& fun)
00176 {
00177     impl::ForEach<TupleType>::call(tuple, fun);
00178 }
00179 
00180 
00181 
00182 }
00183 
00184 }
00185 }
00186 
00187 #endif
00188 
00189 // EOF

Generated on Mon Nov 10 14:22:01 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo