Skip to content

Commit

Permalink
Add operator<< for CG types
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=260009
rdar://113667751

Reviewed by Tim Horton.

Instead of having to use `NSStringFromCG*`, now we can just stream CG types directly.

* Source/WTF/wtf/text/TextStream.h:
* Source/WTF/wtf/text/cocoa/TextStreamCocoa.mm:
(WTF::TextStream::operator<<):
* Tools/TestWebKitAPI/Tests/WTF/cocoa/TextStreamCocoa.mm:
(TEST):

Canonical link: https://commits.webkit.org/266782@main
  • Loading branch information
rr-codes committed Aug 10, 2023
1 parent 457eb42 commit 10e44f9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Source/WTF/wtf/text/TextStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class TextStream {
WTF_EXPORT_PRIVATE TextStream& operator<<(id);
#ifdef __OBJC__
WTF_EXPORT_PRIVATE TextStream& operator<<(NSArray *);
WTF_EXPORT_PRIVATE TextStream& operator<<(CGRect);
WTF_EXPORT_PRIVATE TextStream& operator<<(CGSize);
WTF_EXPORT_PRIVATE TextStream& operator<<(CGPoint);
#endif
#endif

Expand Down
18 changes: 18 additions & 0 deletions Source/WTF/wtf/text/cocoa/TextStreamCocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,22 @@
return *this << "]";
}

TextStream& TextStream::operator<<(CGRect rect)
{
*this << "{{" << rect.origin.x << ", " << rect.origin.y << "}, {" << rect.size.width << ", " << rect.size.height << "}}";
return *this;
}

TextStream& TextStream::operator<<(CGSize size)
{
*this << "{" << size.width << ", " << size.height << "}";
return *this;
}

TextStream& TextStream::operator<<(CGPoint point)
{
*this << "{" << point.x << ", " << point.y << "}";
return *this;
}

}
19 changes: 19 additions & 0 deletions Tools/TestWebKitAPI/Tests/WTF/cocoa/TextStreamCocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,22 @@
ts << value;
EXPECT_EQ(ts.release(), "3"_s);
}

TEST(WTF_TextStream, CoreGraphics)
{
{
TextStream ts;
ts << CGRectMake(1, 2, 3, 4);
EXPECT_EQ(ts.release(), "{{1.00, 2.00}, {3.00, 4.00}}"_s);
}
{
TextStream ts;
ts << CGPointMake(1, 2);
EXPECT_EQ(ts.release(), "{1.00, 2.00}"_s);
}
{
TextStream ts;
ts << CGSizeMake(3, 4);
EXPECT_EQ(ts.release(), "{3.00, 4.00}"_s);
}
}

0 comments on commit 10e44f9

Please sign in to comment.