// // mp::serialize // // Copyright (C) 2008 FURUHASHI Sadayuki // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #ifndef MP_SERIALIZE_H__ #define MP_SERIALIZE_H__ namespace mp { namespace detail { #ifdef _BIG_ENDIAN #define MP_SERIALIZE_htonll(x) (x) #define MP_SERIALIZE_ntohll(x) (x) #else #define MP_SERIALIZE_htonll(x) ( (((uint64_t)htonl(x)) << 32) + htonl(x >> 32) ) #define MP_SERIALIZE_ntohll(x) ( (((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32) ) #endif } template T serialize(T h); template <> inline uint8_t serialize(uint8_t h) { return h; } template <> inline uint16_t serialize(uint16_t h) { return htons(h); } template <> inline uint32_t serialize(uint32_t h) { return htonl(h); } template <> inline uint64_t serialize(uint64_t h) { return MP_SERIALIZE_htonll(h); } template <> inline int8_t serialize(int8_t h) { return h; } template <> inline int16_t serialize(int16_t h) { return htons(h); } template <> inline int32_t serialize(int32_t h) { return htonl(h); } template <> inline int64_t serialize(int64_t h) { return MP_SERIALIZE_htonll(h); } template <> inline char serialize(char h) { return h; } template T deserialize(T n); template <> inline uint8_t deserialize(uint8_t n) { return n; } template <> inline uint16_t deserialize(uint16_t n) { return ntohs(n); } template <> inline uint32_t deserialize(uint32_t n) { return ntohl(n); } template <> inline uint64_t deserialize(uint64_t h) { return MP_SERIALIZE_ntohll(h); } template <> inline int8_t deserialize(int8_t n) { return n; } template <> inline int16_t deserialize(int16_t n) { return ntohs(n); } template <> inline int32_t deserialize(int32_t n) { return ntohl(n); } template <> inline int64_t deserialize(int64_t h) { return MP_SERIALIZE_ntohll(h); } template <> inline char deserialize(char n) { return n; } MP_ARGS_BEGIN template void* dump(void* buffer, MP_ARGS_PARAMS) { char* b = reinterpret_cast(buffer); MP_ARGS_ITERATOR_BEGIN *reinterpret_cast(b) = serialize(MP_ARGS_ITERATOR_PARAM); b += sizeof(MP_ARGS_ITERATOR_TYPE); MP_ARGS_ITERATOR_END return b; } MP_ARGS_END MP_ARGS_BEGIN template const void* load(const void* buffer, MP_ARGS_PARAMS_PTR) { const char* b = reinterpret_cast(buffer); MP_ARGS_ITERATOR_BEGIN *MP_ARGS_ITERATOR_PARAM = deserialize( *reinterpret_cast(b) ); b += sizeof(MP_ARGS_ITERATOR_TYPE); MP_ARGS_ITERATOR_END return b; } MP_ARGS_END } // namespace mp #endif /* mp/serialize.h */