Skip to content

Commit

Permalink
Reformatted codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
royvandam committed Dec 10, 2020
1 parent f46ff42 commit 5a9812c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
12 changes: 7 additions & 5 deletions benchmark/rtti_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ struct ParentB : virtual GrandParent {
RTTI_DECLARE_TYPEINFO(ParentB, GrandParent);
};

struct Child : ParentA, ParentB {
struct Child
: ParentA
, ParentB {
RTTI_DECLARE_TYPEINFO(Child, ParentA, ParentB);
};

Expand All @@ -28,10 +30,10 @@ NativeDynamicCast(benchmark::State& state) {
GrandParent* g;
benchmark::DoNotOptimize(g = dynamic_cast<GrandParent*>(&c));

ParentA *pa;
ParentA* pa;
benchmark::DoNotOptimize(pa = dynamic_cast<ParentA*>(g));

ParentB *pb;
ParentB* pb;
benchmark::DoNotOptimize(pb = dynamic_cast<ParentB*>(g));

InvalidCast* invalid;
Expand All @@ -48,10 +50,10 @@ RttiDynamicCast(benchmark::State& state) {
GrandParent* g;
benchmark::DoNotOptimize(g = c.cast<GrandParent>());

ParentA *pa;
ParentA* pa;
benchmark::DoNotOptimize(pa = g->cast<ParentA>());

ParentB *pb;
ParentB* pb;
benchmark::DoNotOptimize(pb = g->cast<ParentB>());

InvalidCast* invalid;
Expand Down
49 changes: 25 additions & 24 deletions include/rtti.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

#pragma once

#include <cstdint>
#include <type_traits>
#include <algorithm>
#include <array>
#include <cstdint>
#include <type_traits>

namespace RTTI {
/// Forward declaration of the Enable base.
Expand All @@ -37,9 +37,11 @@ namespace RTTI {
using TypeId = uintptr_t;

namespace Detail {
template<typename T>
struct Type { static constexpr int Id = 0; };
}
template <typename T>
struct Type {
static constexpr int Id = 0;
};
} // namespace Detail

/**
* Static typeinfo structure for registering types and accessing their information.
Expand All @@ -48,11 +50,11 @@ namespace RTTI {
struct TypeInfo {
/// Ensure all passed parents are basses of this type.
static_assert((... && std::is_base_of<Parents, This>::value),
"One or more parents are not a base of this type.");
"One or more parents are not a base of this type.");

/// Ensure all passed parent hierarchies have RTTI enabled.
static_assert((... && std::is_base_of<Enable, Parents>::value),
"One or more parent hierarchies is not based on top of RTTI::Enable.");
"One or more parent hierarchies is not based on top of RTTI::Enable.");

/**
* Returns the type identifier of the type T.
Expand Down Expand Up @@ -115,15 +117,15 @@ namespace RTTI {
* Returns the type identifier of the object.
* @returns Type identifier
*/
[[nodiscard]] virtual TypeId typeId() const noexcept =0;
[[nodiscard]] virtual TypeId typeId() const noexcept = 0;

/**
* Checks whether the object is a direct or derived instance of
* the type identified by the passed identifier.
* @tparam The identifier to compare with.
* @returns True in case a match was found.
*/
[[nodiscard]] virtual bool isById(TypeId typeId) const noexcept =0;
[[nodiscard]] virtual bool isById(TypeId typeId) const noexcept = 0;

/**
* Checks whether the object is an instance of child instance of
Expand Down Expand Up @@ -180,18 +182,17 @@ namespace RTTI {
* @param T The type it self.
* @param Parents Variadic number of direct parrent types of the type
*/
#define RTTI_DECLARE_TYPEINFO(T, ...) \
public: \
using TypeInfo = RTTI::TypeInfo<T, ##__VA_ARGS__>; \
[[nodiscard]] virtual RTTI::TypeId typeId() const noexcept override { \
return TypeInfo::Id(); \
} \
[[nodiscard]] virtual bool isById(RTTI::TypeId typeId) const noexcept override { \
return TypeInfo::Is(typeId); \
} \
protected: \
[[nodiscard]] virtual void const* _cast(RTTI::TypeId typeId) const noexcept override { \
return TypeInfo::DynamicCast(typeId, this); \
}


#define RTTI_DECLARE_TYPEINFO(T, ...) \
public: \
using TypeInfo = RTTI::TypeInfo<T, ##__VA_ARGS__>; \
[[nodiscard]] virtual RTTI::TypeId typeId() const noexcept override { \
return TypeInfo::Id(); \
} \
[[nodiscard]] virtual bool isById(RTTI::TypeId typeId) const noexcept override { \
return TypeInfo::Is(typeId); \
} \
\
protected: \
[[nodiscard]] virtual void const* _cast(RTTI::TypeId typeId) const noexcept override { \
return TypeInfo::DynamicCast(typeId, this); \
}
8 changes: 6 additions & 2 deletions tests/hierarchy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ struct ParentB : virtual GrandParent {
RTTI_DECLARE_TYPEINFO(ParentB, GrandParent);
};

struct ChildA : ParentA, ParentB {
struct ChildA
: ParentA
, ParentB {
RTTI_DECLARE_TYPEINFO(ChildA, ParentA, ParentB);
};

struct ChildB : ParentA, ParentB {
struct ChildB
: ParentA
, ParentB {
RTTI_DECLARE_TYPEINFO(ChildB, ParentA, ParentB);
};

Expand Down

0 comments on commit 5a9812c

Please sign in to comment.