common: code refactoring

remove exceptional handling which is inevitable scenario.

Change-Id: I761a59a38f4578291ee8bc044c5ca989feedbe79
This commit is contained in:
Hermet Park 2020-07-29 19:47:28 +09:00
parent 9e0c4666af
commit afc7bc8c2f
8 changed files with 67 additions and 173 deletions

View file

@ -36,59 +36,44 @@ Canvas::~Canvas()
Result Canvas::reserve(uint32_t n) noexcept Result Canvas::reserve(uint32_t n) noexcept
{ {
auto impl = pImpl.get(); IMPL->paints.reserve(n);
if (!impl) return Result::MemoryCorruption;
impl->paints.reserve(n);
return Result::Success; return Result::Success;
} }
Result Canvas::push(unique_ptr<Paint> paint) noexcept Result Canvas::push(unique_ptr<Paint> paint) noexcept
{ {
auto impl = pImpl.get(); return IMPL->push(move(paint));
if (!impl) return Result::MemoryCorruption;
return impl->push(move(paint));
} }
Result Canvas::clear() noexcept Result Canvas::clear() noexcept
{ {
auto impl = pImpl.get(); return IMPL->clear();
if (!impl) return Result::MemoryCorruption;
return impl->clear();
} }
Result Canvas::draw(bool async) noexcept Result Canvas::draw(bool async) noexcept
{ {
auto impl = pImpl.get(); return IMPL->draw();
if (!impl) return Result::MemoryCorruption;
return impl->draw();
} }
Result Canvas::update() noexcept Result Canvas::update() noexcept
{ {
auto impl = pImpl.get(); return IMPL->update();
if (!impl) return Result::MemoryCorruption;
return impl->update();
} }
Result Canvas::update(Paint* paint) noexcept Result Canvas::update(Paint* paint) noexcept
{ {
auto impl = pImpl.get(); return IMPL->update(paint);
if (!impl) return Result::MemoryCorruption;
return impl->update(paint);
} }
Result Canvas::sync() noexcept Result Canvas::sync() noexcept
{ {
auto impl = pImpl.get(); if (IMPL->renderer->flush()) return Result::Success;
if (!impl) return Result::MemoryCorruption;
if (impl->renderer->flush()) return Result::Success;
return Result::InsufficientCondition; return Result::InsufficientCondition;
} }

View file

@ -30,6 +30,7 @@
using namespace std; using namespace std;
using namespace tvg; using namespace tvg;
#define IMPL pImpl.get()
#define SCENE_IMPL scene->pImpl.get() #define SCENE_IMPL scene->pImpl.get()
#define SHAPE_IMPL shape->pImpl.get() #define SHAPE_IMPL shape->pImpl.get()

View file

