CrystalSpace

Public API Reference

csutil/typetraits.h

Go to the documentation of this file.
00001 /*
00002   Copyright (C) 2006 by Marten Svanfeldt
00003 
00004   This library is free software; you can redistribute it and/or
00005   modify it under the terms of the GNU Library General Public
00006   License as published by the Free Software Foundation; either
00007   version 2 of the License, or (at your option) any later version.
00008 
00009   This library is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012   Library General Public License for more details.
00013 
00014   You should have received a copy of the GNU Library General Public
00015   License along with this library; if not, write to the Free
00016   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CSUTIL_TYPETRAITS_H__
00020 #define __CSUTIL_TYPETRAITS_H__
00021 
00027 namespace CS
00028 {
00029   namespace Meta
00030   {
00031     namespace Implementation
00032     {
00033       // Types for checking yes/no
00034 
00036       typedef char YesType;
00037 
00039       struct NoType
00040       {
00041         char padding[8];
00042       };
00043 
00045       template <class T>
00046       struct Wrap { Wrap () {} };
00047 
00048       template <class T> T&(* IsReferenceHelper1(Wrap<T>) )(Wrap<T>);
00049       char IsReferenceHelper1(...);
00050 
00051       template <class T> NoType IsReferenceHelper2(T&(*)(Wrap<T>));
00052       YesType IsReferenceHelper2(...);
00053 
00054       template <class T>
00055       struct IsReferenceImpl
00056       {
00057         static const bool value = sizeof(IsReferenceHelper2 (
00058           IsReferenceHelper1 (Wrap<T>()))) == 1;
00059       };
00060 
00061 
00062       template <bool b1, bool b2, bool b3 = true, bool b4 = true, bool b5 = true, 
00063                 bool b6 = true, bool b7 = true>
00064       struct TraitAnd;
00065 
00066       template <bool b1, bool b2, bool b3, bool b4, bool b5, bool b6, bool b7>
00067       struct TraitAnd
00068       {
00069         static const bool value = false;
00070       };
00071 
00072       template <>
00073       struct TraitAnd<true, true, true, true, true, true, true>
00074       {
00075         static const bool value = true;
00076       };
00077 
00078       template <class T>
00079       YesType IsSameTester(T*, T*);
00080 
00081       NoType IsSameTester(...);
00082 
00083       template <class T, class U>
00084       struct IsSameImpl
00085       {
00086         static T* t;
00087         static U* u;
00088 
00089         static const bool value = TraitAnd<
00090           sizeof(YesType) == sizeof(IsSameTester(t, u)),
00091           IsReferenceImpl<T>::value == IsReferenceImpl<U>::value,
00092           sizeof(T) == sizeof(U)>::value;
00093       };
00094     } // namespace Implementation
00095 
00096 
00100     template <class Type>
00101     struct IsReference
00102     {
00103       static const bool value = CS::Meta::Implementation
00104         ::IsReferenceImpl<Type>::value;
00105     };
00106 
00110     template <class Type1, class Type2>
00111     struct IsSame
00112     {
00113       static const bool value = CS::Meta::Implementation
00114         ::IsSameImpl<Type1, Type2>::value;
00115     };
00116   } // namespace TypeTraits
00117 
00118 } // namespace CS
00119 
00120 #endif 

Generated for Crystal Space 1.2.1 by doxygen 1.5.3