00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef METALIC_TYPE_HH
00029 # define METALIC_TYPE_HH
00030
00031 # include <mlc/config/system.hh>
00032 # include <mlc/bool.hh>
00033
00034 # include <string>
00035 # include <cassert>
00036
00037
00038
00039
00040
00041
00042
00043 namespace mlc_hierarchy
00044 {
00045
00046
00047
00048
00049
00050 template<class E>
00051 class any_with_diamond
00052 {
00053 public:
00054 typedef E exact_type;
00055
00056 E& exact()
00057 {
00058 return *(E*)((char*)this - exact_offset);
00059 }
00060 const E& exact() const
00061 {
00062 return *(const E*)((const char*)this - exact_offset);
00063 }
00064
00065 static std::string name()
00066 {
00067 return std::string("any_with_diamond<") + E::name() + ">";
00068 }
00069
00070 private:
00071
00072
00073
00074
00075 static const int exact_offset;
00076
00077
00078 static const E exact_obj;
00079 static const any_with_diamond<E>& ref_exact_obj;
00080 };
00081
00082 template <class E>
00083 const E any_with_diamond<E>::exact_obj = E();
00084
00085 template <class E>
00086 const any_with_diamond<E>&
00087 any_with_diamond<E>::ref_exact_obj = any_with_diamond<E>::exact_obj;
00088
00089 template <class E>
00090 const int any_with_diamond<E>::exact_offset =
00091 (const char*)(void*)(&any_with_diamond<E>::ref_exact_obj)
00092 - (const char*)(void*)(&any_with_diamond<E>::exact_obj);
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 template<class E>
00103 class any
00104 {
00105 public:
00106 typedef E exact_type;
00107
00108 E& exact() { return static_cast<E&>(*this); }
00109 const E& exact() const { return static_cast<const E&>(*this); }
00110
00111 static std::string name()
00112 {
00113 return std::string("any<") + E::name() + ">";
00114 }
00115
00116 protected:
00117 any() {}
00118 };
00119
00120 }
00121
00122 namespace mlc
00123 {
00124
00125
00126
00127
00128
00129 struct undefined {};
00130
00131 class final
00132 {
00133 public:
00134 static std::string name() { return "final"; }
00135 protected:
00136 final() {}
00137 };
00138
00139 class top
00140 {
00141 public:
00142 static std::string name() { return ""; }
00143 protected:
00144 top() {}
00145 };
00146
00147
00148
00149
00150
00151
00152
00153
00154 template <class Exact, class Final>
00155 struct assign_exact_offset
00156 {
00157 template <class E>
00158 static void doit(const E&)
00159 {}
00160 };
00161
00162 template <>
00163 struct assign_exact_offset<mlc::final, mlc::final>
00164 {
00165 template <class E>
00166 static void doit(const E* t)
00167 {
00168 if (!mlc_hierarchy::any_with_diamond<E>::offset_assigned)
00169 {
00170 mlc_hierarchy::any_with_diamond<E>::exact_offset =
00171 (const char*)
00172 static_cast<const mlc_hierarchy::any_with_diamond<E>*>(t)
00173 - (const char*) t;
00174 mlc_hierarchy::any_with_diamond<E>::offset_assigned = true;
00175 }
00176 }
00177 };
00178
00179
00180
00181
00182
00183 template<class T>
00184 struct exact
00185 {
00186 typedef typename T::exact_type ret;
00187 };
00188
00189 template<class T>
00190 struct exact<const T>
00191 {
00192 typedef const typename exact<T>::ret ret;
00193 };
00194
00195 template<class T>
00196 struct exact<T*>
00197 {
00198 typedef typename exact<T>::ret* ret;
00199 };
00200
00201 template<class T>
00202 struct exact<T&>
00203 {
00204 typedef typename exact<T>::ret& ret;
00205 };
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 template<class T, class Exact>
00218 struct exact_vt
00219 {
00220 typedef Exact ret;
00221 };
00222
00223 template<class Exact>
00224 struct exact_vt<Exact,final>
00225 {
00226 typedef Exact ret;
00227 };
00228
00229 }
00230
00231
00232
00233
00234
00235
00236
00237 # define mlc_exact_type(T) typename mlc::exact< T >::ret
00238 # define mlc_exact_type_(T) mlc::exact< T >::ret
00239
00240
00241
00242 # define mlc_exact_vt_type(T, Exact) typename mlc::exact_vt<T, Exact >::ret
00243 # define mlc_2_exact_vt_type(self, T, Exact) typename mlc::exact_vt<self<T, Exact >, Exact >::ret
00244 # define mlc_exact_vt_type_(T, Exact) mlc::exact_vt<T, Exact >::ret
00245 # define mlc_2_exact_vt_type_(self, T, Exact) mlc::exact_vt<self<T, Exact >, Exact >::ret
00246
00247 namespace mlc
00248 {
00249
00250
00251
00252
00253
00254 template<class T> inline
00255 mlc_exact_type(T)&
00256 to_exact(T& ref)
00257 {
00258 return ref.exact();
00259 }
00260
00261 template<class T> inline
00262 const mlc_exact_type(T)&
00263 to_exact(const T& ref)
00264 {
00265 return ref.exact();
00266 }
00267
00268 template<class T> inline
00269 mlc_exact_type(T)*
00270 to_exact(T* ptr)
00271 {
00272 return &ptr->exact();
00273 }
00274
00275 template<class T> inline
00276 const mlc_exact_type(T)*
00277 to_exact(const T* ptr)
00278 {
00279 return &ptr->exact();
00280 }
00281
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 # define mlc_init_static_hierarchy(Exact)
00308
00309
00310
00311
00312
00313 # define mlc_dispatch(Fun) return exact().Fun##_impl
00314
00315 #endif // ! METALIC_TYPE_HH