Skip to content

Commit

Permalink
Mark RTTI methods as const / pure, so compiler could optimize subsequ…
Browse files Browse the repository at this point in the history
…ent calls, if necessary.

Signed-off-by: Anton Korobeynikov <[email protected]>
  • Loading branch information
asl committed Dec 11, 2024
1 parent 650af7e commit 8b2c0e7
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions lib/rtti.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,20 @@ struct Base {
}; \
DECLARE_TYPEINFO_COMMON(T, ##__VA_ARGS__)

#define DECLARE_TYPEINFO_COMMON(T, ...) \
public: \
static constexpr T *rttiEnabledMarker(T *); \
using TypeInfo = P4::RTTI::TypeInfo<T, ##__VA_ARGS__>; \
[[nodiscard]] P4::RTTI::TypeId typeId() const noexcept override { return TypeInfo::id(); } \
[[nodiscard]] bool isA(P4::RTTI::TypeId typeId) const noexcept override { \
return TypeInfo::isA(typeId); \
} \
\
protected: \
[[nodiscard]] const void *toImpl(P4::RTTI::TypeId typeId) const noexcept override { \
return TypeInfo::isA(typeId) ? TypeInfo::dyn_cast(typeId, this) : nullptr; \
#define DECLARE_TYPEINFO_COMMON(T, ...) \
public: \
static constexpr T *rttiEnabledMarker(T *); \
using TypeInfo = P4::RTTI::TypeInfo<T, ##__VA_ARGS__>; \
[[nodiscard, gnu::const]] P4::RTTI::TypeId typeId() const noexcept override { \
return TypeInfo::id(); \
} \
[[nodiscard, gnu::const]] bool isA(P4::RTTI::TypeId typeId) const noexcept override { \
return TypeInfo::isA(typeId); \
} \
\
protected: \
[[nodiscard, gnu::pure]] const void *toImpl(P4::RTTI::TypeId typeId) const noexcept override { \
return TypeInfo::isA(typeId) ? TypeInfo::dyn_cast(typeId, this) : nullptr; \
}

#endif /* LIB_RTTI_H_ */

0 comments on commit 8b2c0e7

Please sign in to comment.