From 8d7aa70fb0dd37bd969cdd3a7fc6a5d969e6b398 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 5 Jan 2015 20:50:40 +0000 Subject: Allow conversions to be specified on a class/struct --- slicer/slicer/parser.cpp | 15 +++++++++++++-- slicer/slicer/parser.h | 1 + slicer/test/conversions.cpp | 27 +++++++++++++++++++++++++++ slicer/test/initial/conv-datetime.xml | 1 + slicer/test/preprocessor.cpp | 2 +- slicer/test/types.ice | 7 +++++++ 6 files changed, 50 insertions(+), 3 deletions(-) diff --git a/slicer/slicer/parser.cpp b/slicer/slicer/parser.cpp index d84b248..cd11bff 100644 --- a/slicer/slicer/parser.cpp +++ b/slicer/slicer/parser.cpp @@ -29,7 +29,7 @@ namespace Slicer { auto type = dm->type(); auto c = Slice::ContainedPtr::dynamicCast(dm->container()); - auto conversions = getConversions(dm->getMetaData()); + auto conversions = getAllConversions(dm); for (const auto & conversion : conversions) { fprintf(cpp, "%s %s(const %s &);\n", conversion.ExchangeType.c_str(), @@ -223,7 +223,7 @@ namespace Slicer { t = Slice::ClassDefPtr::dynamicCast(dm->container())->declaration(); } auto name = metaDataValue("slicer:name:", dm->getMetaData()); - auto conversions = metaDataValues("slicer:conversion:", dm->getMetaData()); + auto conversions = getAllConversions(dm); fprintf(cpp, "\t\tnew "); auto type = dm->type(); createNewModelPartPtrFor(t); @@ -427,6 +427,17 @@ namespace Slicer { fprintf(cpp, "};\n\n"); } + Slicer::Conversions + Slicer::getAllConversions(Slice::DataMemberPtr dm) + { + auto conversions = getConversions(dm->getMetaData()); + auto typec = Slice::ContainedPtr::dynamicCast(dm->type()); + if (typec) { + auto typeConversions = getConversions(typec->getMetaData()); + std::copy(typeConversions.begin(), typeConversions.end(), std::back_inserter(conversions)); + } + return conversions; + } Slicer::Conversions Slicer::getConversions(const std::list & dm) diff --git a/slicer/slicer/parser.h b/slicer/slicer/parser.h index 9d880d8..7965b4b 100644 --- a/slicer/slicer/parser.h +++ b/slicer/slicer/parser.h @@ -49,6 +49,7 @@ namespace Slicer { void defineConversions(Slice::DataMemberPtr dm) const; void copyMetadata(const std::list & metadata) const; + static Conversions getAllConversions(Slice::DataMemberPtr dm); static Conversions getConversions(const std::list & metadata); unsigned int components; diff --git a/slicer/test/conversions.cpp b/slicer/test/conversions.cpp index 552135c..b09be27 100644 --- a/slicer/test/conversions.cpp +++ b/slicer/test/conversions.cpp @@ -16,6 +16,33 @@ namespace Slicer { throw std::runtime_error("Not implemented"); } + std::string + isoDateToString(const ::TestModule::IsoDate & in) + { + struct tm tm; + memset(&tm, 0, sizeof(struct tm)); + tm.tm_mday = in.day; + tm.tm_mon = in.month; + tm.tm_year = in.year; + mktime(&tm); + char buf[BUFSIZ]; + auto len = strftime(buf, BUFSIZ, "%Y-%m-%d", &tm); + return std::string(buf, len); + } + + ::TestModule::IsoDate + stringToIsoDate(const std::string & in) + { + struct tm tm; + memset(&tm, 0, sizeof(struct tm)); + auto end = strptime(in.c_str(), "%Y-%m-%d", &tm); + if (!end || *end) { + throw std::runtime_error("Invalid iso-date string: " + in); + } + return ::TestModule::IsoDate({ + SHORT(tm.tm_year), SHORT(tm.tm_mon), SHORT(tm.tm_mday)}); + } + std::string dateTimeToString(const ::TestModule::DateTime & in) { diff --git a/slicer/test/initial/conv-datetime.xml b/slicer/test/initial/conv-datetime.xml index b4678b4..17cd6be 100644 --- a/slicer/test/initial/conv-datetime.xml +++ b/slicer/test/initial/conv-datetime.xml @@ -1,4 +1,5 @@
2014-Dec-25 12:13:14
+ 2014-06-26
diff --git a/slicer/test/preprocessor.cpp b/slicer/test/preprocessor.cpp index 245d742..54099df 100644 --- a/slicer/test/preprocessor.cpp +++ b/slicer/test/preprocessor.cpp @@ -13,7 +13,7 @@ namespace fs = boost::filesystem; -const unsigned int COMPONENTS_IN_TEST_ICE = 25; +const unsigned int COMPONENTS_IN_TEST_ICE = 26; BOOST_FIXTURE_TEST_SUITE ( preprocessor, FileStructure ); diff --git a/slicer/test/types.ice b/slicer/test/types.ice index 62037a1..6339cd7 100644 --- a/slicer/test/types.ice +++ b/slicer/test/types.ice @@ -9,10 +9,17 @@ module TestModule { short minute; short second; }; + [ "slicer:conversion:std.string:stringToIsoDate:isoDateToString" ] + struct IsoDate { + short year; + short month; + short day; + }; class DateTimeContainer { [ "slicer:conversion:boost.posix_time.ptime:ptimeToDateTime:dateTimeToPTime", "slicer:conversion:std.string:stringToDateTime:dateTimeToString" ] DateTime dt; + IsoDate date; }; class BuiltIns { bool mbool; -- cgit v1.2.3