From c28ded1bca67a5e0076fd6df8cf530bf725e4cb2 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 1 Jun 2019 22:54:44 +0200 Subject: [PATCH] Allow instantiating TagValue from TimeSpan and DateTime --- tagvalue.h | 19 ++++++++++++++++++- tests/tagvalue.cpp | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tagvalue.h b/tagvalue.h index 916fad5..fc3097d 100644 --- a/tagvalue.h +++ b/tagvalue.h @@ -76,6 +76,8 @@ public: TagValue(std::unique_ptr &&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 &&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(&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(&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(&value), sizeof(value), TagDataType::TimeSpan) +{ +} + /*! * \brief Returns whether both instances are not equal. * \remarks Simply the negation of operator==() so check there for details. diff --git a/tests/tagvalue.cpp b/tests/tagvalue.cpp index 746096d..bca3228 100644 --- a/tests/tagvalue.cpp +++ b/tests/tagvalue.cpp @@ -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);