diff --git a/include/boost/astronomy/io/binary_data_converter.hpp b/include/boost/astronomy/io/binary_data_converter.hpp index 10466ce8..8eeebb35 100644 --- a/include/boost/astronomy/io/binary_data_converter.hpp +++ b/include/boost/astronomy/io/binary_data_converter.hpp @@ -12,6 +12,7 @@ file License.txt or copy at https://www.boost.org/LICENSE_1_0.txt) #include #include #include +#include #include #include #include @@ -53,7 +54,7 @@ namespace boost{namespace astronomy{namespace io{ * @brief Provides convenience methods for deserializing Binary data * @author Gopi Krishna Menon */ - struct binary_data_converter { + struct binary_data_converter { /** @@ -67,7 +68,7 @@ namespace boost{namespace astronomy{namespace io{ AssumeType temp = boost::endian::big_to_native( *reinterpret_cast(element.c_str())); - NumericType value; + NumericType value; std::memcpy(&value, &temp, sizeof(NumericType)); return value; } @@ -271,45 +272,52 @@ namespace boost{namespace astronomy{namespace io{ return values; } - template<> - boost::int16_t binary_data_converter::deserialize_to(const std::string& element, int) { - return element_to_numeric(element); - } + struct yes{char a;}; + struct no{char a[2];}; - template<> - std::vector binary_data_converter::deserialize_to(const std::string& elements, int num_elements) { - return elements_to_numeric_collection( - elements, num_elements); - } + template + constexpr yes is_non_primitive(typename T::value_type* x); - template<> - boost::int32_t binary_data_converter::deserialize_to(const std::string& element, int) { - return element_to_numeric(element); - } - template<> - std::vector binary_data_converter::deserialize_to(const std::string& elements, int num_elements) { - return binary_data_converter::elements_to_numeric_collection( - elements, num_elements); - } + template + constexpr no is_non_primitive(...); - template<> - boost::float32_t binary_data_converter::deserialize_to(const std::string& element,int) { - return element_to_numeric(element); - } - template<> - std::vector binary_data_converter::deserialize_to(const std::string& elements, int num_elements) { - return elements_to_numeric_collection( - elements, num_elements); - } - template<> - boost::float64_t binary_data_converter::deserialize_to(const std::string& element, int) { - return binary_data_converter::element_to_numeric(element); - } - template<> - std::vector binary_data_converter::deserialize_to(const std::string& elements, int num_elements) { - return binary_data_converter::elements_to_numeric_collection( - elements, num_elements); - } + template + constexpr bool is_vector(){ + return sizeof(is_non_primitive(0))== sizeof(yes); + } + + template + static T deserialize_to(const std::string& data, int) + { + return + boost::hana::eval_if(is_vector>() + [&data]{ return thunk1(boost::type{},data,size); } + [&data](auto _){ return thunk2(boost::type{},data); } + ); + + } + + template + static void thunk1(boost::type) + { + return + boost::hana::eval_if(std::numeric_limits::is_integer, + [&data]{ return elements_to_numeric_collection(data,size);} + [&data](auto _){ return elemets_to_numeric_collection(data,size); } + ); + } + + + template + static void thunk2(boost::type) + { + return + boost::hana::eval_if(std::numeric_limits::is_integer, + [&data]{ return element_to_numeric(data); } + [&data](auto _){ return elemet_to_numeric(data); } + ); + } + template<> std::pair binary_data_converter::deserialize_to(const std::string& element,int) { auto x = boost::endian::big_to_native(