Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions include/tscore/ink_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,51 @@ inkcoreapi void _ink_assert(const char *a, const char *f, int l) TS_NORETURN;

#ifdef __cplusplus
}
#endif /* __cplusplus */

/* workaround a bug in the stupid Sun preprocessor
/*
Use cast_to_derived() to cast a pointer/reference to a dynamic base class into a pointer/reference to a class
that inherits directly or indirectly from the base class. Uses checked dynamic_cast in debug builds, and
static_cast in release (optimized) builds. Use examples:

class A { public: virtual ~A(); };
class B : public A {};
B * foo(A *a) { return cast_to_derived<B>(a); }
B & foo2(A &a) { return cast_to_derived<B>(a); }
B const & foo3(A const &a) { return cast_to_derived<B const>(a); }

#undef assert
#define assert __DONT_USE_BARE_assert_USE_ink_assert__
#define _ASSERT_H
#undef __ASSERT_H__
#define __ASSERT_H__
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted these kruft comments while I was in here.

*/

template <class Derived, class Base>
Derived *
cast_to_derived(Base *b) // b must not be nullptr.
{
#ifdef DEBUG

ink_assert(b != nullptr);
auto d = dynamic_cast<Derived *>(b);
ink_assert(d != nullptr);
return d;

#else

return static_cast<Derived *>(b);

#endif
}

template <class Derived, class Base>
Derived &
cast_to_derived(Base &b)
{
#ifdef DEBUG

return dynamic_cast<Derived &>(b);

#else

return static_cast<Derived &>(b);

#endif
}

#endif /* __cplusplus */