// Semantics Consulting's Tyr Library // http://www.semantics.org // // Copyright (c) 2003 by Stephen C. Dewhurst // // Permission to use, copy, modify, distribute, and sell this software // for any purpose is hereby granted without fee, provided that the above // copyright notice appears in all copies and that both that copyright // notice and this permission notice appear in supporting documentation. // The author makes no representations about the suitability of this // software for any purpose. It is provided "as is" without express // or implied warranty. // Described in: // Unfinished Business. C/C++ Users Journal Experts Forum, 21, 11 (November 2003). #ifndef DISMANTLE_H #define DISMANTLE_H #include "typelist.h" namespace Tyr { // // This is a quick and experimental version of a template metaprogramming // facility for compile time type manipulation. It is described in // Semantics Consulting's "Once, Weakly" feature of 23 February 2003, at // http://www.semantics.org/once_weakly.html. // // Basically, we dismantle a type into its constituent parts. We then have // the opportunity to examine its parts, change them, remove some, add some // new pieces, perform regular-expression-like matching and substitution, etc. // Finally, we can regenerate a "regular" type from the (potentially) modified // dismantled type. // // From the web posting: "In effect, we now have two equivalent, but structurally // distinct, versions of the same type. The normal version is optimized for use // in a traditional fashion: accessing its operations, causing code to be generated // that will execute at runtime, etc. The dismantled version is optimized for // compile time analysis and manipulation. Therefore, we now have the ability to // move an arbitrary type among different representations according to how we want // to use it. The invertibility of the representations assures us that either // representation will contain all the information present in the other." //------------------------------ // WHAT'S HERE? // // Dismantle a type into its constituent parts. template struct Dis; // Regenerate a dismantled type. template struct Regen; //------------------------------- // // Types that represent type qualifiers and type modifiers in // the dismantled version of a type. // struct ConstDis {}; struct VolatileDis {}; struct RefDis {}; struct PtrDis {}; template struct AryDis { enum { bound = b }; }; template struct PcmDis { typedef typename Dis::R Class; }; // Function modifiers (member and non-member) modify the return type. // Therefore, only the arguments are available within these modifier types. struct Fun0 {}; template struct Fun1 { typedef typename Dis::R Arg; }; template struct Fun2 { typedef typename Dis::R Arg1; typedef typename Dis::R Arg2; }; template struct MFun0 { typedef typename Dis::R Class; }; template struct MFun1 { typedef typename Dis::R Class; typedef typename Dis::R Arg; }; template struct MFun2 { typedef typename Dis::R Class; typedef typename Dis::R Arg1; typedef typename Dis::R Arg2; }; template struct CMFun0 { typedef typename Dis::R Class; }; template struct CMFun1 { typedef typename Dis::R Class; typedef typename Dis::R Arg; }; template struct CMFun2 { typedef typename Dis::R Class; typedef typename Dis::R Arg1; typedef typename Dis::R Arg2; }; //------------------------------- // TYPE DISMANTLING // // The primary template is the catch-all for any type // that is not more specifically specialized. // See specialized types below. // template struct Dis { typedef typelist R; }; template struct Dis { typedef typelist::R> R; }; template struct Dis { typedef typelist::R> R; }; template struct Dis { typedef typelist::R> R; }; template struct Dis { typedef typelist::R> R; }; template struct Dis { typedef typelist::R> R; }; template struct Dis< T *const> { typedef typelist::R> R; }; template struct Dis< T *volatile> { typedef typelist::R> R; }; template struct Dis< T *const volatile> { typedef typelist::R> R; }; template struct Dis { typedef typelist,typename Dis::R> R; }; template struct Dis { typedef typelist,typelist::R> > R; }; template struct Dis { typedef typelist,typelist::R> > R; }; template struct Dis { typedef typelist,typelist::R> > > R; }; template struct Dis { typedef typelist,typename Dis::R> R; }; template struct Dis { typedef typelist::R> R; }; template struct Dis { typedef typelist::R> R; }; template struct Dis { typedef typelist::R> R; }; template struct Dis { typedef typelist::R> R; }; template struct Dis { typedef typelist,typename Dis::R> R; }; template struct Dis { typedef typelist,typename Dis::R> R; }; template struct Dis { typedef typelist,typename Dis::R> R; }; template struct Dis { typedef typelist,typename Dis::R> R; }; template struct Dis { typedef typelist,typename Dis::R> R; }; template struct Dis { typedef typelist< ConstDis,typelist,typename Dis::R> > R; }; template struct Dis { typedef typelist< ConstDis, typelist,typename Dis::R> > R; }; template struct Dis { typedef typelist< ConstDis, typelist,typename Dis::R> > R; }; //------------------------------ // REGENERATING A DISMANTLED TYPE // template struct Regen; template struct Regen< typelist > { typedef T R; }; template struct Regen< typelist > { typedef const typename Regen::R R; }; template struct Regen< typelist > { typedef volatile typename Regen::R R; }; template struct Regen< typelist > { typedef typename Regen::R &R; }; template struct Regen< typelist > { typedef typename Regen::R *R; }; template struct Regen< typelist,T> > { typedef typename Regen::R R[b]; }; template struct Regen< typelist,T> > { typedef typename Regen::R C::*R; }; template struct Regen< typelist > { typedef typename Regen::R R(); }; template struct Regen< typelist,T> > { typedef typename Regen::R R( A ); }; template struct Regen< typelist,T> > { typedef typename Regen::R R( A1, A2 ); }; template struct Regen< typelist,T> > { typedef typename Regen::R (C::*R)(); }; template struct Regen< typelist,T> > { typedef typename Regen::R (C::*R)( A ); }; template struct Regen< typelist,T> > { typedef typename Regen::R (C::*R)( A1, A2 ); }; template struct Regen< typelist,T> > { typedef typename Regen::R (C::*R)() const; }; template struct Regen< typelist,T> > { typedef typename Regen::R (C::*R)( A ) const; }; template struct Regen< typelist,T> > { typedef typename Regen::R (C::*R)( A1, A2 ) const; }; } // namespace Tyr #endif