common: code refactoring.

revise duplicate() approach with stategy pattern.
This commit is contained in:
Hermet Park 2020-09-18 12:14:01 +09:00 committed by Hermet Park
parent c8a03494a1
commit 87fbff63cb
11 changed files with 83 additions and 57 deletions

View file

@ -92,7 +92,7 @@ public:
Result transform(const Matrix& m) noexcept;
Result bounds(float* x, float* y, float* w, float* h) const noexcept;
virtual std::unique_ptr<Paint> duplicate() const noexcept = 0;
std::unique_ptr<Paint> duplicate() const noexcept;
_TVG_DECLARE_ACCESSOR();
_TVG_DECLARE_PRIVATE(Paint);
@ -250,7 +250,6 @@ public:
StrokeJoin strokeJoin() const noexcept;
static std::unique_ptr<Shape> gen() noexcept;
std::unique_ptr<Paint> duplicate() const noexcept override;
_TVG_DECLARE_PRIVATE(Shape);
};
@ -274,7 +273,6 @@ public:
Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
static std::unique_ptr<Picture> gen() noexcept;
std::unique_ptr<Paint> duplicate() const noexcept override;
_TVG_DECLARE_PRIVATE(Picture);
};
@ -297,7 +295,6 @@ public:
Result reserve(uint32_t size) noexcept;
static std::unique_ptr<Scene> gen() noexcept;
std::unique_ptr<Paint> duplicate() const noexcept override;
_TVG_DECLARE_PRIVATE(Scene);
};

View file

@ -30,7 +30,6 @@ struct LinearGradient::Impl
float x1, y1, x2, y2;
};
/************************************************************************/
/* External Class Implementation */
/************************************************************************/

View file

@ -72,3 +72,8 @@ Result Paint::bounds(float* x, float* y, float* w, float* h) const noexcept
if (IMPL->bounds(x, y, w, h)) return Result::Success;
return Result::InsufficientCondition;
}
unique_ptr<Paint> Paint::duplicate() const noexcept
{
return IMPL->duplicate();
}

View file

@ -32,6 +32,7 @@ namespace tvg
virtual bool update(RenderMethod& renderer, const RenderTransform* transform, RenderUpdateFlag pFlag) = 0;
virtual bool render(RenderMethod& renderer) = 0;
virtual bool bounds(float* x, float* y, float* w, float* h) const = 0;
virtual unique_ptr<Paint> duplicate() = 0;
};
struct Paint::Impl
@ -144,6 +145,11 @@ namespace tvg
{
return smethod->render(renderer);
}
unique_ptr<Paint> duplicate()
{
return smethod->duplicate();
}
};
@ -174,6 +180,11 @@ namespace tvg
{
return inst->render(renderer);
}
unique_ptr<Paint> duplicate() override
{
return inst->duplicate();
}
};
}

View file

@ -63,10 +63,3 @@ Result Picture::viewbox(float* x, float* y, float* w, float* h) const noexcept
if (IMPL->viewbox(x, y, w, h)) return Result::Success;
return Result::InsufficientCondition;
}
std::unique_ptr<Paint> Picture::duplicate() const noexcept
{
//TODO: implement
return nullptr;
}

View file

@ -104,6 +104,12 @@ struct Picture::Impl
if (!loader->read()) return Result::Unknown;
return Result::Success;
}
unique_ptr<Paint> duplicate()
{
//TODO:
return nullptr;
}
};
#endif //_TVG_PICTURE_IMPL_H_

View file

@ -58,10 +58,3 @@ Result Scene::reserve(uint32_t size) noexcept
return Result::Success;
}
std::unique_ptr<Paint> Scene::duplicate() const noexcept
{
//TODO: implement
return nullptr;
}

View file

@ -88,6 +88,12 @@ struct Scene::Impl
return true;
}
unique_ptr<Paint> duplicate()
{
//TODO:
return nullptr;
}
};
#endif //_TVG_SCENE_IMPL_H_

