// // 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 == type::POSITIVE_INTEGER) { if(o.via.u64 > (uint64_t)std::numeric_limits::max()) { throw type_error(); } return o.via.u64; } else if(o.type == 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 == 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(packer& o, T v) { o.pack_int8(v); } }; template struct pack_integer_size_sign { static inline void pack(packer& o, T v) { o.pack_uint8(v); } }; template struct pack_integer_size_sign { static inline void pack(packer& o, T v) { 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(packer& o, T v) { 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(packer& o, T v) { 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(packer& o, T v) { 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(packer& o, T v) { 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(packer& o, T v) { 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(o, v); } } // namespace detail } // namespace type inline signed char& operator>> (object o, signed char& v) { v = type::detail::convert_integer(o); return v; } inline signed short& operator>> (object o, signed short& v) { v = type::detail::convert_integer(o); return v; } inline signed int& operator>> (object o, signed int& v) { v = type::detail::convert_integer(o); return v; } inline signed long& operator>> (object o, signed long& v) { v = type::detail::convert_integer(o); return v; } inline signed long long& operator>> (object o, signed long long& v) { v = type::detail::convert_integer(o); return v; } inline unsigned char& operator>> (object o, unsigned char& v) { v = type::detail::convert_integer(o); return v; } inline unsigned short& operator>> (object o, unsigned short& v) { v = type::detail::convert_integer(o); return v; } inline unsigned int& operator>> (object o, unsigned int& v) { v = type::detail::convert_integer(o); return v; } inline unsigned long& operator>> (object o, unsigned long& v) { v = type::detail::convert_integer(o); return v; } inline unsigned long long& operator>> (object o, unsigned long long& v) { v = type::detail::convert_integer(o); return v; } template inline packer& operator<< (packer& o, const signed char& v) { type::detail::pack_integer(v, o); return o; } template inline packer& operator<< (packer& o, const signed short& v) { type::detail::pack_integer(v, o); return o; } template inline packer& operator<< (packer& o, const signed int& v) { type::detail::pack_integer(v, o); return o; } template inline packer& operator<< (packer& o, const signed long& v) { type::detail::pack_integer(v, o); return o; } template inline packer& operator<< (packer& o, const signed long long& v) { type::detail::pack_integer(v, o); return o; } template inline packer& operator<< (packer& o, const unsigned char& v) { type::detail::pack_integer(v, o); return o; } template inline packer& operator<< (packer& o, const unsigned short& v) { type::detail::pack_integer(v, o); return o; } template inline packer& operator<< (packer& o, const unsigned int& v) { type::detail::pack_integer(v, o); return o; } template inline packer& operator<< (packer& o, const unsigned long& v) { type::detail::pack_integer(v, o); return o; } template inline packer& operator<< (packer& o, const unsigned long long& v) { type::detail::pack_integer(v, o); return o; } } // namespace msgpack #endif /* msgpack/type/integer.hpp */