From 78d85d714aad650064f9a211a17db8b9588e2c6a Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Fri, 22 Oct 2021 14:39:38 +0900 Subject: [PATCH] common: Introduce class type identifier apis. This identifier is useful when user identify the instance type in runtime. ThorVG basically don't prefer to dynamic_cast() nor typeid(), it compiles with -fno-rtti option for the optimial size. Here is an example for the simple usage. if (paint->identifier() == Shape::identifier()) auto shape = static_cast(paint); @Issue: https://github.com/Samsung/thorvg/issues/693 --- inc/thorvg.h | 77 +++++++++++++++++++++++++++++++++++ src/lib/tvgLinearGradient.cpp | 8 +++- src/lib/tvgPicture.cpp | 6 +++ src/lib/tvgRadialGradient.cpp | 6 +++ src/lib/tvgScene.cpp | 6 +++ src/lib/tvgShape.cpp | 8 +++- 6 files changed, 109 insertions(+), 2 deletions(-) diff --git a/inc/thorvg.h b/inc/thorvg.h index f0b96669..0c2b334b 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -339,6 +339,17 @@ public: */ CompositeMethod composite(const Paint** target) const noexcept; + /** + * @brief Return the unique id value of the paint instance. + * + * This method can be called for checking the current concrete instance type. + * + * @return The type id of the Paint instance. + * + * @BETA_API + */ + uint32_t identifier() const { return _id; } + _TVG_DECLARE_ACCESSOR(); _TVG_DECALRE_IDENTIFIER(); _TVG_DECLARE_PRIVATE(Paint); @@ -441,6 +452,17 @@ public: */ Fill* duplicate() const noexcept; + /** + * @brief Return the unique id value of the Fill instance. + * + * This method can be called for checking the current concrete instance type. + * + * @return The type id of the Fill instance. + * + * @BETA_API + */ + uint32_t identifier() const { return _id; } + _TVG_DECALRE_IDENTIFIER(); _TVG_DECLARE_PRIVATE(Fill); }; @@ -598,6 +620,17 @@ public: */ static std::unique_ptr gen() noexcept; + /** + * @brief Return the unique id value of this class. + * + * This method can be referred for identifying the LinearGradient class type. + * + * @return The type id of the LinearGradient class. + * + * @BETA_API + */ + static uint32_t identifier() noexcept; + _TVG_DECLARE_PRIVATE(LinearGradient); }; @@ -646,6 +679,17 @@ public: */ static std::unique_ptr gen() noexcept; + /** + * @brief Return the unique id value of this class. + * + * This method can be referred for identifying the RadialGradient class type. + * + * @return The type id of the RadialGradient class. + * + * @BETA_API + */ + static uint32_t identifier() noexcept; + _TVG_DECLARE_PRIVATE(RadialGradient); }; @@ -1020,6 +1064,17 @@ public: */ static std::unique_ptr gen() noexcept; + /** + * @brief Return the unique id value of this class. + * + * This method can be referred for identifying the Shape class type. + * + * @return The type id of the Shape class. + * + * @BETA_API + */ + static uint32_t identifier() noexcept; + _TVG_DECLARE_PRIVATE(Shape); }; @@ -1146,6 +1201,17 @@ public: */ static std::unique_ptr gen() noexcept; + /** + * @brief Return the unique id value of this class. + * + * This method can be referred for identifying the Picture class type. + * + * @return The type id of the Picture class. + * + * @BETA_API + */ + static uint32_t identifier() noexcept; + _TVG_DECLARE_PRIVATE(Picture); }; @@ -1215,6 +1281,17 @@ public: */ static std::unique_ptr gen() noexcept; + /** + * @brief Return the unique id value of this class. + * + * This method can be referred for identifying the Scene class type. + * + * @return The type id of the Scene class. + * + * @BETA_API + */ + static uint32_t identifier() noexcept; + _TVG_DECLARE_PRIVATE(Scene); }; diff --git a/src/lib/tvgLinearGradient.cpp b/src/lib/tvgLinearGradient.cpp index 57f2b3f2..46ca45fc 100644 --- a/src/lib/tvgLinearGradient.cpp +++ b/src/lib/tvgLinearGradient.cpp @@ -90,4 +90,10 @@ Result LinearGradient::linear(float* x1, float* y1, float* x2, float* y2) const unique_ptr LinearGradient::gen() noexcept { return unique_ptr(new LinearGradient); -} \ No newline at end of file +} + + +uint32_t LinearGradient::identifier() noexcept +{ + return TVG_CLASS_ID_LINEAR; +} diff --git a/src/lib/tvgPicture.cpp b/src/lib/tvgPicture.cpp index a1d6f5da..9aecbb14 100644 --- a/src/lib/tvgPicture.cpp +++ b/src/lib/tvgPicture.cpp @@ -45,6 +45,12 @@ unique_ptr Picture::gen() noexcept } +uint32_t Picture::identifier() noexcept +{ + return TVG_CLASS_ID_PICTURE; +} + + Result Picture::load(const std::string& path) noexcept { if (path.empty()) return Result::InvalidArguments; diff --git a/src/lib/tvgRadialGradient.cpp b/src/lib/tvgRadialGradient.cpp index 2680286d..c289ca59 100644 --- a/src/lib/tvgRadialGradient.cpp +++ b/src/lib/tvgRadialGradient.cpp @@ -89,3 +89,9 @@ unique_ptr RadialGradient::gen() noexcept { return unique_ptr(new RadialGradient); } + + +uint32_t RadialGradient::identifier() noexcept +{ + return TVG_CLASS_ID_RADIAL; +} \ No newline at end of file diff --git a/src/lib/tvgScene.cpp b/src/lib/tvgScene.cpp index 798f1648..1516b2eb 100644 --- a/src/lib/tvgScene.cpp +++ b/src/lib/tvgScene.cpp @@ -44,6 +44,12 @@ unique_ptr Scene::gen() noexcept } +uint32_t Scene::identifier() noexcept +{ + return TVG_CLASS_ID_SCENE; +} + + Result Scene::push(unique_ptr paint) noexcept { auto p = paint.release(); diff --git a/src/lib/tvgShape.cpp b/src/lib/tvgShape.cpp index c360f1d6..e1eaa59d 100644 --- a/src/lib/tvgShape.cpp +++ b/src/lib/tvgShape.cpp @@ -55,6 +55,12 @@ unique_ptr Shape::gen() noexcept } +uint32_t Shape::identifier() noexcept +{ + return TVG_CLASS_ID_SHAPE; +} + + Result Shape::reset() noexcept { pImpl->path.reset(); @@ -424,4 +430,4 @@ Result Shape::fill(FillRule r) noexcept FillRule Shape::fillRule() const noexcept { return pImpl->rule; -} +} \ No newline at end of file