View file

@ -49,14 +49,6 @@ unique_ptr<Shape> Shape::gen() noexcept
}
unique_ptr<Paint> Shape::duplicate() const noexcept
{
auto shape = Shape::gen();
if (!shape->pImpl->duplicate(IMPL)) return nullptr;
return shape;
}
Result Shape::reset() noexcept
{
IMPL->path->reset();

View file

@ -38,6 +38,19 @@ struct ShapeStroke
StrokeCap cap = StrokeCap::Square;
StrokeJoin join = StrokeJoin::Bevel;
ShapeStroke() {}
ShapeStroke(const ShapeStroke* src)
{
width = src->width;
dashCnt = src->dashCnt;
cap = src->cap;
join = src->join;
memcpy(color, src->color, sizeof(color));
dashPattern = static_cast<float*>(malloc(sizeof(float) * dashCnt));
memcpy(dashPattern, src->dashPattern, sizeof(float) * dashCnt);
}
~ShapeStroke()
{
if (dashPattern) free(dashPattern);
@ -163,42 +176,32 @@ struct Shape::Impl
return true;
}
bool duplicate(Shape::Impl *src)
unique_ptr<Paint> duplicate()
{
auto ret = Shape::gen();
if (!ret) return nullptr;
auto dup = ret.get()->pImpl.get();
//Color
memcpy(color, src->color, sizeof(color));
flag = RenderUpdateFlag::Color;
memcpy(dup->color, color, sizeof(color));
dup->flag = RenderUpdateFlag::Color;
//Copy Path
if (src->path) {
path = new ShapePath();
if (!path) return false;
*path = *src->path;
path->cmds = static_cast<PathCommand*>(malloc(sizeof(PathCommand) * path->reservedCmdCnt));
if (!path->cmds) return false;
memcpy(path->cmds, src->path->cmds, sizeof(PathCommand) * path->cmdCnt);
path->pts = static_cast<Point*>(malloc(sizeof(Point) * path->reservedPtsCnt));
if (!path->pts) return false;
memcpy(path->pts, src->path->pts, sizeof(Point) * path->ptsCnt);
flag |= RenderUpdateFlag::Path;
//Path
if (path) {
dup->path = new ShapePath(path);
dup->flag |= RenderUpdateFlag::Path;
}
//Copy Stroke
if (src->stroke) {
stroke = new ShapeStroke();
if (!stroke) return false;
*stroke = *src->stroke;
stroke->dashPattern = static_cast<float*>(malloc(sizeof(float) * stroke->dashCnt));
memcpy(stroke->dashPattern, src->stroke->dashPattern, sizeof(float) * stroke->dashCnt);
flag |= RenderUpdateFlag::Stroke;
//Stroke
if (stroke) {
dup->stroke = new ShapeStroke(stroke);
dup->flag |= RenderUpdateFlag::Stroke;
}
//TODO: Copy Fill
//TODO: Fill
return true;
return ret;
}
};

View file

@ -44,6 +44,27 @@ struct ShapePath
if (pts) free(pts);
}
ShapePath() {}
ShapePath(const ShapePath* src)
{
cmdCnt = src->cmdCnt;
reservedCmdCnt = src->reservedCmdCnt;
ptsCnt = src->ptsCnt;
reservedPtsCnt = src->reservedPtsCnt;
cmds = static_cast<PathCommand*>(malloc(sizeof(PathCommand) * reservedCmdCnt));
if (!cmds) return;
memcpy(cmds, src->cmds, sizeof(PathCommand) * cmdCnt);
pts = static_cast<Point*>(malloc(sizeof(Point) * reservedPtsCnt));
if (!pts) {
free(cmds);
return;
}
memcpy(pts, src->pts, sizeof(Point) * ptsCnt);
}
void reserveCmd(uint32_t cmdCnt)
{
if (cmdCnt <= reservedCmdCnt) return;