C++类型萃取编写技巧(3)

template<typename T>
struct type_expand_traits<const T*>
{
    typedef T* value_type;
    typedef T*& reference_type;
    typedef T** pointer_type;
    typedef const T* const_value_type;
    typedef const T*& const_reference_type;
    typedef const T** const_pointer_type;
};

template<typename T>
struct type_expand_traits<T**>
{
    typedef T** value_type;
    typedef const T** const_value_type;
};

template<typename T>
struct type_expand_traits<const T**>
{
    typedef T** value_type;
    typedef const T** const_value_type;
};

template<typename T>
struct type_expand_traits<T&>
{
    typedef T value_type;
    typedef T& reference_type;
    typedef T* pointer_type;
    typedef const T const_value_type;
    typedef const T& const_reference_type;
    typedef const T* const_pointer_type;
};

template<typename T>
struct type_expand_traits<const T&>
{
    typedef T value_type;
    typedef T& reference_type;
    typedef T* pointer_type;
    typedef const T const_value_type;
    typedef const T& const_reference_type;
    typedef const T* const_pointer_type;
};

template<typename T>
struct type_expand_traits<T*&>
{
    typedef T* value_type;
    typedef T*& reference_type;
    typedef T** pointer_type;
    typedef const T* const_value_type;
    typedef const T*& const_reference_type;
    typedef const T** const_pointer_type;
};

template<typename T>
struct type_expand_traits<const T*&>
{
    typedef T* value_type;
    typedef T*& reference_type;
    typedef T** pointer_type;
    typedef const T* const_value_type;
    typedef const T*& const_reference_type;
    typedef const T** const_pointer_type;
};

NAMESPACE_TRAITS_END
#endif

########################


########################
//pointer_integer_traits.hpp

/*
函数地址与整数之间的转换
*/
#ifndef POINTER_INTEGER_TRAITS_INCLUDE
#define POINTER_INTEGER_TRAITS_INCLUDE

#include "traits_config.hpp"

NAMESPACE_TRAITS_BEGIN

// 指针与整数的转换
template<typename funp>
struct _pointer_integer_traits
{
protected:
    funp _fun;
    _pointer_integer_traits(unsigned int addr)
    {
        union
        {
            funp _fun;
            unsigned int _addr;
        }_u;
        _u._addr = addr;
        _fun = _u._fun;
    }
public:
    operator funp()
    {
        return _fun;
    }
};


template<>
struct _pointer_integer_traits<unsigned int>
{
protected:
    unsigned int _addr;
    template<typename funp>
    _pointer_integer_traits(funp fun)
    {
        union
        {
            funp _fun;
            unsigned int _addr;
        }_u;
        _u._fun = fun;
        _addr = _u._addr;
    }
public:
    operator unsigned int()
    {
        return _addr;
    }
};

template<typename funp>
struct pointer_integer_traits : public _pointer_integer_traits<funp>
{
    pointer_integer_traits(const unsigned int addr):_pointer_integer_traits<funp>(addr){}
};

template<typename funp>
struct pointer_integer_traits<const funp*> : public _pointer_integer_traits<funp>
{
    pointer_integer_traits(const unsigned int addr):_pointer_integer_traits<funp>(addr){}
};

template<>
struct pointer_integer_traits<int> : public _pointer_integer_traits<unsigned int>
{
    template<typename funp>
    pointer_integer_traits(funp fun):_pointer_integer_traits<unsigned int>(fun){}

template<typename funp>
    pointer_integer_traits(const funp* fun):_pointer_integer_traits<unsigned int>(fun){}
};

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/4b11dba9eca97ff5bf235e221cf6c9e1.html