Version FC
Dependencies: DmTftLibrary eeprom SX1280Lib filesystem mbed
Fork of MSNV2-Terminal_V1-5 by
Value.cpp
- Committer:
- patrick_duc
- Date:
- 2018-09-05
- Revision:
- 22:a5929d3668b6
- Parent:
- 21:8524d815c587
- Child:
- 24:92c30dabfda4
File content as of revision 22:a5929d3668b6:
#include "Value.hpp" using namespace misnet; void Value::setValue(Value::VALUE_TYPE type, Value::GENERIC_VALUE value) { this->_value.type = type; switch (type) { case Value::BOOLEAN: this->_value.value.bool_value = value.value.bool_value; break; case Value::CHAR: this->_value.value.char_value = value.value.char_value; break; case Value::UINT8_T: this->_value.value.uint8_value = value.value.uint8_value; break; case Value::INT8_T: this->_value.value.int8_value = value.value.int8_value; break; case Value::UINT16_T: this->_value.value.uint16_value = value.value.uint16_value; break; case Value::INT16_T: this->_value.value.int16_value = value.value.int16_value; break; case Value::UINT32_T: this->_value.value.uint32_value = value.value.uint32_value; break; case Value::INT32_T: this->_value.value.int32_value = value.value.int32_value; break; case Value::FLOAT: this->_value.value.float_value = value.value.float_value; break; case Value::DOUBLE: this->_value.value.double_value = value.value.double_value; break; case Value::TIME: this->_value.value.time_value = value.value.time_value; break; default: break; } } int8_t Value::compareTo(Value& otherValue) { if (this->_value.type != otherValue._value.type) { return -2; } switch (this->_value.type) { case Value::BOOLEAN: if (this->_value.value.bool_value == otherValue._value.value.bool_value) { return 0; } else if ((this->_value.value.bool_value == true) && (otherValue._value.value.bool_value == false)) { return 1; } return -1; break; case Value::CHAR: if (this->_value.value.char_value == otherValue._value.value.char_value) { return 0; } else if (this->_value.value.char_value > otherValue._value.value.char_value) { return 1; } return -1; break; case Value::UINT8_T: if (this->_value.value.uint8_value == otherValue._value.value.uint8_value) { return 0; } else if (this->_value.value.uint8_value > otherValue._value.value.uint8_value) { return 1; } return -1; break; case Value::INT8_T: if (this->_value.value.int8_value == otherValue._value.value.int8_value) { return 0; } else if (this->_value.value.int8_value > otherValue._value.value.int8_value) { return 1; } return -1; break; case Value::UINT16_T: if (this->_value.value.uint16_value == otherValue._value.value.uint16_value) { return 0; } else if (this->_value.value.uint16_value > otherValue._value.value.uint16_value) { return 1; } return -1; break; case Value::INT16_T: if (this->_value.value.int16_value == otherValue._value.value.int16_value) { return 0; } else if (this->_value.value.int16_value > otherValue._value.value.int16_value) { return 1; } return -1; break; case Value::UINT32_T: if (this->_value.value.uint32_value == otherValue._value.value.uint32_value) { return 0; } else if (this->_value.value.uint32_value > otherValue._value.value.uint32_value) { return 1; } return -1; break; case Value::INT32_T: if (this->_value.value.int32_value == otherValue._value.value.int32_value) { return 0; } else if (this->_value.value.int32_value > otherValue._value.value.int32_value) { return 1; } return -1; break; case Value::FLOAT: if (this->_value.value.float_value == otherValue._value.value.float_value) { return 0; } else if (this->_value.value.float_value > otherValue._value.value.float_value) { return 1; } return -1; break; case Value::DOUBLE: if (this->_value.value.double_value == otherValue._value.value.double_value) { return 0; } else if (this->_value.value.double_value > otherValue._value.value.double_value) { return 1; } return -1; break; case Value::TIME: if (this->_value.value.time_value == otherValue._value.value.time_value) { return 0; } else if (this->_value.value.time_value > otherValue._value.value.time_value) { return 1; } return -1; break; default: return -2; break; } } std::string Value::getValueAsString() { /* DEBUG("ENTREE\n"); DEBUG("This = %p\n", this); */ std::ostringstream stringStream; switch (this->_value.type) { case Value::BOOLEAN: stringStream << (this->_value.value.bool_value ? "true" : "false"); break; case Value::CHAR: stringStream << this->_value.value.char_value; break; case Value::UINT8_T: #if defined(TEST_ENVIRONMENT) && not defined(CPLUSPLUS_99) stringStream << std::to_string(this->_value.value.uint8_value); #else stringStream << this->_value.value.uint8_value; #endif break; case Value::INT8_T: #if defined(TEST_ENVIRONMENT) && not defined(CPLUSPLUS_99) stringStream << std::to_string(this->_value.value.int8_value); #else stringStream << this->_value.value.int8_value; #endif break; case Value::UINT16_T: stringStream << this->_value.value.uint16_value; break; case Value::INT16_T: stringStream << this->_value.value.int16_value; break; case Value::UINT32_T: stringStream << this->_value.value.uint32_value; break; case Value::INT32_T: stringStream << this->_value.value.int32_value; break; case Value::FLOAT: stringStream << this->_value.value.float_value; break; case Value::DOUBLE: stringStream << this->_value.value.double_value; break; case Value::TIME: stringStream << this->_value.value.time_value; break; case Value::NOT_SET: default: stringStream << "Value not set !"; break; } return stringStream.str(); } Value Value::substract(Value& otherValue) { Value result; if (this->_value.type == Value::NOT_SET || otherValue._value.type == Value::NOT_SET) { result._value.type = Value::NOT_SET; return result; } if (this->_value.type != otherValue._value.type) { result._value.type = Value::NOT_SET; return result; } switch (this->_value.type) { case Value::BOOLEAN: result._value.type = Value::NOT_SET; return result; break; case Value::CHAR: result._value.value.char_value = this->_value.value.char_value - otherValue._value.value.char_value; result._value.type = Value::CHAR; break; case Value::UINT8_T: result._value.value.uint8_value = this->_value.value.uint8_value - otherValue._value.value.uint8_value; result._value.type = Value::UINT8_T; break; case Value::INT8_T: result._value.value.int8_value = this->_value.value.int8_value - otherValue._value.value.int8_value; result._value.type = Value::INT8_T; break; case Value::UINT16_T: result._value.value.uint16_value = this->_value.value.uint16_value - otherValue._value.value.uint16_value; result._value.type = Value::UINT16_T; break; case Value::INT16_T: result._value.value.int16_value = this->_value.value.int16_value - otherValue._value.value.int16_value; result._value.type = Value::INT16_T; break; case Value::UINT32_T: result._value.value.uint32_value = this->_value.value.uint32_value - otherValue._value.value.uint32_value; result._value.type = Value::UINT32_T; break; case Value::INT32_T: result._value.value.int32_value = this->_value.value.int32_value - otherValue._value.value.int32_value; result._value.type = Value::INT32_T; break; case Value::FLOAT: result._value.value.float_value = this->_value.value.float_value - otherValue._value.value.float_value; result._value.type = Value::FLOAT; break; case Value::DOUBLE: result._value.value.double_value = this->_value.value.double_value - otherValue._value.value.double_value; result._value.type = Value::DOUBLE; break; case Value::TIME: result._value.value.time_value = this->_value.value.time_value - otherValue._value.value.time_value; result._value.type = Value::TIME; break; default: result._value.type = Value::NOT_SET; break; } return result; } bool Value::isAbsoluteDifferenceValueGreaterThanDelta(Value& valueToSubstract, Value& delta) { if (((this->_value.type != valueToSubstract._value.type) || (this->_value.type != valueToSubstract._value.type)) || (this->_value.type == Value::NOT_SET)) { return true; } int16_t int16_result; int32_t int32_result; double double_result; switch (this->_value.type) { case Value::BOOLEAN: return false; break; case Value::CHAR: int16_result = (int16_t) this->_value.value.char_value - valueToSubstract._value.value.char_value; if (int16_result < 0) { int16_result = -int16_result; } return (int16_result > delta._value.value.char_value); break; case Value::UINT8_T: int16_result = (int16_t) this->_value.value.uint8_value - valueToSubstract._value.value.uint8_value; //DEBUG("First value = %d, second value = %d\n", this->_value.value.uint8_value, valueToSubstract._value.value.uint8_value); //DEBUG("Difference = %d\n", int16_result); if (int16_result < 0) { int16_result = -int16_result; } return (int16_result > delta._value.value.uint8_value); break; case Value::INT8_T: int16_result = this->_value.value.int8_value - valueToSubstract._value.value.int8_value; if (int16_result < 0) { int16_result = -int16_result; } return (int16_result > delta._value.value.int8_value); break; case Value::UINT16_T: int32_result = (int32_t) this->_value.value.uint16_value - valueToSubstract._value.value.uint16_value; if (int32_result < 0) { int32_result = -int32_result; } return (int32_result > delta._value.value.uint16_value); break; case Value::INT16_T: int32_result = (int32_t) this->_value.value.int16_value - valueToSubstract._value.value.int16_value; if (int32_result < 0) { int32_result = -int32_result; } return (int32_result > delta._value.value.int16_value); break; case Value::UINT32_T: double_result = (double) this->_value.value.uint32_value - valueToSubstract._value.value.uint32_value; if (double_result < 0) { double_result = -double_result; } return (double_result > delta._value.value.uint32_value); break; case Value::INT32_T: double_result = this->_value.value.int32_value - valueToSubstract._value.value.int32_value; if (double_result < 0) { double_result = -double_result; } return (double_result > delta._value.value.int32_value); break; case Value::FLOAT: double_result = this->_value.value.float_value - valueToSubstract._value.value.float_value; if (double_result < 0) { double_result = -double_result; } return (double_result > delta._value.value.float_value); break; case Value::DOUBLE: double_result = this->_value.value.double_value - valueToSubstract._value.value.double_value; if (double_result < 0) { double_result = -double_result; } return (double_result > delta._value.value.double_value); break; case Value::TIME: double_result = (double) this->_value.value.time_value - valueToSubstract._value.value.time_value; if (double_result < 0) { double_result = -double_result; } return (double_result > delta._value.value.time_value); break; default: return true; break; } } bool Value::isDifferentFrom(Value& otherValue) { if (this->_value.type != otherValue._value.type) { return true; } switch (this->_value.type) { case Value::BOOLEAN: return (this->_value.value.bool_value != otherValue._value.value.bool_value); break; case Value::CHAR: return (this->_value.value.char_value != otherValue._value.value.char_value); break; case Value::UINT8_T: return (this->_value.value.uint8_value != otherValue._value.value.uint8_value); break; case Value::INT8_T: return (this->_value.value.int8_value != otherValue._value.value.int8_value); break; case Value::UINT16_T: return (this->_value.value.uint16_value != otherValue._value.value.uint16_value); break; case Value::INT16_T: return (this->_value.value.int16_value != otherValue._value.value.int16_value); break; case Value::UINT32_T: return (this->_value.value.uint32_value != otherValue._value.value.uint32_value); break; case Value::INT32_T: return (this->_value.value.int32_value != otherValue._value.value.int32_value); break; case Value::FLOAT: return (this->_value.value.float_value == otherValue._value.value.float_value); break; case Value::DOUBLE: return (this->_value.value.double_value != otherValue._value.value.double_value); break; case Value::TIME: return (this->_value.value.time_value != otherValue._value.value.time_value); break; default: return true; break; } }