// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // * Neither the name of the Andrey N. Sabelnikov nor the // names of its contributors may be used to endorse or promote products // derived from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // #pragma once #include "misc_language.h" #include "portable_storage_base.h" #include "warnings.h" namespace epee { namespace serialization { #define ASSERT_AND_THROW_WRONG_CONVERSION() ASSERT_MES_AND_THROW("WRONG DATA CONVERSION: from type=" << typeid(from).name() << " to type " << typeid(to).name()) template void convert_int_to_uint(const from_type& from, to_type& to) { PUSH_WARNINGS DISABLE_VS_WARNINGS(4018) CHECK_AND_ASSERT_THROW_MES(from >=0, "unexpected int value with signed storage value less than 0, and unsigned receiver value"); DISABLE_GCC_AND_CLANG_WARNING(sign-compare) CHECK_AND_ASSERT_THROW_MES(from <= std::numeric_limits::max(), "int value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with max possible value = " << std::numeric_limits::max()); to = static_cast(from); POP_WARNINGS } template void convert_int_to_int(const from_type& from, to_type& to) { CHECK_AND_ASSERT_THROW_MES(from >= boost::numeric::bounds::lowest(), "int value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with lowest possible value = " << boost::numeric::bounds::lowest()); PUSH_WARNINGS DISABLE_CLANG_WARNING(tautological-constant-out-of-range-compare) CHECK_AND_ASSERT_THROW_MES(from <= std::numeric_limits::max(), "int value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with max possible value = " << std::numeric_limits::max()); POP_WARNINGS to = static_cast(from); } template void convert_uint_to_any_int(const from_type& from, to_type& to) { PUSH_WARNINGS DISABLE_VS_WARNINGS(4018) DISABLE_CLANG_WARNING(tautological-constant-out-of-range-compare) CHECK_AND_ASSERT_THROW_MES(from <= std::numeric_limits::max(), "uint value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with max possible value = " << std::numeric_limits::max()); to = static_cast(from); POP_WARNINGS } template //is from signed, is from to signed struct convert_to_signed_unsigned; template struct convert_to_signed_unsigned { static void convert(const from_type& from, to_type& to) {//from signed to signed convert_int_to_int(from, to); } }; template struct convert_to_signed_unsigned { static void convert(const from_type& from, to_type& to) {//from signed to unsigned convert_int_to_uint(from, to); } }; template struct convert_to_signed_unsigned { static void convert(const from_type& from, to_type& to) {//from unsigned to signed convert_uint_to_any_int(from, to); } }; template struct convert_to_signed_unsigned { static void convert(const from_type& from, to_type& to) { //from unsigned to unsigned convert_uint_to_any_int(from, to); } }; template struct convert_to_integral; template struct convert_to_integral { static void convert(const from_type& from, to_type& to) { convert_to_signed_unsigned::value, std::is_signed::value>::convert(from, to); } }; template struct convert_to_integral { static void convert(const from_type& from, to_type& to) { ASSERT_AND_THROW_WRONG_CONVERSION(); } }; template struct is_convertable: std::integral_constant::value && std::is_integral::value && !std::is_same::value && !std::is_same::value > {}; template struct convert_to_same; template struct convert_to_same { static void convert(const from_type& from, to_type& to) { to = from; } }; template struct convert_to_same { static void convert(const from_type& from, to_type& to) { convert_to_integral::value>::convert(from, to);// ASSERT_AND_THROW_WRONG_CONVERSION(); } }; template void convert_t(const from_type& from, to_type& to) { convert_to_same::value>::convert(from, to); } } }