From 2b0aa4245bc5e6b0a38dbb0fb31ccb56794d13ff Mon Sep 17 00:00:00 2001 From: X512 Date: Sun, 10 Nov 2024 18:08:43 +0900 Subject: [PATCH] BShape: use `BShape::Private` class to access private methods. Avoid declaring random friend classes in public header. Allow to access private methods from arbitrary source if needed. Change-Id: Iac2cf0ca59e483aa0657e3fe1fc47080c661cf8b Reviewed-on: https://review.haiku-os.org/c/haiku/+/8534 Reviewed-by: waddlesplash Tested-by: Commit checker robot --- headers/os/interface/Shape.h | 12 +++--------- headers/private/interface/ShapePrivate.h | 17 +++++++++++++++++ src/kits/app/ServerLink.cpp | 5 +++-- src/kits/interface/PicturePlayer.cpp | 5 +++-- src/kits/interface/Shape.cpp | 14 +++++++------- src/kits/interface/View.cpp | 8 ++++---- 6 files changed, 37 insertions(+), 24 deletions(-) diff --git a/headers/os/interface/Shape.h b/headers/os/interface/Shape.h index faa58f12d9..389c8bcbf9 100644 --- a/headers/os/interface/Shape.h +++ b/headers/os/interface/Shape.h @@ -87,6 +87,8 @@ public: const BPoint& point); status_t Close(); + class Private; + private: // FBC padding virtual status_t Perform(perform_code code, void* data); @@ -97,17 +99,9 @@ private: virtual void _ReservedShape4(); private: + friend class Private; friend class BShapeIterator; - friend class BView; - friend class BFont; - friend class BPrivate::PicturePlayer; - friend class BPrivate::ServerLink; - void GetData(int32* opCount, int32* ptCount, - uint32** opList, BPoint** ptList); - void SetData(int32 opCount, int32 ptCount, - const uint32* opList, - const BPoint* ptList); void InitData(); bool AllocatePts(int32 count); bool AllocateOps(int32 count); diff --git a/headers/private/interface/ShapePrivate.h b/headers/private/interface/ShapePrivate.h index 204081550b..4f29337007 100644 --- a/headers/private/interface/ShapePrivate.h +++ b/headers/private/interface/ShapePrivate.h @@ -9,6 +9,7 @@ #ifndef SHAPE_PRIVATE_H #define SHAPE_PRIVATE_H +#include #include #include #include @@ -97,4 +98,20 @@ public: }; +class BShape::Private { +public: + Private(BShape& shape) : fShape(shape) {} + + void GetData(int32* opCount, int32* ptCount, + uint32** opList, BPoint** ptList); + void SetData(int32 opCount, int32 ptCount, + const uint32* opList, + const BPoint* ptList); + shape_data* PrivateData() {return (shape_data*)fShape.fPrivateData;} + +private: + BShape& fShape; +}; + + #endif // SHAPE_PRIVATE_H diff --git a/src/kits/app/ServerLink.cpp b/src/kits/app/ServerLink.cpp index 05ac105465..48e4fd8d4b 100644 --- a/src/kits/app/ServerLink.cpp +++ b/src/kits/app/ServerLink.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -105,7 +106,7 @@ ServerLink::ReadShape(BShape* shape) if (ptCount > 0) fReceiver->Read(ptList, ptCount * sizeof(BPoint)); - shape->SetData(opCount, ptCount, opList, ptList); + BShape::Private(*shape).SetData(opCount, ptCount, opList, ptList); return B_OK; } @@ -117,7 +118,7 @@ ServerLink::AttachShape(BShape& shape) uint32* opList; BPoint* ptList; - shape.GetData(&opCount, &ptCount, &opList, &ptList); + BShape::Private(shape).GetData(&opCount, &ptCount, &opList, &ptList); fSender->Attach(&opCount, sizeof(int32)); fSender->Attach(&ptCount, sizeof(int32)); diff --git a/src/kits/interface/PicturePlayer.cpp b/src/kits/interface/PicturePlayer.cpp index 2b144a703f..2c468dff0e 100644 --- a/src/kits/interface/PicturePlayer.cpp +++ b/src/kits/interface/PicturePlayer.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -1062,7 +1063,7 @@ PicturePlayer::_Play(const picture_player_callbacks& callbacks, void* userData, // TODO: remove BShape data copying BShape shape; - shape.SetData(*opCount, *pointCount, opList, pointList); + BShape::Private(shape).SetData(*opCount, *pointCount, opList, pointList); callbacks.draw_shape(userData, shape, header->op == B_PIC_FILL_SHAPE); @@ -1163,7 +1164,7 @@ PicturePlayer::_Play(const picture_player_callbacks& callbacks, void* userData, // TODO: remove BShape data copying BShape shape; - shape.SetData(*opCount, *pointCount, opList, pointList); + BShape::Private(shape).SetData(*opCount, *pointCount, opList, pointList); callbacks.draw_shape_gradient(userData, shape, *gradient, header->op == B_PIC_FILL_SHAPE_GRADIENT); diff --git a/src/kits/interface/Shape.cpp b/src/kits/interface/Shape.cpp index 60b6406623..83e2fd96b3 100644 --- a/src/kits/interface/Shape.cpp +++ b/src/kits/interface/Shape.cpp @@ -571,10 +571,10 @@ void BShape::_ReservedShape4() {} void -BShape::GetData(int32* opCount, int32* ptCount, uint32** opList, +BShape::Private::GetData(int32* opCount, int32* ptCount, uint32** opList, BPoint** ptList) { - shape_data* data = (shape_data*)fPrivateData; + shape_data* data = PrivateData(); *opCount = data->opCount; *ptCount = data->ptCount; @@ -584,22 +584,22 @@ BShape::GetData(int32* opCount, int32* ptCount, uint32** opList, void -BShape::SetData(int32 opCount, int32 ptCount, const uint32* opList, +BShape::Private::SetData(int32 opCount, int32 ptCount, const uint32* opList, const BPoint* ptList) { - Clear(); + fShape.Clear(); if (opCount == 0) return; - shape_data* data = (shape_data*)fPrivateData; + shape_data* data = PrivateData(); - if (!AllocateOps(opCount) || !AllocatePts(ptCount)) + if (!fShape.AllocateOps(opCount) || !fShape.AllocatePts(ptCount)) return; memcpy(data->opList, opList, opCount * sizeof(uint32)); data->opCount = opCount; - fBuildingOp = data->opList[data->opCount - 1]; + fShape.fBuildingOp = data->opList[data->opCount - 1]; if (ptCount > 0) { memcpy((void*)data->ptList, ptList, ptCount * sizeof(BPoint)); diff --git a/src/kits/interface/View.cpp b/src/kits/interface/View.cpp index c4365356b7..e3f58e5b31 100644 --- a/src/kits/interface/View.cpp +++ b/src/kits/interface/View.cpp @@ -4067,7 +4067,7 @@ BView::StrokeShape(BShape* shape, ::pattern pattern) if (shape == NULL || fOwner == NULL) return; - shape_data* sd = (shape_data*)shape->fPrivateData; + shape_data* sd = BShape::Private(*shape).PrivateData(); if (sd->opCount == 0 || sd->ptCount == 0) return; @@ -4088,7 +4088,7 @@ BView::FillShape(BShape* shape, ::pattern pattern) if (shape == NULL || fOwner == NULL) return; - shape_data* sd = (shape_data*)(shape->fPrivateData); + shape_data* sd = BShape::Private(*shape).PrivateData(); if (sd->opCount == 0 || sd->ptCount == 0) return; @@ -4109,7 +4109,7 @@ BView::FillShape(BShape* shape, const BGradient& gradient) if (shape == NULL || fOwner == NULL) return; - shape_data* sd = (shape_data*)(shape->fPrivateData); + shape_data* sd = BShape::Private(*shape).PrivateData(); if (sd->opCount == 0 || sd->ptCount == 0) return; @@ -5977,7 +5977,7 @@ BView::_ClipToShape(BShape* shape, bool inverse) if (shape == NULL) return; - shape_data* sd = (shape_data*)shape->fPrivateData; + shape_data* sd = BShape::Private(*shape).PrivateData(); if (sd->opCount == 0 || sd->ptCount == 0) return;