1 Star 0 Fork 121

blog/kbengine

forked from likecg/kbengine 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
memorystream_converter.hpp 2.58 KB
一键复制 编辑 原始数据 按行查看 历史
liquidx 提交于 2012-12-19 17:27 +08:00 . 1:继续完善billing机制
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2012 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
/*
字节序交换处理模块:
由于网络通信一般采用BIG字节序\也叫做网络字节序.
我们使用的PC机或者嵌入式系统可能使用BIG字节序也可能使用LITTEN(小字节序)
所以我们必须在此之间做一个字节序的转换。
*/
#ifndef __MEMORYSTREAMCONVERTER_H__
#define __MEMORYSTREAMCONVERTER_H__
// common include
#include "cstdkbe/cstdkbe.hpp"
#include <algorithm>
// windows include
#if KBE_PLATFORM == PLATFORM_WIN32
#else
// linux include
#include <errno.h>
#endif
namespace KBEngine{
namespace MemoryStreamConverter
{
template<size_t T>
inline void convert(char *val)
{
std::swap(*val, *(val + T - 1));
convert<T - 2>(val + 1);
}
template<> inline void convert<0>(char *) {}
template<> inline void convert<1>(char *) {} // ignore central byte
template<typename T> inline void apply(T *val)
{
convert<sizeof(T)>((char *)(val));
}
inline void convert(char *val, size_t size)
{
if(size < 2)
return;
std::swap(*val, *(val + size - 1));
convert(val + 1, size - 2);
}
}
#if KBENGINE_ENDIAN == KBENGINE_BIG_ENDIAN // 可以使用sys.isPlatformLittleEndian() 进行测试
template<typename T> inline void EndianConvert(T& val) { MemoryStreamConverter::apply<T>(&val); }
template<typename T> inline void EndianConvertReverse(T&) { }
#else
template<typename T> inline void EndianConvert(T&) { }
template<typename T> inline void EndianConvertReverse(T& val) { MemoryStreamConverter::apply<T>(&val); }
#endif
template<typename T> void EndianConvert(T*); // will generate link error
template<typename T> void EndianConvertReverse(T*); // will generate link error
inline void EndianConvert(uint8&) { }
inline void EndianConvert(int8&) { }
inline void EndianConvertReverse(uint8&) { }
inline void EndianConvertReverse(int8&) { }
}
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/blog/kbengine.git
git@gitee.com:blog/kbengine.git
blog
kbengine
kbengine
a1b691f57f98fa0bff1bcfb0210ebd07e318c0cd

搜索帮助