C++序列化库的实现(4)

template<typename param_type,int NUM>
    _My& operator &(const param_type (&param)[NUM])
    {
        oarchive_param_deliverer<storage_class,_storage_type> param_helper;
        param_helper.m_boi = this;
        param_helper._storage = basic_archive<storage_class>::_storage;
        param_helper._storage_pos = &(basic_archive<storage_class>::_storage_pos);

oarchive_impl_helper<param_type (&)[NUM],storage_class,_storage_type> helper(param_helper,param);
        return (*this);
    }

template<typename param_type>
    _My& operator <<(const param_type& param)
    {
        (*this)&param;
        return (*this);
    }

template<typename param_type,int NUM>
    _My& operator <<(const param_type (&param)[NUM])
    {
        (*this)&param;
        return (*this);
    }
};


NAMESPACE_SERIALIZATION_END
#endif

// from_archive.hpp

#ifndef FROM_ARCHIVE_INCLUDE
#define FROM_ARCHIVE_INCLUDE

#include "archive_config.hpp"
#include "converter/lexical_cast.hpp"
#include "converter/codecvt.hpp"

NAMESPACE_SERIALIZATION_BEGIN

template<storage_type::VEHICLE_TYPE _type,typename storage_class,typename param_type> struct From_Archive_Helper;

template<storage_type::VEHICLE_TYPE _type,typename storage_class,typename param_type>
struct From_Archive_Helper{};

/// istring
template<typename storage_class,typename param_type>
struct From_Archive_Helper<storage_type::_ISTRING_TYPE,storage_class,param_type>
{
    From_Archive_Helper(storage_class& storage,param_type& param,storagepos& pos)
    {
        const char* pstorage = storage.c_str();
        pstorage += (int)pos;
        converter::lexical_cast<param_type> cast(param,pstorage);
        pos +=(storagepos)(cast.length + 1);
    }
};

/// istring
template<typename storage_class>
struct From_Archive_Helper<storage_type::_ISTRING_TYPE,storage_class,std::string>
{
    From_Archive_Helper(storage_class& storage,std::string& param,storagepos& pos)
    {
        const char* pstorage = storage.c_str();
        pstorage += (int)pos;

// 取大小
        unsigned int size = 0;
        converter::lexical_cast<unsigned int> cast(size,pstorage);
        pos +=(storagepos)(cast.length + 1);

// 取数据
        pstorage = storage.c_str();
        pstorage += (int)pos;
        param.append(pstorage,size);
        pos +=(storagepos)(size+1);
    }
};

/// istring
template<typename storage_class>
struct From_Archive_Helper<storage_type::_ISTRING_TYPE,storage_class,std::wstring>
{
    From_Archive_Helper(storage_class& storage,std::wstring& param,storagepos& pos)
    {
        std::string new_str;
        From_Archive_Helper<storage_type::_ISTRING_TYPE,storage_class,std::string>(storage,new_str,pos);
        converter::multi_to_utf16(param,new_str);
    }
};

/// istring
template<typename storage_class>
struct From_Archive_Helper<storage_type::_ISTRING_TYPE,storage_class,wchar_t>
{
    From_Archive_Helper(storage_class& storage,wchar_t& param,storagepos& pos)
    {
        char c;
        From_Archive_Helper<storage_type::_ISTRING_TYPE,storage_class,char>(storage,c,pos);

// char 转 wchar_t
        std::string str;
        str.push_back(c);

std::wstring wstr;
        converter::multi_to_utf16(wstr,str);
        param = wstr.c_str()[0];
    }
};

/// iwstring
template<typename storage_class,typename param_type>
struct From_Archive_Helper<storage_type::_WISTRING_TYPE,storage_class,param_type>
{
    From_Archive_Helper(storage_class& storage,param_type& param,storagepos& pos)
    {
        const wchar_t* pstorage = storage.c_str();
        pstorage += (int)pos;
        converter::lexical_cast<param_type> cast(param,pstorage);
        pos +=(storagepos)(cast.length + 1);
    }
};

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

转载注明出处:https://www.heiqu.com/4027141282656c44471a7b6eeb1dc451.html