Sierra Toolkit  Version of the Day
TypeListMap.hpp
Go to the documentation of this file.
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2005 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8 
9 
10 #ifndef STK_UTIL_UTIL_TypeListMap_h
11 #define STK_UTIL_UTIL_TypeListMap_h
12 
19 #include <stk_util/util/TypeList.hpp>
20 
26 namespace sierra {
27 
36 template< class ListType > class TypeListMap ;
37 
38 //----------------------------------------------------------------------
39 
40 template<typename T> class TypeListMapValue ;
41 
42 template<typename T>
43 class TypeListMapValue<T const &>
44 {
45 private:
46  T const * x ;
47 public:
48  typedef TypeListMapValue<T const &> SelfType ;
49  typedef T const & const_reference_type ;
50  typedef T const & reference_type ;
51 
52  TypeListMapValue() : x(0) {}
53  TypeListMapValue( SelfType const & v ) : x( v.x ) {}
54  explicit TypeListMapValue( T const & v ) : x( & v ) {}
55 
56  SelfType & operator = ( SelfType const & v ) { x = v.x ; return *this ; }
57  SelfType & operator = ( T const & v ) { x = &v ; return *this ; }
58 
59  operator T const & () const { return *x ; }
60  T const & get() const { return *x ; }
61 };
62 
63 template<typename T>
64 class TypeListMapValue<T&>
65 {
66 private:
67  T * x ;
68 public:
69  typedef TypeListMapValue<T&> SelfType ;
70  typedef T & const_reference_type ;
71  typedef T & reference_type ;
72 
73  TypeListMapValue() : x(0) {}
74  TypeListMapValue( const SelfType & v ) : x( v.x ) {}
75  explicit TypeListMapValue( T & v ) : x( & v ) {}
76 
77  SelfType & operator = ( SelfType const & v ) { x = v.x ; return *this ; }
78  SelfType & operator = ( T & v ) { x = &v ; return *this ; }
79 
80  operator T & () const { return *x ; }
81  T & get() const { return *x ; }
82 };
83 
84 template<typename T>
85 class TypeListMapValue<T const>
86 {
87 private:
88  T x ;
89 public:
90  typedef TypeListMapValue<T> SelfType ;
91  typedef T const & const_reference_type ;
92  typedef T const & reference_type ;
93 
94  TypeListMapValue() {}
95  TypeListMapValue( SelfType const & v ) : x( v.x ) {}
96  explicit TypeListMapValue( T const & v ) : x( v ) {}
97 
98  SelfType & operator = ( SelfType const & v ) { x = v.x ; return *this ; }
99  SelfType & operator = ( T const & v ) { x = v ; return *this ; }
100 
101  operator T const & () const { return x ; }
102  T const & get() const { return x ; }
103 };
104 
105 template<typename T>
106 class TypeListMapValue
107 {
108 private:
109  T x ;
110 public:
111  typedef TypeListMapValue<T> SelfType ;
112  typedef T const & const_reference_type ;
113  typedef T & reference_type ;
114 
115  TypeListMapValue() {}
116  TypeListMapValue( SelfType const & v ) : x( v.x ) {}
117  explicit TypeListMapValue( T const & v ) : x( v ) {}
118 
119  SelfType & operator = ( SelfType const & v ) { x = v.x ; return *this ; }
120  SelfType & operator = ( T const & v ) { x = v ; return *this ; }
121 
122  operator T const & () const { return x ; }
123  T const & get() const { return x ; }
124 };
125 
126 //----------------------------------------------------------------------
127 
128 template<>
129 class TypeListMap<TypeListEnd> {};
130 
131 template<typename Tail>
132 class TypeListMap< TypeList<TypeListEnd,Tail> > {};
133 
134 template<class ListType>
135 class TypeListMap : public TypeListMap<typename ListType::TypeListTail>
136 {
137 private:
138  template<typename U> friend class TypeListMap ;
139 
140  typedef typename ListType::TypeListTail TailType ;
141  typedef typename ListType::TypeListValue TagType ;
142  typedef typename TagType::type type ;
143  TypeListMapValue<type> m_value ;
144 
145 public:
146 
147  typedef TypeListMap<ListType> SelfType ;
148 
149  //----------
150 
151  template<class Tag>
152  typename TypeListMapValue<typename Tag::type>::const_reference_type
153  get() const
154  {
155  typedef typename TypeListMember<ListType,Tag>::list_type MemberListType ;
156  return ((TypeListMap<MemberListType> const &) *this).m_value.get();
157  }
158 
159  template<class Tag>
160  void
161  set( typename TypeListMapValue<typename Tag::type>::const_reference_type v )
162  {
163  typedef typename TypeListMember<ListType,Tag>::list_type MemberListType ;
164  ((TypeListMap<MemberListType> &) *this).m_value.operator=( v );
165  }
166 
167  //----------
168 
169  TypeListMap<TailType> & operator <<
170  ( typename TypeListMapValue<type>::const_reference_type v )
171  { m_value = v ; return *this ; }
172 
173  TypeListMap<TailType> const & operator >>
174  ( typename TypeListMapValue<type>::reference_type v ) const
175  { v = m_value ; return *this ; }
176 
177  //----------
178 
179  void copy( TypeListMap<TypeListEnd> const & ) {}
180 
181  template<class ListB>
182  void copy( TypeListMap<TypeList<TypeListEnd,ListB> > const & b ) {}
183 
184  template<class ListB>
185  void copy( TypeListMap<ListB> const & b )
186  {
187  typedef typename ListB::TypeListValue TagB ;
188  typedef typename ListB::TypeListTail TailB ;
189  this->template set<TagB>( b.template get<TagB>() );
190  copy( (TypeListMap<TailB> const &) b );
191  }
192 
193  //----------
194 
195  ~TypeListMap() {}
196 
197  TypeListMap() {}
198 
199  TypeListMap( const SelfType & m )
200  : TypeListMap<TailType>( m ), m_value( m.m_value ) {}
201 
202  SelfType & operator = ( const SelfType & m )
203  {
204  TypeListMap<TailType>::operator=( m );
205  m_value = m.m_value ;
206  return *this ;
207  }
208 };
209 
210 //----------------------------------------------------------------------
211 //----------------------------------------------------------------------
212 
213 }
214 
215 #endif // STK_UTIL_UTIL_TypeListMap_h
Definition: Env.cpp:53