diff --git a/include/shad/extensions/data_types/data_types.h b/include/shad/extensions/data_types/data_types.h index fd5f9b7f..33391148 100644 --- a/include/shad/extensions/data_types/data_types.h +++ b/include/shad/extensions/data_types/data_types.h @@ -104,6 +104,9 @@ namespace data_types { template ENC_t encode(IN_t &in); + template + ENC_t encode(IN_t &in, data_t dt); + template std::array encode(std::string &str) { std::array res; @@ -116,7 +119,7 @@ namespace data_types { } template - typename std::enable_if::value, DEC_t>::type + typename std::enable_if<(std::is_arithmetic::value or (sizeof(DEC_t) == sizeof(ENC_t))), DEC_t>::type decode(ENC_t encvalue) { DEC_t val; memcpy(&val, &encvalue, sizeof(DEC_t)); @@ -129,16 +132,38 @@ namespace data_types { template typename std::enable_if<(ST==data_t::INT), int64_t>::type decode(ENC_t encvalue) { - int64_t val; - memcpy(&val, &encvalue, sizeof(int64_t)); - return val; + return decode(encvalue); } - // template - // DEC_t decode(ENC_t value); - - // template - // DEC_t decode (ENC_t value); + template + typename std::enable_if<(ST==data_t::UINT), uint64_t>::type + decode(ENC_t encvalue) { + return decode(encvalue); + } + + template + typename std::enable_if<(ST==data_t::FLOAT), float>::type + decode(ENC_t encvalue) { + return decode(encvalue); + } + + template + typename std::enable_if<(ST==data_t::DOUBLE), double>::type + decode(ENC_t encvalue) { + return decode(encvalue); + } + + template + typename std::enable_if<(ST==data_t::BOOL), bool>::type + decode(ENC_t encvalue) { + return decode(encvalue); + } + + template + typename std::enable_if<(ST==data_t::DATE), std::time_t>::type + decode(ENC_t encvalue) { + return decode(encvalue); + } template std::string decode(std::array &val) { @@ -295,6 +320,35 @@ uint64_t data_types::encode +ENC_t data_types::encode(IN_t &in, data_types::data_t dt) { + switch (dt) { +// case data_types::STRING : +// return data_types::encode(in); +// case data_types::CHARS : +// return data_types::encode(in); + case data_types::UINT : + return data_types::encode(in); + case data_types::INT : + return data_types::encode(in); + case data_types::FLOAT : + return data_types::encode(in); + case data_types::DOUBLE : + return data_types::encode(in); + case data_types::BOOL : + return data_types::encode(in); + case data_types::DATE : + return data_types::encode(in); + case data_types::USDATE : + return data_types::encode(in); + case data_types::DATE_TIME : + return data_types::encode(in); + case data_types::IP_ADDRESS : + return data_types::encode(in); + } + return data_types::kNullValue; +} + template<> std::string data_types::decode +std::string data_types::decode(uint64_t value) { + std::string ipAddr = ""; + uint64_t octets[4]; + for (uint64_t k = 0; k < 4; k ++) {octets[k] = value & 255; value = value >> 8;} + for (uint64_t k = 3; k >= 1; k --) ipAddr += std::to_string(octets[k]) + '.'; + return ipAddr + std::to_string(octets[0]); +} + template<> std::string data_types::decode +std::string data_types::decode(uint64_t value) { + time_t t = data_types::decode(value); + char dateString[11]; + strftime(dateString, 11, "%Y-%m-%d", std::localtime(&t)); + return std::string(dateString); +} + template<> std::string data_types::decode(uint64_t encvalue) { } -// template <> -// int64_t data_types::decode(uint64_t encvalue) { -// int64_t val; -// memcpy(&val, &encvalue, sizeof(val)); -// return val; -// } - -// template <> -// float data_types::decode(uint64_t encvalue) { -// float val; -// memcpy(&val, &encvalue, sizeof(val)); -// return val; -// } - -// template <> -// double data_types::decode(uint64_t encvalue) { -// double val; -// memcpy(&val, &encvalue, sizeof(val)); -// return val; -// } - -// template <> -// bool data_types::decode(uint64_t encvalue) { -// float val; -// memcpy(&val, &encvalue, sizeof(val)); -// return val; -// } } // namespace shad -#endif // INCLUDE_SHAD_EXTENSIONS_DATA_TYPES_DATA_TYPES_H_ +#endif // INCLUDE_SHAD_EXTENSIONS_DATA_TYPES_DATA_TYPES_H_ \ No newline at end of file