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<Shape*>(paint);

@Issue: https://github.com/Samsung/thorvg/issues/693
This commit is contained in:
Hermet Park 2021-10-22 14:39:38 +09:00 committed by Hermet Park
parent 8a7ec66cb2
commit 78d85d714a
6 changed files with 109 additions and 2 deletions

View 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);
};

View 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);
}
}
uint32_t LinearGradient::identifier() noexcept
{
return TVG_CLASS_ID_LINEAR;
}

View 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;

View 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;
}

View 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();

View 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;
}
}