csutil/metautils.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

csutil/metautils.h

    00001 /*
    00002     Copyright (C) 2007 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
    00020 #ifndef __CS_CSUTIL_METAUTILS_H__
    00021 #define __CS_CSUTIL_METAUTILS_H__
    00022
    00023 namespace CS
    00024 {
    00025   namespace Meta
    00026   {
    00027     namespace Implementation
    00028     {
    00029
    00031       template<typename T>
    00032       struct AlignmentOfHack
    00033       {
    00034         char c;
    00035         T t;
    00036         AlignmentOfHack();
    00037       };
    00038
    00040       template<size_t A, size_t S>
    00041       struct AlignmentLogic
    00042       {
    00043         static const unsigned int value = A < S ? A : S;
    00044       };
    00045
    00047       template<typename T>
    00048       struct AlignmentOfImpl
    00049       {
    00050         static const size_t value = AlignmentLogic<
    00051           sizeof(AlignmentOfHack<T>) - sizeof(T),
    00052           sizeof(T)>::value;
    00053       };
    00054
    00055
    00056       // Provide types forced aligned to certain boundaries
    00057       struct Aligned1 {CS_ALIGNED_MEMBER(char c, 1);};
    00058       struct Aligned2 {CS_ALIGNED_MEMBER(char c, 2);};
    00059       struct Aligned4 {CS_ALIGNED_MEMBER(char c, 4);};
    00060       struct Aligned8 {CS_ALIGNED_MEMBER(char c, 8);};
    00061       struct Aligned16 {CS_ALIGNED_MEMBER(char c, 16);};
    00062
    00063     }
    00064
    00068     template<typename T>
    00069     struct AlignmentOf
    00070     {
    00071       static const unsigned int value = Implementation::AlignmentOfImpl<T>::value;
    00072     };
    00073
    00078     template<typename T, unsigned int Alignment>
    00079     struct AlignSize
    00080     {
    00081       static const unsigned int value = (sizeof(T) + (Alignment - 1)) & ~(Alignment-1);
    00082     };
    00083
    00087     template<size_t Alignment>
    00088     struct TypeWithAlignment
    00089     {/* No default... */};
    00090
    00091     template<> struct TypeWithAlignment<1> { typedef Implementation::Aligned1 Type; };
    00092     template<> struct TypeWithAlignment<2> { typedef Implementation::Aligned2 Type; };
    00093     template<> struct TypeWithAlignment<4> { typedef Implementation::Aligned4 Type; };
    00094     template<> struct TypeWithAlignment<8> { typedef Implementation::Aligned8 Type; };
    00095     template<> struct TypeWithAlignment<16> { typedef Implementation::Aligned16 Type; };
    00096
    00100     template<size_t Size>
    00101     struct TypeOfSize
    00102     { /*No default...*/ };
    00103
    00104     template<> struct TypeOfSize<1> { typedef uint8 Type; };
    00105
    00106     template<> struct TypeOfSize<2> { typedef uint16 Type; };
    00107
    00108     template<> struct TypeOfSize<3> { typedef uint32 Type; };
    00109     template<> struct TypeOfSize<4> { typedef uint32 Type; };
    00110
    00111     template<> struct TypeOfSize<5> { typedef uint64 Type; };
    00112     template<> struct TypeOfSize<6> { typedef uint64 Type; };
    00113     template<> struct TypeOfSize<7> { typedef uint64 Type; };
    00114     template<> struct TypeOfSize<8> { typedef uint64 Type; };
    00115
    00116
    00118     template<size_t R>
    00119     struct Log2
    00120     {
    00121       static const unsigned int RShift = R >> 1;
    00122       static const unsigned int value = Log2<RShift>::value + 1;
    00123     };
    00124
    00125     template<>
    00126     struct Log2<1>
    00127     {
    00128       static const unsigned int value = 0;
    00129     };
    00130
    00131     template<>
    00132     struct Log2<0>
    00133     {
    00134       static const unsigned int value = 0;
    00135     };
    00136
    00138     template<size_t R>
    00139     struct IsLog2
    00140     {
    00141       static const bool value = (!(R & (R - 1)) && R);
    00142     };
    00143
    00145     template<typename T>
    00146     struct EBOptHelper : public T
    00147     {};
    00148
    00149     template<>
    00150     struct EBOptHelper<void>
    00151     {};
    00152   }
    00153 }
    00154
    00155 #endif