diff --git a/six/modules/c++/cphd/include/cphd/CPHDXMLControl.h b/six/modules/c++/cphd/include/cphd/CPHDXMLControl.h index f3ef81da7..2a6b619cd 100644 --- a/six/modules/c++/cphd/include/cphd/CPHDXMLControl.h +++ b/six/modules/c++/cphd/include/cphd/CPHDXMLControl.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -145,6 +146,7 @@ class CPHDXMLControl //! \return Suported version to uri mapping static std::unordered_map getVersionUriMap(); + static void getVersionUriMap(std::map&); protected: logging::Logger *mLog = nullptr; @@ -179,7 +181,7 @@ class CPHDXMLControl getParser(const xml::lite::Uri&) const; // Given the URI get associated version - std::string uriToVersion(const xml::lite::Uri&) const; + Version uriToVersion(const xml::lite::Uri&) const; }; } diff --git a/six/modules/c++/cphd/include/cphd/FileHeader.h b/six/modules/c++/cphd/include/cphd/FileHeader.h index e678635f8..09f4c776e 100644 --- a/six/modules/c++/cphd/include/cphd/FileHeader.h +++ b/six/modules/c++/cphd/include/cphd/FileHeader.h @@ -28,6 +28,7 @@ #include #include +#include namespace cphd { @@ -36,10 +37,10 @@ namespace cphd * * \brief Stores CPHD file header information */ -class FileHeader : public BaseFileHeader +class FileHeader final : public BaseFileHeader { public: - static const char DEFAULT_VERSION[]; + static const std::string DEFAULT_VERSION; /* * \func FileHeader @@ -96,6 +97,7 @@ class FileHeader : public BaseFileHeader * */ void setVersion(const std::string& version); + void setVersion(Version); /* * \func set diff --git a/six/modules/c++/cphd/include/cphd/Metadata.h b/six/modules/c++/cphd/include/cphd/Metadata.h index b7de4ea0a..1cca99a41 100644 --- a/six/modules/c++/cphd/include/cphd/Metadata.h +++ b/six/modules/c++/cphd/include/cphd/Metadata.h @@ -80,9 +80,11 @@ struct Metadata final : MetadataBase //! Get CPHD version std::string getVersion() const; + void getVersion(Version&) const; //! Set CPHD version void setVersion(const std::string& version); + void setVersion(Version); //! CollectionInfo block. Contains the general collection information //! CPHD can use the SICD Collection Information block directly @@ -152,7 +154,7 @@ struct Metadata final : MetadataBase private: //! Stores file Version - std::string mVersion; + Version mVersion; }; //! Ostream operator diff --git a/six/modules/c++/cphd/include/cphd/Types.h b/six/modules/c++/cphd/include/cphd/Types.h index 6eab11477..a86a7b005 100644 --- a/six/modules/c++/cphd/include/cphd/Types.h +++ b/six/modules/c++/cphd/include/cphd/Types.h @@ -91,6 +91,15 @@ typedef six::CollectionInformation CollectionInformation; typedef six::GeoInfo GeoInfo; typedef six::MatchInformation MatchInformation; + +enum class Version +{ + v100, // {"1.0.0", xml::lite::Uri("urn:CPHD:1.0.0")}, + v101, // {"1.0.1", xml::lite::Uri("http://api.nsgreg.nga.mil/schema/cphd/1.0.1")}, + v110, // {"1.1.0", xml::lite::Uri("http://api.nsgreg.nga.mil/schema/cphd/1.1.0")} +}; +std::string to_string(Version); // "1.0.0", "1.0.1", "1.1.0" + } diff --git a/six/modules/c++/cphd/source/CPHDWriter.cpp b/six/modules/c++/cphd/source/CPHDWriter.cpp index 4d8ad2d9a..aafbe4ad8 100644 --- a/six/modules/c++/cphd/source/CPHDWriter.cpp +++ b/six/modules/c++/cphd/source/CPHDWriter.cpp @@ -76,8 +76,9 @@ void CPHDWriter::writeMetadata(size_t supportSize, { const auto xmlMetadata(CPHDXMLControl().toXMLString(mMetadata, mSchemaPaths)); - // update header version, or remains default if unset - mHeader.setVersion(mMetadata.getVersion()); + Version cphdVersion; + mMetadata.getVersion(cphdVersion); + mHeader.setVersion(cphdVersion); // update classification and release info if (!six::Init::isUndefined( diff --git a/six/modules/c++/cphd/source/CPHDXMLControl.cpp b/six/modules/c++/cphd/source/CPHDXMLControl.cpp index 9aa6bff7b..c25350aa8 100644 --- a/six/modules/c++/cphd/source/CPHDXMLControl.cpp +++ b/six/modules/c++/cphd/source/CPHDXMLControl.cpp @@ -24,8 +24,9 @@ #include #include #include -#include +#include #include +#include #include #include @@ -103,21 +104,47 @@ std::unique_ptr CPHDXMLControl::toXML( return doc; } -std::unordered_map CPHDXMLControl::getVersionUriMap() +static std::unordered_map makeVersionUriMap_() { - return { - {"1.0.0", xml::lite::Uri("urn:CPHD:1.0.0")}, - {"1.0.1", xml::lite::Uri("http://api.nsgreg.nga.mil/schema/cphd/1.0.1")}, - {"1.1.0", xml::lite::Uri("http://api.nsgreg.nga.mil/schema/cphd/1.1.0")} + std::map result; + CPHDXMLControl::getVersionUriMap(result); + + std::unordered_map retval; + for (const auto& version_and_uri : result) + { + retval[to_string(version_and_uri.first)] = version_and_uri.second; + } + return retval; +} +std::unordered_map CPHDXMLControl::getVersionUriMap() // for existing code +{ + static const auto retval = makeVersionUriMap_(); + return retval; +} +static std::map getVersionUriMap_() +{ + static const std::map retval = { + {Version::v100, xml::lite::Uri("urn:CPHD:1.0.0")}, + {Version::v101, xml::lite::Uri("http://api.nsgreg.nga.mil/schema/cphd/1.0.1")}, + {Version::v110, xml::lite::Uri("http://api.nsgreg.nga.mil/schema/cphd/1.1.0")} }; + return retval; +} +void CPHDXMLControl::getVersionUriMap(std::map& result) +{ + result = getVersionUriMap_(); } std::unique_ptr CPHDXMLControl::toXMLImpl(const Metadata& metadata) { - const auto versionUriMap = getVersionUriMap(); - if (versionUriMap.find(metadata.getVersion()) != versionUriMap.end()) + Version cphdVersion; + metadata.getVersion(cphdVersion); + + static const auto versionUriMap = getVersionUriMap_(); + const auto it = versionUriMap.find(cphdVersion); + if (it != versionUriMap.end()) { - return getParser(versionUriMap.find(metadata.getVersion())->second)->toXML(metadata); + return getParser(it->second)->toXML(metadata); } std::ostringstream ostr; ostr << "The version " << metadata.getVersion() << " is invalid. " @@ -180,9 +207,9 @@ CPHDXMLControl::getParser(const xml::lite::Uri& uri) const return std::make_unique(uri.value, false, mLog); } -std::string CPHDXMLControl::uriToVersion(const xml::lite::Uri& uri) const +Version CPHDXMLControl::uriToVersion(const xml::lite::Uri& uri) const { - const auto versionUriMap = getVersionUriMap(); + static const auto versionUriMap = getVersionUriMap_(); for (const auto& p : versionUriMap) { if (p.second == uri) @@ -198,3 +225,16 @@ std::string CPHDXMLControl::uriToVersion(const xml::lite::Uri& uri) const } } + +std::string cphd::to_string(Version siddVersion) +{ + // Match existing version strings, see CPHDXMLControl::getVersionUriMap + switch (siddVersion) + { + case Version::v100: return "1.0.0"; + case Version::v101: return "1.0.1"; + case Version::v110: return "1.1.0"; + default: break; + } + throw std::logic_error("Unkown 'Version' value."); +} \ No newline at end of file diff --git a/six/modules/c++/cphd/source/FileHeader.cpp b/six/modules/c++/cphd/source/FileHeader.cpp index 501bca29c..db2a5a89a 100644 --- a/six/modules/c++/cphd/source/FileHeader.cpp +++ b/six/modules/c++/cphd/source/FileHeader.cpp @@ -27,10 +27,11 @@ #include #include #include +#include namespace cphd { -const char FileHeader::DEFAULT_VERSION[] = "1.0.1"; +const std::string FileHeader::DEFAULT_VERSION = to_string(Version::v101); FileHeader::FileHeader() : mVersion(DEFAULT_VERSION), @@ -181,6 +182,10 @@ void FileHeader::setVersion(const std::string& version) { mVersion = version; } +void FileHeader::setVersion(Version version) +{ + setVersion(to_string(version)); +} size_t FileHeader::set(int64_t xmlBlockSize, int64_t supportBlockSize, diff --git a/six/modules/c++/cphd/source/Metadata.cpp b/six/modules/c++/cphd/source/Metadata.cpp index 271a5ae9a..8b79b5835 100644 --- a/six/modules/c++/cphd/source/Metadata.cpp +++ b/six/modules/c++/cphd/source/Metadata.cpp @@ -21,13 +21,15 @@ */ #include +#include + namespace cphd { Metadata::Metadata() { // Default version defined in cphd::FileHeader - mVersion = FileHeader::DEFAULT_VERSION; + setVersion(FileHeader::DEFAULT_VERSION); } size_t Metadata::getNumChannels() const @@ -67,9 +69,33 @@ DomainType Metadata::getDomainType() const std::string Metadata::getVersion() const { - return mVersion; + return to_string(mVersion); +} +void Metadata::getVersion(Version& version) const +{ + version = mVersion; } + void Metadata::setVersion(const std::string& version) +{ + if (version == "1.0.0") + { + setVersion(Version::v100); + } + else if (version == "1.0.1") + { + setVersion(Version::v101); + } + else if (version == "1.1.0") + { + setVersion(Version::v110); + } + else + { + throw std::invalid_argument("Unknown version string: " + version); + } +} +void Metadata::setVersion(Version version) { mVersion = version; }