// // MessagePack for C++ static resolution routine // // 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 MSGPACK_TYPE_INTEGER_HPP__ #define MSGPACK_TYPE_INTEGER_HPP__ #include "msgpack/object.hpp" #include namespace msgpack { namespace type { namespace detail { template struct convert_integer_sign; template struct convert_integer_sign { static inline T convert(object o) { if(o.type == POSITIVE_INTEGER) { if(o.via.u64 > (uint64_t)std::numeric_limits::max()) { throw type_error(); } return o.via.u64; } else if(o.type == NEGATIVE_INTEGER) { if(o.via.i64 < (int64_t)-std::numeric_limits::max()) { throw type_error(); } return o.via.i64; } throw type_error(); } }; template struct convert_integer_sign { static inline T convert(object o) { if(o.type == POSITIVE_INTEGER) { if(o.via.u64 > (uint64_t)std::numeric_limits::max()) { throw type_error(); } return o.via.u64; } throw type_error(); } }; template static inline T convert_integer(object o) { return detail::convert_integer_sign::is_signed>::convert(o); } template struct pack_integer_size_sign; template struct pack_integer_size_sign { static inline void pack(T v, packer& o) { o.pack_int8(v); } }; template struct pack_integer_size_sign { static inline void pack(T v, packer& o) { o.pack_uint8(v); } }; template struct pack_integer_size_sign { static inline void pack(T v, packer& o) { if( (int16_t)v <= (int16_t)std::numeric_limits::max() && (int16_t)v >= (int16_t)std::numeric_limits::min()) { o.pack_int8(v); } else { o.pack_int16(v); } } }; template struct pack_integer_size_sign { static inline void pack(T v, packer& o) { if( (uint16_t)v <= (uint16_t)std::numeric_limits::max()) { o.pack_uint8(v); } else { o.pack_uint16(v); } } }; template struct pack_integer_size_sign { static inline void pack(T v, packer& o) { if( (int32_t)v <= (int32_t)std::numeric_limits::max() && (int32_t)v >= (int32_t)std::numeric_limits::min()) { o.pack_int8(v); } else if( (int32_t)v <= (int32_t)std::numeric_limits::max() && (int32_t)v >= (int32_t)std::numeric_limits::min()) { o.pack_int16(v); } else { o.pack_int32(v); } } }; template struct pack_integer_size_sign { static inline void pack(T v, packer& o) { if( (uint32_t)v <= (uint32_t)std::numeric_limits::max()) { o.pack_uint8(v); } else if( (uint32_t)v <= (uint32_t)std::numeric_limits::max()) { o.pack_uint16(v); } else { o.pack_uint32(v); } } }; template struct pack_integer_size_sign { static inline void pack(T v, packer& o) { if( (int64_t)v <= (int64_t)std::numeric_limits::max() && (int64_t)v >= (int64_t)std::numeric_limits::min()) { o.pack_int8(v); } else if( (int64_t)v <= (int64_t)std::numeric_limits::max() && (int64_t)v >= (int64_t)std::numeric_limits::min()) { o.pack_int16(v); } else if( (int64_t)v <= (int64_t)std::numeric_limits::max() && (int64_t)v >= (int64_t)std::numeric_limits::min()) { o.pack_int32(v); } else { o.pack_int64(v); } } }; template struct pack_integer_size_sign { static inline void pack(T v, packer& o) { if( (uint64_t)v <= (uint64_t)std::numeric_limits::max()) { o.pack_uint8(v); } else if( (uint64_t)v <= (uint64_t)std::numeric_limits::max()) { o.pack_uint16(v); } else if( (uint64_t)v <= (uint64_t)std::numeric_limits::max()) { o.pack_uint32(v); } else { o.pack_uint64(v); } } }; template static inline void pack_integer(T v, packer& o) { pack_integer_size_sign::is_signed>::pack(v, o); } } // namespace detail inline signed char& operator<< (signed char& v, object o) { v = detail::convert_integer(o); return v; } inline signed short& operator<< (signed short& v, object o) { v = detail::convert_integer(o); return v; } inline signed int& operator<< (signed int& v, object o) { v = detail::convert_integer(o); return v; } inline signed long& operator<< (signed long& v, object o) { v = detail::convert_integer(o); return v; } inline signed long long& operator<< (signed long long& v, object o) { v = detail::convert_integer(o); return v; } inline unsigned char& operator<< (unsigned char& v, object o) { v = detail::convert_integer(o); return v; } inline unsigned short& operator<< (unsigned short& v, object o) { v = detail::convert_integer(o); return v; } inline unsigned int& operator<< (unsigned int& v, object o) { v = detail::convert_integer(o); return v; } inline unsigned long& operator<< (unsigned long& v, object o) { v = detail::convert_integer(o); return v; } inline unsigned long long& operator<< (unsigned long long& v, object o) { v = detail::convert_integer(o); return v; } template inline const signed char& operator>> (const signed char& v, packer o) { detail::pack_integer(v, o); return v; } template inline const signed short& operator>> (const signed short& v, packer o) { detail::pack_integer(v, o); return v; } template inline const signed int& operator>> (const signed int& v, packer o) { detail::pack_integer(v, o); return v; } template inline const signed long& operator>> (const signed long& v, packer o) { detail::pack_integer(v, o); return v; } template inline const signed long long& operator>> (const signed long long& v, packer o) { detail::pack_integer(v, o); return v; } template inline const unsigned char& operator>> (const unsigned char& v, packer o) { detail::pack_integer(v, o); return v; } template inline const unsigned short& operator>> (const unsigned short& v, packer o) { detail::pack_integer(v, o); return v; } template inline const unsigned int& operator>> (const unsigned int& v, packer o) { detail::pack_integer(v, o); return v; } template inline const unsigned long& operator>> (const unsigned long& v, packer o) { detail::pack_integer(v, o); return v; } template inline const unsigned long long& operator>> (const unsigned long long& v, packer o) { detail::pack_integer(v, o); return v; } } // namespace type } // namespace msgpack #endif /* msgpack/type/integer.hpp */