From 3246d602c2db788588aa62b5c93dde8ea2059938 Mon Sep 17 00:00:00 2001 From: Andrii Tkachenko Date: Thu, 8 Feb 2018 08:05:41 +0100 Subject: [PATCH] xLnt. Implemented operator less for: rich_text, rich_text_run, color, font, optional. --- include/xlnt/cell/rich_text.hpp | 4 ++ include/xlnt/cell/rich_text_run.hpp | 2 + include/xlnt/styles/color.hpp | 13 +++-- include/xlnt/styles/font.hpp | 13 +++-- include/xlnt/utils/optional.hpp | 19 +++++-- source/cell/rich_text.cpp | 17 +++++++ source/cell/rich_text_run.cpp | 5 ++ source/styles/color.cpp | 28 ++++++++++ source/styles/font.cpp | 79 +++++++++++++++++++++++++++++ 9 files changed, 167 insertions(+), 13 deletions(-) diff --git a/include/xlnt/cell/rich_text.hpp b/include/xlnt/cell/rich_text.hpp index 546910aa..fa1cb486 100644 --- a/include/xlnt/cell/rich_text.hpp +++ b/include/xlnt/cell/rich_text.hpp @@ -119,6 +119,10 @@ public: /// bool operator!=(const std::string &rhs) const; + /// + /// Returns true if this text is less to rhs. + /// + bool operator<(const rich_text &rhs) const; private: /// /// The runs that make up this rich text. diff --git a/include/xlnt/cell/rich_text_run.hpp b/include/xlnt/cell/rich_text_run.hpp index 432097e0..c7c0c7fe 100644 --- a/include/xlnt/cell/rich_text_run.hpp +++ b/include/xlnt/cell/rich_text_run.hpp @@ -39,6 +39,8 @@ struct rich_text_run std::string first; optional second; + bool operator<(const rich_text_run &other) const; + bool operator==(const rich_text_run &other) const; bool operator!=(const rich_text_run &other) const; diff --git a/include/xlnt/styles/color.hpp b/include/xlnt/styles/color.hpp index 2905b2e1..8ff9895b 100644 --- a/include/xlnt/styles/color.hpp +++ b/include/xlnt/styles/color.hpp @@ -276,10 +276,15 @@ public: /// void tint(double tint); - /// - /// Returns true if this color is equivalent to other - /// - bool operator==(const color &other) const; + /// + /// Returns true if this color is less to other + /// + bool operator<(const color &other) const; + + /// + /// Returns true if this color is equivalent to other + /// + bool operator==(const color &other) const; /// /// Returns true if this color is not equivalent to other diff --git a/include/xlnt/styles/font.hpp b/include/xlnt/styles/font.hpp index 2cb9f42a..0f55a404 100644 --- a/include/xlnt/styles/font.hpp +++ b/include/xlnt/styles/font.hpp @@ -224,10 +224,15 @@ public: /// std::string scheme() const; - /// - /// Returns true if left is exactly equal to right. - /// - bool operator==(const font &other) const; + /// + /// Returns true if left is exactly equal to right. + /// + bool operator==(const font &other) const; + + /// + /// Returns true if left is less to right. + /// + bool operator<(const font &other) const; /// /// Returns true if left is not exactly equal to right. diff --git a/include/xlnt/utils/optional.hpp b/include/xlnt/utils/optional.hpp index 655923d5..e51e2501 100644 --- a/include/xlnt/utils/optional.hpp +++ b/include/xlnt/utils/optional.hpp @@ -123,11 +123,20 @@ public: /// or both have a value and those values are equal according to /// their equality operator. /// - bool operator==(const optional &other) const - { - return has_value_ == other.has_value_ - && (!has_value_ || (has_value_ && value_ == other.value_)); - } + bool operator==(const optional &other) const + { + return has_value_ == other.has_value_ + && (!has_value_ || (has_value_ && value_ == other.value_)); + } + + /// + /// Returns true if this is less to other + /// + bool operator<(const optional &other) const + { + return has_value_ < other.has_value_ + && (!has_value_ || (has_value_ && value_ < other.value_)); + } private: bool has_value_; diff --git a/source/cell/rich_text.cpp b/source/cell/rich_text.cpp index d8c3015e..11d2bca0 100644 --- a/source/cell/rich_text.cpp +++ b/source/cell/rich_text.cpp @@ -85,6 +85,23 @@ void rich_text::add_run(const rich_text_run &t) runs_.push_back(t); } +bool rich_text::operator<(const rich_text &rhs) const +{ + if (runs_.size() < rhs.runs_.size()) + return true; + + if (runs_.size() > rhs.runs_.size()) + return false; + + for (std::size_t i = 0; i < runs_.size(); i++) + { + if (runs_[i] != rhs.runs_[i]) + return runs_[i] < rhs.runs_[i]; + } + + return false; +} + bool rich_text::operator==(const rich_text &rhs) const { if (runs_.size() != rhs.runs_.size()) return false; diff --git a/source/cell/rich_text_run.cpp b/source/cell/rich_text_run.cpp index 79b2456d..00737fd4 100644 --- a/source/cell/rich_text_run.cpp +++ b/source/cell/rich_text_run.cpp @@ -25,6 +25,11 @@ namespace xlnt { +bool rich_text_run::operator<(const rich_text_run &other) const +{ + return first < other.first && second < other.second; +} + bool rich_text_run::operator==(const rich_text_run &other) const { return first == other.first && second == other.second; diff --git a/source/styles/color.cpp b/source/styles/color.cpp index f776c9df..585baba4 100644 --- a/source/styles/color.cpp +++ b/source/styles/color.cpp @@ -259,6 +259,34 @@ void color::assert_type(color_type t) const } } +bool color::operator<(const xlnt::color &other) const +{ + if (type_ != other.type_) + { + return type_ < other.type_; + } + + if (std::fabs(tint_ - other.tint_) != 0.0) + { + return tint_ < other.tint_; + } + + if (auto__ != other.auto__) + return auto__ < other.auto__; + + switch (type_) + { + case color_type::indexed: + return indexed_.index() < other.indexed_.index(); + case color_type::theme: + return theme_.index() < other.theme_.index(); + case color_type::rgb: + return rgb_.hex_string() < other.rgb_.hex_string(); + } + + return false; +} + bool color::operator==(const xlnt::color &other) const { if (type_ != other.type_ || std::fabs(tint_ - other.tint_) != 0.0 || auto__ != other.auto__) diff --git a/source/styles/font.cpp b/source/styles/font.cpp index 4d300835..f95f9cc4 100644 --- a/source/styles/font.cpp +++ b/source/styles/font.cpp @@ -286,4 +286,83 @@ bool font::operator==(const font &other) const return true; } +bool font::operator<(const font &other) const +{ + if (bold() != other.bold()) + { + return bold() < other.bold(); + } + + if (has_color() != other.has_color()) + { + return has_color() < other.has_color(); + } + + if (has_color()) + { + if (color() != other.color()) + { + return color() < other.color(); + } + } + + if (has_family() != other.has_family()) + { + return has_family() < other.has_family(); + } + + if (has_family()) + { + if (family() != other.family()) + { + return family() < other.family(); + } + } + + if (italic() != other.italic()) + { + return italic() < other.italic(); + } + + if (name() != other.name()) + { + return name() < other.name(); + } + + if (has_scheme() != other.has_scheme()) + { + return has_scheme() < other.has_scheme(); + } + + if (has_scheme()) + { + if (scheme() != other.scheme()) + { + return scheme() < other.scheme(); + } + } + + if (std::fabs(size() - other.size()) != 0.0) + { + return size() < other.size(); + } + + if (strikethrough() != other.strikethrough()) + { + return strikethrough() < other.strikethrough(); + } + + if (superscript() != other.superscript()) + { + return superscript() < other.superscript(); + } + + if (underline() != other.underline()) + { + return underline() < other.underline(); + } + + return false; +} + } // namespace xlnt