Skip to content

Commit

Permalink
#4748 - Make BCLFileReference sortable, by defining all comparison op…
Browse files Browse the repository at this point in the history
…erators for BCLFileReference
  • Loading branch information
jmarrec committed Mar 17, 2023
1 parent 2e12f6a commit 47698fb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/utilities/bcl/BCLFileReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ bool BCLFileReference::checkForUpdate() {
return false;
}

bool operator==(const BCLFileReference& lhs, const BCLFileReference& rhs) {
return lhs.m_path == rhs.m_path;
}

std::strong_ordering operator<=>(const BCLFileReference& lhs, const BCLFileReference& rhs) {
if (lhs.m_path < rhs.m_path) {
return std::strong_ordering::less;
} else if (lhs.m_path == rhs.m_path) {
return std::strong_ordering::equal;
}
return std::strong_ordering::greater;
}

std::ostream& operator<<(std::ostream& os, const BCLFileReference& file) {
pugi::xml_document doc;
auto element = doc.append_child("File");
Expand Down
5 changes: 5 additions & 0 deletions src/utilities/bcl/BCLFileReference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "../core/Compare.hpp"
#include "../UtilitiesAPI.hpp"

#include <compare>
#include <vector>

namespace pugi {
Expand Down Expand Up @@ -122,6 +123,10 @@ class UTILITIES_API BCLFileReference

//@}

protected:
friend bool operator==(const BCLFileReference& lhs, const BCLFileReference& rhs);
friend std::strong_ordering operator<=>(const BCLFileReference& lhs, const BCLFileReference& rhs);

private:
// configure logging
REGISTER_LOGGER("utilities.bcl.BCLFileReference");
Expand Down
10 changes: 8 additions & 2 deletions src/utilities/bcl/BCLXML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
#include "../time/DateTime.hpp"
#include "../xml/XMLValidator.hpp"

#include <sstream>
#include <pugixml.hpp>

#include <algorithm>
#include <sstream>

namespace openstudio {

BCLXML::BCLXML(const BCLXMLType& bclXMLType)
Expand Down Expand Up @@ -734,7 +736,11 @@ pugi::xml_document BCLXML::toXML() const {

// write files
element = docElement.append_child("files");
for (const BCLFileReference& file : m_files) {

// Fix for #4748 - sort files
auto sortedFiles = m_files;
std::sort(sortedFiles.begin(), sortedFiles.end());
for (const BCLFileReference& file : sortedFiles) {
auto subelement = element.append_child("file");
file.writeValues(subelement);
}
Expand Down

0 comments on commit 47698fb

Please sign in to comment.