aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-10-14 22:48:16 +0800
committercrupest <crupest@outlook.com>2023-10-14 22:48:16 +0800
commit856df14d749014f11a583ade2fee988b076754cc (patch)
tree5e0684b2a4c01f37ddc6ad869c2c86125e4bb77a
parent9ec7c364ce8681305910b728588913f2b11cfbe6 (diff)
downloadcru-856df14d749014f11a583ade2fee988b076754cc.tar.gz
cru-856df14d749014f11a583ade2fee988b076754cc.tar.bz2
cru-856df14d749014f11a583ade2fee988b076754cc.zip
Re-think about ToString.
ToString for a class should lies in the same namespace of the class. Argument-dependent name lookup will help make Format work. The problem is it will hide ToString at parent namespace. But if you call ToString explicitly, it's you duty to use the ToString in correct namespace.
-rw-r--r--include/cru/platform/Color.h12
-rw-r--r--include/cru/platform/GraphicsBase.h23
-rw-r--r--include/cru/platform/graphics/quartz/Brush.h2
-rw-r--r--src/platform/Color.cpp4
-rw-r--r--src/platform/graphics/SvgGeometryBuilderMixin.cpp6
5 files changed, 20 insertions, 27 deletions
diff --git a/include/cru/platform/Color.h b/include/cru/platform/Color.h
index 0d7bce5b..2b38138a 100644
--- a/include/cru/platform/Color.h
+++ b/include/cru/platform/Color.h
@@ -257,6 +257,11 @@ extern const std::unordered_map<StringView, Color> predefined_name_color_map;
std::optional<Color> GetPredefinedColorByName(StringView name);
+inline String ToString(const Color& color) {
+ return cru::Format(u"rgba({}, {}, {}, {})", color.red, color.green,
+ color.blue, color.alpha);
+}
+
struct CRU_PLATFORM_API HslColor {
HslColor() = default;
HslColor(float h, float s, float l, float a = 1.0f)
@@ -271,10 +276,3 @@ struct CRU_PLATFORM_API HslColor {
float a;
};
} // namespace cru::platform
-
-namespace cru {
-inline String ToString(const platform::Color& color) {
- return cru::Format(u"rgba({}, {}, {}, {})", color.red, color.green,
- color.blue, color.alpha);
-}
-} // namespace cru
diff --git a/include/cru/platform/GraphicsBase.h b/include/cru/platform/GraphicsBase.h
index 88dc6ee2..f134e74d 100644
--- a/include/cru/platform/GraphicsBase.h
+++ b/include/cru/platform/GraphicsBase.h
@@ -5,7 +5,11 @@
#include "cru/common/Range.h"
#include "cru/common/String.h"
+#include <cstdint>
#include <limits>
+#include <optional>
+#include <string>
+#include <utility>
namespace cru::platform {
struct Size;
@@ -43,6 +47,10 @@ constexpr bool operator!=(const Point& left, const Point& right) {
return !(left == right);
}
+inline String ToString(const Point& point) {
+ return Format(u"(x: {}, y: {})", point.x, point.y);
+}
+
struct CRU_PLATFORM_API Size final {
static const Size kMax;
static const Size kZero;
@@ -80,6 +88,10 @@ constexpr bool operator!=(const Size& left, const Size& right) {
return !(left == right);
}
+inline String ToString(const Size& size) {
+ return Format(u"(width: {}, height: {})", size.width, size.height);
+}
+
struct Thickness final {
constexpr Thickness() : Thickness(0) {}
@@ -278,14 +290,3 @@ constexpr bool operator!=(const Ellipse& left, const Ellipse& right) {
using TextRange = Range;
} // namespace cru::platform
-
-namespace cru {
-
-inline String ToString(const platform::Point& point) {
- return Format(u"(x: {}, y: {})", point.x, point.y);
-}
-inline String ToString(const platform::Size& size) {
- return Format(u"(width: {}, height: {})", size.width, size.height);
-}
-
-} // namespace cru
diff --git a/include/cru/platform/graphics/quartz/Brush.h b/include/cru/platform/graphics/quartz/Brush.h
index f3579771..d5714293 100644
--- a/include/cru/platform/graphics/quartz/Brush.h
+++ b/include/cru/platform/graphics/quartz/Brush.h
@@ -6,8 +6,6 @@
#include <CoreGraphics/CoreGraphics.h>
-#include <functional>
-
namespace cru::platform::graphics::quartz {
class QuartzBrush : public OsxQuartzResource, public virtual IBrush {
public:
diff --git a/src/platform/Color.cpp b/src/platform/Color.cpp
index 81709c6f..a35ef535 100644
--- a/src/platform/Color.cpp
+++ b/src/platform/Color.cpp
@@ -2,13 +2,9 @@
#include <gsl/gsl>
-#include <algorithm>
#include <cmath>
#include <cstdint>
#include <optional>
-#include <stdexcept>
-#include <string>
-#include <string_view>
namespace cru::platform {
String Color::ToString() const {
diff --git a/src/platform/graphics/SvgGeometryBuilderMixin.cpp b/src/platform/graphics/SvgGeometryBuilderMixin.cpp
index 1f868f44..3f8b48ad 100644
--- a/src/platform/graphics/SvgGeometryBuilderMixin.cpp
+++ b/src/platform/graphics/SvgGeometryBuilderMixin.cpp
@@ -108,14 +108,14 @@ void SvgGeometryBuilderMixin::Append(bool flag) {
}
void SvgGeometryBuilderMixin::Append(float number) {
- current_ += ToString(number);
+ current_ += cru::ToString(number);
current_ += u' ';
}
void SvgGeometryBuilderMixin::Append(const Point& point) {
- current_ += ToString(point.x);
+ current_ += cru::ToString(point.x);
current_ += u',';
- current_ += ToString(point.y);
+ current_ += cru::ToString(point.y);
current_ += u' ';
}
} // namespace cru::platform::graphics