Allow instantiating TagValue from TimeSpan and DateTime

This commit is contained in:
Martchus 2019-06-01 22:54:44 +02:00
parent f042d216fd
commit c28ded1bca
2 changed files with 20 additions and 1 deletions

View File

@ -76,6 +76,8 @@ public:
TagValue(std::unique_ptr<char[]> &&data, std::size_t length, TagDataType type = TagDataType::Binary,
TagTextEncoding encoding = TagTextEncoding::Latin1);
TagValue(PositionInSet value);
TagValue(ChronoUtilities::DateTime value);
TagValue(ChronoUtilities::TimeSpan value);
TagValue(const TagValue &other);
TagValue(TagValue &&other) = default;
~TagValue();
@ -277,13 +279,28 @@ inline TagValue::TagValue(std::unique_ptr<char[]> &&data, std::size_t length, Ta
/*!
* \brief Constructs a new TagValue holding a copy of the given PositionInSet \a value.
* \param value Specifies the PositionInSet.
*/
inline TagValue::TagValue(PositionInSet value)
: TagValue(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::PositionInSet)
{
}
/*!
* \brief Constructs a new TagValue holding a copy of the given DateTime \a value.
*/
inline TagValue::TagValue(ChronoUtilities::DateTime value)
: TagValue(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::DateTime)
{
}
/*!
* \brief Constructs a new TagValue holding a copy of the given TimeSpan \a value.
*/
inline TagValue::TagValue(ChronoUtilities::TimeSpan value)
: TagValue(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::TimeSpan)
{
}
/*!
* \brief Returns whether both instances are not equal.
* \remarks Simply the negation of operator==() so check there for details.

View File

@ -128,6 +128,7 @@ void TagValueTests::testTimeSpan()
const TimeSpan fiveMinutes(TimeSpan::fromMinutes(5));
TagValue timeSpan;
timeSpan.assignTimeSpan(fiveMinutes);
CPPUNIT_ASSERT_EQUAL(timeSpan, TagValue(timeSpan));
CPPUNIT_ASSERT_EQUAL(fiveMinutes, timeSpan.toTimeSpan());
CPPUNIT_ASSERT_EQUAL(fiveMinutes.toString(), timeSpan.toString());
CPPUNIT_ASSERT_THROW(timeSpan.toInteger(), ConversionException);
@ -140,6 +141,7 @@ void TagValueTests::testDateTime()
const DateTime now(DateTime::now());
TagValue dateTime;
dateTime.assignDateTime(now);
CPPUNIT_ASSERT_EQUAL(dateTime, TagValue(dateTime));
CPPUNIT_ASSERT_EQUAL(now, dateTime.toDateTime());
CPPUNIT_ASSERT_EQUAL(now.toString(), dateTime.toString());
CPPUNIT_ASSERT_THROW(dateTime.toInteger(), ConversionException);