⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vc_alignment.h

📁 loki库的源代码。loki库是以模板技术和面向对象技术为基础的c++类库。
💻 H
字号:
// Last update: June 20, 2001

#ifdef _MSC_VER
# pragma once
#else
# error "Visual C++ specific header !"
#endif

#ifndef VC_ALIGNMENT_INC_
#define VC_ALIGNMENT_INC_

#include "Typelist.h"
#include "static_check.h"

////////////////////////////////////////////////////////////////////////////////
// class template MaxAlign
// Computes the maximum alignof for all types in a typelist
// Usage: MaxAlign<TList>::result
////////////////////////////////////////////////////////////////////////////////

template <class TList> struct VC_MaxAlign;

template <> 
struct VC_MaxAlign< ::Loki::NullType >
{
    enum { result = 0 };
};

template <class TList>
struct VC_MaxAlign
{
private:
    ASSERT_TYPELIST(TList);

    typedef typename TList::Head Head;
    typedef typename TList::Tail Tail;

private:
    enum { headResult = __alignof(Head)        };
    enum { tailResult = VC_MaxAlign<Tail>::result };

public:
    enum { result = headResult > tailResult ? 
           headResult : tailResult };
};

                         
////////////////////////////////////////////////////////////////////////////////
// class VC_AlignedPODBase
// Defines a host of protected types used by VC_AlignedPOD (defined later)
// Could be just part of VC_AlignedPOD itself, but making it separate ought to 
// reduce compile times 
////////////////////////////////////////////////////////////////////////////////
class VC_AlignedPODBase
{
protected:

    template<unsigned AlignmentSize>
    struct AlignedPod
    {
        STATIC_CHECK(AlignmentSize == 0, BadAlignmentSize_OnlyUpTo128);
    };

//
// I used the macro because align(#) 
// only works with Integer literals
//
#define ALIGNED_POD(_size_)                                   \
template<> struct AlignedPod<_size_> {                        \
  __declspec(align(_size_)) struct type { char X[_size_]; };  \
  enum { alignment = __alignof(type) }; };                    \
STATIC_CHECK((_size_ == sizeof(AlignedPod<_size_>::type)), SizeofNotEqualSize); \
STATIC_CHECK((_size_ == (AlignedPod<_size_>::alignment)), SizeofNotEqualAlignof)


    ALIGNED_POD(1);
    ALIGNED_POD(2);
    ALIGNED_POD(4);
    ALIGNED_POD(8);
    ALIGNED_POD(16);
    ALIGNED_POD(32);
    ALIGNED_POD(64);
    ALIGNED_POD(128);
    // can be up to 8192 - is it realistic?

#undef ALIGNED_POD

};


////////////////////////////////////////////////////////////////////////////////
// class template VC_AlignedPOD
// Computes the alignment of all types in a typelist
////////////////////////////////////////////////////////////////////////////////

template <typename TList>
class VC_AlignedPOD : VC_AlignedPODBase
{
    enum { maxAlign = VC_MaxAlign<TList>::result };

public:
    typedef typename AlignedPod<maxAlign>::type Result;
};

#endif // VC_ALIGNMENT_INC_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -