Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[RTTI] Add helper macro for OV RTTI definition in base classes (#28911)

Tomasz Jankowski25910a7dab1a
diff --git a/src/core/include/openvino/core/extension.hpp b/src/core/include/openvino/core/extension.hpp
index 6fb59f2a3d1..3349e4e6250 100644
--- a/src/core/include/openvino/core/extension.hpp
+++ b/src/core/include/openvino/core/extension.hpp
@@ -25,13 +25,7 @@ class Extension;
*/
class OPENVINO_API Extension {
public:
- _OPENVINO_HIDDEN_METHOD static const DiscreteTypeInfo& get_type_info_static() {
- static const ::ov::DiscreteTypeInfo type_info_static{"Extension"};
- return type_info_static;
- }
- virtual const DiscreteTypeInfo& get_type_info() const {
- return get_type_info_static();
- }
+ OPENVINO_RTTI_BASE("Extension")
using Ptr = std::shared_ptr<Extension>;
diff --git a/src/core/include/openvino/core/model.hpp b/src/core/include/openvino/core/model.hpp
index 6ad235062ed..2453758442a 100644
--- a/src/core/include/openvino/core/model.hpp
+++ b/src/core/include/openvino/core/model.hpp
@@ -51,13 +51,7 @@ class OPENVINO_API Model : public std::enable_shared_from_this<Model> {
std::shared_ptr<void> m_shared_object; // plugin shared object handle.
public:
- _OPENVINO_HIDDEN_METHOD static const ::ov::DiscreteTypeInfo& get_type_info_static() {
- static const ::ov::DiscreteTypeInfo type_info_static{"Model"};
- return type_info_static;
- }
- const ::ov::DiscreteTypeInfo& get_type_info() const {
- return get_type_info_static();
- }
+ OPENVINO_RTTI_BASE("Model")
Model(const ov::NodeVector& results, const ov::ParameterVector& parameters, const std::string& name = "");
diff --git a/src/core/include/openvino/core/rtti.hpp b/src/core/include/openvino/core/rtti.hpp
index abf3f9f7641..6aa69062347 100644
--- a/src/core/include/openvino/core/rtti.hpp
+++ b/src/core/include/openvino/core/rtti.hpp
@@ -81,15 +81,59 @@
/// for all operations and doesn't provide ability to define some perfect subset of
/// operations. PARENT_CLASS should define RTTI with OPENVINO_RTTI_{DECLARATION/DEFINITION}
/// macros.
-/// \param _VERSION_INDEX is an unsigned integer index to distinguish different versions of
-/// operations that shares the same TYPE_NAME (for backward compatibility)
///
/// OPENVINO_RTTI(name)
/// OPENVINO_RTTI(name, version_id)
/// OPENVINO_RTTI(name, version_id, parent)
-/// OPENVINO_RTTI(name, version_id, parent, old_version)
+
#define OPENVINO_RTTI(...) \
_OPENVINO_RTTI_EXPAND(_OPENVINO_RTTI_DEFINITION_SELECTOR_3(__VA_ARGS__, \
_OPENVINO_RTTI_WITH_TYPE_VERSION_PARENT, \
_OPENVINO_RTTI_WITH_TYPE_VERSION, \
_OPENVINO_RTTI_WITH_TYPE)(__VA_ARGS__))
+
+#define _OPENVINO_RTTI_BASE_WITH_TYPE(TYPE_NAME) _OPENVINO_RTTI_BASE_WITH_TYPE_VERSION(TYPE_NAME, "util")
+
+#define _OPENVINO_RTTI_BASE_WITH_TYPE_VERSION(TYPE_NAME, VERSION_NAME) \
+ _OPENVINO_HIDDEN_METHOD static const ::ov::DiscreteTypeInfo& get_type_info_static() { \
+ static ::ov::DiscreteTypeInfo type_info_static{TYPE_NAME, VERSION_NAME}; \
+ type_info_static.hash(); \
+ return type_info_static; \
+ } \
+ virtual const ::ov::DiscreteTypeInfo& get_type_info() const { return get_type_info_static(); }
+
+/// Helper macro for base (without rtti parrent) class that provides RTTI block definition.
+/// It's a two parameter version of OPENVINO_RTTI macro, without PARENT_CLASS.
+#define OPENVINO_RTTI_BASE(...) \
+ _OPENVINO_RTTI_EXPAND(_OPENVINO_RTTI_DEFINITION_SELECTOR_2(__VA_ARGS__, \
+ _OPENVINO_RTTI_BASE_WITH_TYPE_VERSION, \
+ _OPENVINO_RTTI_BASE_WITH_TYPE)(__VA_ARGS__))
+
+#if defined(__GNUC__)
+# define OPENVINO_DO_PRAGMA(x) _Pragma(#x)
+#elif defined(_MSC_VER)
+# define OPENVINO_DO_PRAGMA(x) __pragma(x)
+#else
+# define OPENVINO_DO_PRAGMA(x)
+#endif
+
+#if defined(__clang__)
+# define OPENVINO_SUPPRESS_SUGGEST_OVERRIDE_START \
+ OPENVINO_DO_PRAGMA(clang diagnostic push) \
+ OPENVINO_DO_PRAGMA(clang diagnostic ignored "-Wsuggest-override") \
+ OPENVINO_DO_PRAGMA(clang diagnostic ignored "-Winconsistent-missing-override")
+# define OPENVINO_SUPPRESS_SUGGEST_OVERRIDE_END OPENVINO_DO_PRAGMA(clang diagnostic pop)
+#elif (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ > 405))
+# define OPENVINO_SUPPRESS_SUGGEST_OVERRIDE_START \
+ OPENVINO_DO_PRAGMA(GCC diagnostic push) \
+ OPENVINO_DO_PRAGMA(GCC diagnostic ignored "-Wsuggest-override")
+# define OPENVINO_SUPPRESS_SUGGEST_OVERRIDE_END OPENVINO_DO_PRAGMA(GCC diagnostic pop)
+#elif defined(_MSC_VER)
+# define OPENVINO_SUPPRESS_SUGGEST_OVERRIDE_START \
+ OPENVINO_DO_PRAGMA(warning(push)) \
+ OPENVINO_DO_PRAGMA(warning(disable : 4373))
+# define OPENVINO_SUPPRESS_SUGGEST_OVERRIDE_END OPENVINO_DO_PRAGMA(warning(pop))
+#else
+# define OPENVINO_SUPPRESS_SUGGEST_OVERRIDE_START
+# define OPENVINO_SUPPRESS_SUGGEST_OVERRIDE_END
+#endif
diff --git a/src/core/include/openvino/core/runtime_attribute.hpp b/src/core/include/openvino/core/runtime_attribute.hpp
index 86171a81562..5dc056c93ed 100644
--- a/src/core/include/openvino/core/runtime_attribute.hpp
+++ b/src/core/include/openvino/core/runtime_attribute.hpp
@@ -19,13 +19,8 @@ class Any;
class OPENVINO_API RuntimeAttribute {
public:
- _OPENVINO_HIDDEN_METHOD static const DiscreteTypeInfo& get_type_info_static() {
- static const ::ov::DiscreteTypeInfo type_info_static{"RuntimeAttribute"};
- return type_info_static;
- }
- virtual const DiscreteTypeInfo& get_type_info() const {
- return get_type_info_static();
- }
+ OPENVINO_RTTI_BASE("RuntimeAttribute")
+
using Ptr = std::shared_ptr<RuntimeAttribute>;
using Base = std::tuple<::ov::RuntimeAttribute>;
virtual ~RuntimeAttribute();
diff --git a/src/core/include/openvino/op/op.hpp b/src/core/include/openvino/op/op.hpp
index 917bee62533..c9eb6dc603d 100644
--- a/src/core/include/openvino/op/op.hpp
+++ b/src/core/include/openvino/op/op.hpp
@@ -33,19 +33,14 @@ namespace op {
/// \brief Root of all actual ops
/// \ingroup ov_ops_cpp_api
class OPENVINO_API Op : public Node {
+public:
+ OPENVINO_SUPPRESS_SUGGEST_OVERRIDE_START
+ OPENVINO_RTTI_BASE("Op", "util")
+ OPENVINO_SUPPRESS_SUGGEST_OVERRIDE_END
+
protected:
Op() : Node() {}
Op(const OutputVector& arguments);
-
-public:
- _OPENVINO_HIDDEN_METHOD static const ::ov::Node::type_info_t& get_type_info_static() {
- static ::ov::Node::type_info_t info{"Op", "util"};
- info.hash();
- return info;
- }
- const ::ov::Node::type_info_t& get_type_info() const override {
- return get_type_info_static();
- }
};
} // namespace op
} // namespace ov
diff --git a/src/core/tests/node_test.cpp b/src/core/tests/node_test.cpp
index f4f4e43e9ca..2beb87975cb 100644
--- a/src/core/tests/node_test.cpp
+++ b/src/core/tests/node_test.cpp
@@ -13,16 +13,9 @@ using namespace testing;
class TestNode : public ov::Node {
public:
TestNode() : Node() {}
-
- static const type_info_t& get_type_info_static() {
- static const type_info_t info{"TestNode", ""};
- info.hash();
- return info;
- }
-
- const type_info_t& get_type_info() const override {
- return get_type_info_static();
- }
+ OPENVINO_SUPPRESS_SUGGEST_OVERRIDE_START
+ OPENVINO_RTTI_BASE("TestNode", "")
+ OPENVINO_SUPPRESS_SUGGEST_OVERRIDE_END
std::shared_ptr<Node> clone_with_new_inputs(const ov::OutputVector&) const override {
return std::make_shared<TestNode>();