common: Introduce class type identifier apis.
authorHermet Park <chuneon.park@samsung.com>
Fri, 22 Oct 2021 05:39:38 +0000 (14:39 +0900)
committerHermet Park <chuneon.park@samsung.com>
Tue, 26 Oct 2021 03:56:35 +0000 (12:56 +0900)
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<Shape*>(paint);

@Issue: https://github.com/Samsung/thorvg/issues/693

inc/thorvg.h
src/lib/tvgLinearGradient.cpp
src/lib/tvgPicture.cpp
src/lib/tvgRadialGradient.cpp
src/lib/tvgScene.cpp
src/lib/tvgShape.cpp

index f0b9666..0c2b334 100644 (file)
@@ -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<LinearGradient> 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<RadialGradient> 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<Shape> 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<Picture> 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<Scene> 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);
 };
 
index 57f2b3f..46ca45f 100644 (file)
@@ -90,4 +90,10 @@ Result LinearGradient::linear(float* x1, float* y1, float* x2, float* y2) const
 unique_ptr<LinearGradient> LinearGradient::gen() noexcept
 {
     return unique_ptr<LinearGradient>(new LinearGradient);
-}
\ No newline at end of file
+}
+
+
+uint32_t LinearGradient::identifier() noexcept
+{
+    return TVG_CLASS_ID_LINEAR;
+}
index a1d6f5d..9aecbb1 100644 (file)
@@ -45,6 +45,12 @@ unique_ptr<Picture> 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;
index 2680286..c289ca5 100644 (file)
@@ -89,3 +89,9 @@ unique_ptr<RadialGradient> RadialGradient::gen() noexcept
 {
     return unique_ptr<RadialGradient>(new RadialGradient);
 }
+
+
+uint32_t RadialGradient::identifier() noexcept
+{
+    return TVG_CLASS_ID_RADIAL;
+}
\ No newline at end of file
index 798f164..1516b2e 100644 (file)
@@ -44,6 +44,12 @@ unique_ptr<Scene> Scene::gen() noexcept
 }
 
 
+uint32_t Scene::identifier() noexcept
+{
+    return TVG_CLASS_ID_SCENE;
+}
+
+
 Result Scene::push(unique_ptr<Paint> paint) noexcept
 {
     auto p = paint.release();
index c360f1d..e1eaa59 100644 (file)
@@ -55,6 +55,12 @@ unique_ptr<Shape> 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