@ -54,7 +54,6 @@ Fill::~Fill()
Result Fill::colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept Result Fill::colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept
{ {
auto impl = pImpl.get(); auto impl = pImpl.get();
if (!impl) return Result::MemoryCorruption;
if (cnt == 0) { if (cnt == 0) {
if (impl->colorStops) { if (impl->colorStops) {
@ -78,21 +77,15 @@ Result Fill::colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept
uint32_t Fill::colorStops(const ColorStop** colorStops) const noexcept uint32_t Fill::colorStops(const ColorStop** colorStops) const noexcept
{ {
auto impl = pImpl.get(); if (colorStops) *colorStops = IMPL->colorStops;
if (!impl) return 0;
if (colorStops) *colorStops = impl->colorStops; return IMPL->cnt;
return impl->cnt;
} }
Result Fill::spread(FillSpread s) noexcept Result Fill::spread(FillSpread s) noexcept
{ {
auto impl = pImpl.get(); IMPL->spread = s;
if (!impl) return Result::MemoryCorruption;
impl->spread = s;
return Result::Success; return Result::Success;
} }
@ -100,10 +93,7 @@ Result Fill::spread(FillSpread s) noexcept
FillSpread Fill::spread() const noexcept FillSpread Fill::spread() const noexcept
{ {
auto impl = pImpl.get(); return IMPL->spread;
assert(impl);
return impl->spread;
} }
#endif /* _TVG_FILL_CPP_ */ #endif /* _TVG_FILL_CPP_ */

View file

@ -49,13 +49,10 @@ Result LinearGradient::linear(float x1, float y1, float x2, float y2) noexcept
if (fabsf(x2 - x1) < FLT_EPSILON && fabsf(y2 - y1) < FLT_EPSILON) if (fabsf(x2 - x1) < FLT_EPSILON && fabsf(y2 - y1) < FLT_EPSILON)
return Result::InvalidArguments; return Result::InvalidArguments;
auto impl = pImpl.get(); IMPL->x1 = x1;
if (!impl) return Result::MemoryCorruption; IMPL->y1 = y1;
IMPL->x2 = x2;
impl->x1 = x1; IMPL->y2 = y2;
impl->y1 = y1;
impl->x2 = x2;
impl->y2 = y2;
return Result::Success; return Result::Success;
} }
@ -63,13 +60,10 @@ Result LinearGradient::linear(float x1, float y1, float x2, float y2) noexcept
Result LinearGradient::linear(float* x1, float* y1, float* x2, float* y2) const noexcept Result LinearGradient::linear(float* x1, float* y1, float* x2, float* y2) const noexcept
{ {
auto impl = pImpl.get(); if (x1) *x1 = IMPL->x1;
if (!impl) return Result::MemoryCorruption; if (x2) *x2 = IMPL->x2;
if (y1) *y1 = IMPL->y1;
if (x1) *x1 = impl->x1; if (y2) *y2 = IMPL->y2;
if (x2) *x2 = impl->x2;
if (y1) *y1 = impl->y1;
if (y2) *y2 = impl->y2;
return Result::Success; return Result::Success;
} }

View file

@ -39,35 +39,35 @@ Paint :: ~Paint()
Result Paint::rotate(float degree) noexcept Result Paint::rotate(float degree) noexcept
{ {
if (pImpl.get()->ts->rotate(degree)) return Result::Success; if (IMPL->ts->rotate(degree)) return Result::Success;
return Result::FailedAllocation; return Result::FailedAllocation;
} }
Result Paint::scale(float factor) noexcept Result Paint::scale(float factor) noexcept
{ {
if (pImpl.get()->ts->scale(factor)) return Result::Success; if (IMPL->ts->scale(factor)) return Result::Success;
return Result::FailedAllocation; return Result::FailedAllocation;
} }
Result Paint::translate(float x, float y) noexcept Result Paint::translate(float x, float y) noexcept
{ {
if (pImpl.get()->ts->translate(x, y)) return Result::Success; if (IMPL->ts->translate(x, y)) return Result::Success;
return Result::FailedAllocation; return Result::FailedAllocation;
} }
Result Paint::transform(const Matrix& m) noexcept Result Paint::transform(const Matrix& m) noexcept
{ {
if (pImpl.get()->ts->transform(m)) return Result::Success; if (IMPL->ts->transform(m)) return Result::Success;
return Result::FailedAllocation; return Result::FailedAllocation;
} }
Result Paint::bounds(float* x, float* y, float* w, float* h) const noexcept Result Paint::bounds(float* x, float* y, float* w, float* h) const noexcept
{ {
if (pImpl.get()->ts->bounds(x, y, w, h)) return Result::Success; if (IMPL->ts->bounds(x, y, w, h)) return Result::Success;
return Result::InsufficientCondition; return Result::InsufficientCondition;
} }

View file

@ -46,15 +46,11 @@ RadialGradient::~RadialGradient()
Result RadialGradient::radial(float cx, float cy, float radius) noexcept Result RadialGradient::radial(float cx, float cy, float radius) noexcept
{ {
if (radius < FLT_EPSILON) if (radius < FLT_EPSILON) return Result::InvalidArguments;
return Result::InvalidArguments;
auto impl = pImpl.get(); IMPL->cx = cx;
if (!impl) return Result::MemoryCorruption; IMPL->cy = cy;
IMPL->radius = radius;
impl->cx = cx;
impl->cy = cy;
impl->radius = radius;
return Result::Success; return Result::Success;
} }
@ -62,12 +58,9 @@ Result RadialGradient::radial(float cx, float cy, float radius) noexcept
Result RadialGradient::radial(float* cx, float* cy, float* radius) const noexcept Result RadialGradient::radial(float* cx, float* cy, float* radius) const noexcept
{ {
auto impl = pImpl.get(); if (cx) *cx = IMPL->cx;
if (!impl) return Result::MemoryCorruption; if (cy) *cy = IMPL->cy;
if (radius) *radius = IMPL->radius;
if (cx) *cx = impl->cx;
if (cy) *cy = impl->cy;
if (radius) *radius = impl->radius;
return Result::Success; return Result::Success;
} }

View file

@ -44,12 +44,9 @@ unique_ptr<Scene> Scene::gen() noexcept
Result Scene::push(unique_ptr<Paint> paint) noexcept Result Scene::push(unique_ptr<Paint> paint) noexcept
{ {
auto impl = pImpl.get();
if (!impl) return Result::MemoryCorruption;
auto p = paint.release(); auto p = paint.release();
if (!p) return Result::MemoryCorruption; if (!p) return Result::MemoryCorruption;
impl->paints.push_back(p); IMPL->paints.push_back(p);
return Result::Success; return Result::Success;
} }
@ -57,10 +54,7 @@ Result Scene::push(unique_ptr<Paint> paint) noexcept
Result Scene::reserve(uint32_t size) noexcept Result Scene::reserve(uint32_t size) noexcept
{ {
auto impl = pImpl.get(); IMPL->paints.reserve(size);
if (!impl) return Result::MemoryCorruption;
impl->paints.reserve(size);
return Result::Success; return Result::Success;
} }
@ -70,10 +64,7 @@ Result Scene::load(const std::string& path) noexcept
{ {
if (path.empty()) return Result::InvalidArguments; if (path.empty()) return Result::InvalidArguments;
auto impl = pImpl.get(); return IMPL->load(path);
if (!impl) return Result::MemoryCorruption;
return impl->load(path);
} }
#endif /* _TVG_SCENE_CPP_ */ #endif /* _TVG_SCENE_CPP_ */

View file

@ -50,12 +50,9 @@ unique_ptr<Shape> Shape::gen() noexcept
Result Shape::reset() noexcept Result Shape::reset() noexcept
{ {
auto impl = pImpl.get(); IMPL->path->reset();
if (!impl || !impl->path) return Result::MemoryCorruption;
impl->path->reset(); IMPL->flag |= RenderUpdateFlag::Path;
impl->flag |= RenderUpdateFlag::Path;
return Result::Success; return Result::Success;
} }
@ -65,12 +62,9 @@ uint32_t Shape::pathCommands(const PathCommand** cmds) const noexcept
{ {
if (!cmds) return 0; if (!cmds) return 0;
auto impl = pImpl.get(); *cmds = IMPL->path->cmds;
if (!impl || !impl->path) return 0;
*cmds = impl->path->cmds; return IMPL->path->cmdCnt;
return impl->path->cmdCnt;
} }
@ -78,12 +72,9 @@ uint32_t Shape::pathCoords(const Point** pts) const noexcept
{ {
if (!pts) return 0; if (!pts) return 0;
auto impl = pImpl.get(); *pts = IMPL->path->pts;
if (!impl || !impl->path) return 0;
*pts = impl->path->pts; return IMPL->path->ptsCnt;
return impl->path->ptsCnt;
} }
@ -91,13 +82,10 @@ Result Shape::appendPath(const PathCommand *cmds, uint32_t cmdCnt, const Point*
{ {
if (cmdCnt < 0 || ptsCnt < 0 || !pts || !ptsCnt) return Result::InvalidArguments; if (cmdCnt < 0 || ptsCnt < 0 || !pts || !ptsCnt) return Result::InvalidArguments;
auto impl = pImpl.get(); IMPL->path->grow(cmdCnt, ptsCnt);
if (!impl || !impl->path) return Result::MemoryCorruption; IMPL->path->append(cmds, cmdCnt, pts, ptsCnt);
impl->path->grow(cmdCnt, ptsCnt); IMPL->flag |= RenderUpdateFlag::Path;
impl->path->append(cmds, cmdCnt, pts, ptsCnt);
impl->flag |= RenderUpdateFlag::Path;
return Result::Success; return Result::Success;
} }
@ -105,12 +93,9 @@ Result Shape::appendPath(const PathCommand *cmds, uint32_t cmdCnt, const Point*
Result Shape::moveTo(float x, float y) noexcept Result Shape::moveTo(float x, float y) noexcept
{ {
auto impl = pImpl.get(); IMPL->path->moveTo(x, y);
if (!impl || !impl->path) return Result::MemoryCorruption;
impl->path->moveTo(x, y); IMPL->flag |= RenderUpdateFlag::Path;
impl->flag |= RenderUpdateFlag::Path;
return Result::Success; return Result::Success;
} }
@ -118,12 +103,9 @@ Result Shape::moveTo(float x, float y) noexcept
Result Shape::lineTo(float x, float y) noexcept Result Shape::lineTo(float x, float y) noexcept
{ {
auto impl = pImpl.get(); IMPL->path->lineTo(x, y);
if (!impl || !impl->path) return Result::MemoryCorruption;
impl->path->lineTo(x, y); IMPL->flag |= RenderUpdateFlag::Path;
impl->flag |= RenderUpdateFlag::Path;
return Result::Success; return Result::Success;
} }
@ -131,12 +113,9 @@ Result Shape::lineTo(float x, float y) noexcept
Result Shape::cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept Result Shape::cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept
{ {
auto impl = pImpl.get(); IMPL->path->cubicTo(cx1, cy1, cx2, cy2, x, y);
if (!impl || !impl->path) return Result::MemoryCorruption;
impl->path->cubicTo(cx1, cy1, cx2, cy2, x, y); IMPL->flag |= RenderUpdateFlag::Path;
impl->flag |= RenderUpdateFlag::Path;
return Result::Success; return Result::Success;
} }
@ -144,12 +123,9 @@ Result Shape::cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float
Result Shape::close() noexcept Result Shape::close() noexcept
{ {
auto impl = pImpl.get(); IMPL->path->close();
if (!impl || !impl->path) return Result::MemoryCorruption;
impl->path->close(); IMPL->flag |= RenderUpdateFlag::Path;
impl->flag |= RenderUpdateFlag::Path;
return Result::Success; return Result::Success;
} }
@ -158,7 +134,6 @@ Result Shape::close() noexcept
Result Shape::appendCircle(float cx, float cy, float rx, float ry) noexcept Result Shape::appendCircle(float cx, float cy, float rx, float ry) noexcept
{ {
auto impl = pImpl.get(); auto impl = pImpl.get();
if (!impl || !impl->path) return Result::MemoryCorruption;
auto rxKappa = rx * PATH_KAPPA; auto rxKappa = rx * PATH_KAPPA;
auto ryKappa = ry * PATH_KAPPA; auto ryKappa = ry * PATH_KAPPA;
@ -180,7 +155,6 @@ Result Shape::appendCircle(float cx, float cy, float rx, float ry) noexcept
Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry) noexcept Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry) noexcept
{ {
auto impl = pImpl.get(); auto impl = pImpl.get();
if (!impl || !impl->path) return Result::MemoryCorruption;
auto halfW = w * 0.5f; auto halfW = w * 0.5f;
auto halfH = h * 0.5f; auto halfH = h * 0.5f;
@ -225,7 +199,6 @@ Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry)
Result Shape::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept Result Shape::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
{ {
auto impl = pImpl.get(); auto impl = pImpl.get();
if (!impl) return Result::MemoryCorruption;
impl->color[0] = r; impl->color[0] = r;
impl->color[1] = g; impl->color[1] = g;
@ -246,7 +219,6 @@ Result Shape::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
Result Shape::fill(unique_ptr<Fill> f) noexcept Result Shape::fill(unique_ptr<Fill> f) noexcept
{ {
auto impl = pImpl.get(); auto impl = pImpl.get();
if (!impl) return Result::MemoryCorruption;
auto p = f.release(); auto p = f.release();
if (!p) return Result::MemoryCorruption; if (!p) return Result::MemoryCorruption;
@ -262,7 +234,6 @@ Result Shape::fill(unique_ptr<Fill> f) noexcept
Result Shape::fill(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept Result Shape::fill(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
{ {
auto impl = pImpl.get(); auto impl = pImpl.get();
if (!impl) return Result::MemoryCorruption;
if (r) *r = impl->color[0]; if (r) *r = impl->color[0];
if (g) *g = impl->color[1]; if (g) *g = impl->color[1];
@ -274,19 +245,13 @@ Result Shape::fill(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcep
const Fill* Shape::fill() const noexcept const Fill* Shape::fill() const noexcept
{ {
auto impl = pImpl.get(); return IMPL->fill;
if (!impl) return nullptr;
return impl->fill;
} }
Result Shape::stroke(float width) noexcept Result Shape::stroke(float width) noexcept
{ {
auto impl = pImpl.get(); if (!IMPL->strokeWidth(width)) return Result::FailedAllocation;
if (!impl) return Result::MemoryCorruption;
if (!impl->strokeWidth(width)) return Result::FailedAllocation;
return Result::Success; return Result::Success;
} }
@ -294,20 +259,14 @@ Result Shape::stroke(float width) noexcept
float Shape::strokeWidth() const noexcept float Shape::strokeWidth() const noexcept
{ {
auto impl = pImpl.get(); if (!IMPL->stroke) return 0;
if (!impl) return 0; return IMPL->stroke->width;
if (!impl->stroke) return 0;
return impl->stroke->width;
} }
Result Shape::stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept Result Shape::stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
{ {
auto impl = pImpl.get(); if (!IMPL->strokeColor(r, g, b, a)) return Result::FailedAllocation;
if (!impl) return Result::MemoryCorruption;
if (!impl->strokeColor(r, g, b, a)) return Result::FailedAllocation;
return Result::Success; return Result::Success;
} }
@ -316,7 +275,6 @@ Result Shape::stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
Result Shape::strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept Result Shape::strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
{ {
auto impl = pImpl.get(); auto impl = pImpl.get();
if (!impl) return Result::MemoryCorruption;
if (!impl->stroke) return Result::InsufficientCondition; if (!impl->stroke) return Result::InsufficientCondition;
@ -333,10 +291,7 @@ Result Shape::stroke(const float* dashPattern, uint32_t cnt) noexcept
{ {
if (cnt < 2 || !dashPattern) return Result::InvalidArguments; if (cnt < 2 || !dashPattern) return Result::InvalidArguments;
auto impl = pImpl.get(); if (!IMPL->strokeDash(dashPattern, cnt)) return Result::FailedAllocation;
if (!impl) return Result::MemoryCorruption;
if (!impl->strokeDash(dashPattern, cnt)) return Result::FailedAllocation;
return Result::Success; return Result::Success;
} }
@ -344,22 +299,16 @@ Result Shape::stroke(const float* dashPattern, uint32_t cnt) noexcept
uint32_t Shape::strokeDash(const float** dashPattern) const noexcept uint32_t Shape::strokeDash(const float** dashPattern) const noexcept
{ {
auto impl = pImpl.get(); if (!IMPL->stroke) return 0;
assert(impl);
if (!impl->stroke) return 0; if (dashPattern) *dashPattern = IMPL->stroke->dashPattern;
return IMPL->stroke->dashCnt;
if (dashPattern) *dashPattern = impl->stroke->dashPattern;
return impl->stroke->dashCnt;
} }
Result Shape::stroke(StrokeCap cap) noexcept Result Shape::stroke(StrokeCap cap) noexcept
{ {
auto impl = pImpl.get(); if (!IMPL->strokeCap(cap)) return Result::FailedAllocation;
if (!impl) return Result::MemoryCorruption;
if (!impl->strokeCap(cap)) return Result::FailedAllocation;
return Result::Success; return Result::Success;
} }
@ -367,10 +316,7 @@ Result Shape::stroke(StrokeCap cap) noexcept
Result Shape::stroke(StrokeJoin join) noexcept Result Shape::stroke(StrokeJoin join) noexcept
{ {
auto impl = pImpl.get(); if (!IMPL->strokeJoin(join)) return Result::FailedAllocation;
if (!impl) return Result::MemoryCorruption;
if (!impl->strokeJoin(join)) return Result::FailedAllocation;
return Result::Success; return Result::Success;
} }
@ -378,23 +324,17 @@ Result Shape::stroke(StrokeJoin join) noexcept
StrokeCap Shape::strokeCap() const noexcept StrokeCap Shape::strokeCap() const noexcept
{ {
auto impl = pImpl.get(); if (!IMPL->stroke) return StrokeCap::Square;
assert(impl);
if (!impl->stroke) return StrokeCap::Square; return IMPL->stroke->cap;
return impl->stroke->cap;
} }
StrokeJoin Shape::strokeJoin() const noexcept StrokeJoin Shape::strokeJoin() const noexcept
{ {
auto impl = pImpl.get(); if (!IMPL->stroke) return StrokeJoin::Bevel;
assert(impl);
if (!impl->stroke) return StrokeJoin::Bevel; return IMPL->stroke->join;
return impl->stroke->join;
} }