summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-01-05 20:50:40 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-01-05 20:50:40 +0000
commit8d7aa70fb0dd37bd969cdd3a7fc6a5d969e6b398 (patch)
tree5e89f26ed76cf4c3f628f97bce3791af62faaf72
parentTypedef conversion list (diff)
downloadslicer-8d7aa70fb0dd37bd969cdd3a7fc6a5d969e6b398.tar.bz2
slicer-8d7aa70fb0dd37bd969cdd3a7fc6a5d969e6b398.tar.xz
slicer-8d7aa70fb0dd37bd969cdd3a7fc6a5d969e6b398.zip
Allow conversions to be specified on a class/struct
-rw-r--r--slicer/slicer/parser.cpp15
-rw-r--r--slicer/slicer/parser.h1
-rw-r--r--slicer/test/conversions.cpp27
-rw-r--r--slicer/test/initial/conv-datetime.xml1
-rw-r--r--slicer/test/preprocessor.cpp2
-rw-r--r--slicer/test/types.ice7
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<std::string> & 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<std::string> & metadata) const;
+ static Conversions getAllConversions(Slice::DataMemberPtr dm);
static Conversions getConversions(const std::list<std::string> & 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
@@ -17,6 +17,33 @@ namespace Slicer {
}
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)
{
struct tm tm;
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 @@
<?xml version="1.0" encoding="UTF-8"?>
<DateTimeContainer>
<dt>2014-Dec-25 12:13:14</dt>
+ <date>2014-06-26</date>
</DateTimeContainer>
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;