Skip to content

Commit

Permalink
[pnnl#187] Additional encode/decode functions
Browse files Browse the repository at this point in the history
  • Loading branch information
VitoCastellana authored and mfbalin committed Jun 23, 2021
1 parent 7804027 commit 8eb1b97
Showing 1 changed file with 85 additions and 37 deletions.
122 changes: 85 additions & 37 deletions include/shad/extensions/data_types/data_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ namespace data_types {
template <typename ENC_t, typename IN_t>
ENC_t encode(IN_t &in);

template <typename ENC_t, typename IN_t>
ENC_t encode(IN_t &in, data_t dt);

template <typename ENC_t, size_t MAX_s, data_t ST>
std::array<ENC_t, MAX_s> encode(std::string &str) {
std::array<ENC_t, MAX_s> res;
Expand All @@ -116,7 +119,7 @@ namespace data_types {
}

template <typename ENC_t, typename DEC_t>
typename std::enable_if<std::is_arithmetic<DEC_t>::value, DEC_t>::type
typename std::enable_if<(std::is_arithmetic<DEC_t>::value or (sizeof(DEC_t) == sizeof(ENC_t))), DEC_t>::type
decode(ENC_t encvalue) {
DEC_t val;
memcpy(&val, &encvalue, sizeof(DEC_t));
Expand All @@ -129,16 +132,38 @@ namespace data_types {
template <typename ENC_t, data_t ST>
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<ENC_t, int64_t>(encvalue);
}

// template <typename ENC_t, typename DEC_t, data_t ST>
// DEC_t decode(ENC_t value);

// template <typename ENC_t, typename DEC_t>
// DEC_t decode (ENC_t value);
template <typename ENC_t, data_t ST>
typename std::enable_if<(ST==data_t::UINT), uint64_t>::type
decode(ENC_t encvalue) {
return decode<ENC_t, uint64_t>(encvalue);
}

template <typename ENC_t, data_t ST>
typename std::enable_if<(ST==data_t::FLOAT), float>::type
decode(ENC_t encvalue) {
return decode<ENC_t, float>(encvalue);
}

template <typename ENC_t, data_t ST>
typename std::enable_if<(ST==data_t::DOUBLE), double>::type
decode(ENC_t encvalue) {
return decode<ENC_t, double>(encvalue);
}

template <typename ENC_t, data_t ST>
typename std::enable_if<(ST==data_t::BOOL), bool>::type
decode(ENC_t encvalue) {
return decode<ENC_t, bool>(encvalue);
}

template <typename ENC_t, data_t ST>
typename std::enable_if<(ST==data_t::DATE), std::time_t>::type
decode(ENC_t encvalue) {
return decode<ENC_t, std::time_t>(encvalue);
}

template <typename ENC_t, size_t MAX_s, data_t ST>
std::string decode(std::array<ENC_t, MAX_s> &val) {
Expand Down Expand Up @@ -295,6 +320,35 @@ uint64_t data_types::encode<uint64_t,
return value;
}

template <typename ENC_t, typename IN_t>
ENC_t data_types::encode(IN_t &in, data_types::data_t dt) {
switch (dt) {
// case data_types::STRING :
// return data_types::encode<ENC_t, IN_t, data_types::STRING>(in);
// case data_types::CHARS :
// return data_types::encode<ENC_t, IN_t, data_types::CHARS>(in);
case data_types::UINT :
return data_types::encode<ENC_t, IN_t, data_types::UINT>(in);
case data_types::INT :
return data_types::encode<ENC_t, IN_t, data_types::INT>(in);
case data_types::FLOAT :
return data_types::encode<ENC_t, IN_t, data_types::FLOAT>(in);
case data_types::DOUBLE :
return data_types::encode<ENC_t, IN_t, data_types::DOUBLE>(in);
case data_types::BOOL :
return data_types::encode<ENC_t, IN_t, data_types::BOOL>(in);
case data_types::DATE :
return data_types::encode<ENC_t, IN_t, data_types::DATE>(in);
case data_types::USDATE :
return data_types::encode<ENC_t, IN_t, data_types::USDATE>(in);
case data_types::DATE_TIME :
return data_types::encode<ENC_t, IN_t, data_types::DATE_TIME>(in);
case data_types::IP_ADDRESS :
return data_types::encode<ENC_t, IN_t, data_types::IP_ADDRESS>(in);
}
return data_types::kNullValue<ENC_t>;
}

template<>
std::string data_types::decode<uint64_t,
std::string,
Expand Down Expand Up @@ -333,6 +387,17 @@ std::string data_types::decode<uint64_t,
return std::to_string(v);
}

template<>
std::string data_types::decode<uint64_t,
std::string,
data_types::IP_ADDRESS>(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<uint64_t,
std::string,
Expand All @@ -341,6 +406,16 @@ std::string data_types::decode<uint64_t,
return std::to_string(value);
}

template<>
std::string data_types::decode<uint64_t,
std::string,
data_types::DATE>(uint64_t value) {
time_t t = data_types::decode<uint64_t, data_types::DATE>(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,
std::string,
Expand All @@ -355,35 +430,8 @@ uint64_t data_types::decode<uint64_t, uint64_t>(uint64_t encvalue) {
}


// template <>
// int64_t data_types::decode<uint64_t, int64_t>(uint64_t encvalue) {
// int64_t val;
// memcpy(&val, &encvalue, sizeof(val));
// return val;
// }

// template <>
// float data_types::decode<uint64_t, float>(uint64_t encvalue) {
// float val;
// memcpy(&val, &encvalue, sizeof(val));
// return val;
// }

// template <>
// double data_types::decode<uint64_t, double>(uint64_t encvalue) {
// double val;
// memcpy(&val, &encvalue, sizeof(val));
// return val;
// }

// template <>
// bool data_types::decode<uint64_t, bool>(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_

0 comments on commit 8eb1b97

Please sign in to comment.