P4C
The P4 Compiler
Loading...
Searching...
No Matches
P4::ICastable Class Reference

#include <castable.h>

Inheritance diagram for P4::ICastable:
[legend]

Public Member Functions

template<typename T >
T & as ()
 Tries to convert the class to type T. A BUG occurs if the cast fails.
 
template<typename T >
const T & as () const
 Tries to convert the class to type T. A BUG occurs if the cast fails.
 
template<typename T >
T * checkedTo ()
 Performs a checked cast. A BUG occurs if the cast fails.
 
template<typename T >
const T * checkedTo () const
 Performs a checked cast. A BUG occurs if the cast fails.
 
- Public Member Functions inherited from P4::RTTI::Base
template<typename T >
bool is () const noexcept
 
virtual bool isA (TypeId typeId) const noexcept=0
 
template<typename T >
const T * to () const noexcept
 Same as to, but returns const pointer to T.
 
template<typename T >
T * to () noexcept
 
virtual TypeId typeId () const noexcept=0
 

Additional Inherited Members

- Protected Member Functions inherited from P4::RTTI::Base
virtual const void * toImpl (TypeId typeId) const noexcept=0
 

Detailed Description

Handy type conversion methods that can be inherited by various base classes. In order to use ICastable one also need to provide intrusive lightweight RTTI metadata for the given class hierarchy. See docs/C++.md for more information, but in short, one need to use DECLARE_TYPEINFO() macro for this. There is no DECLARE_TYPEINFO for ICastable itself as we are not expecting pointers neither to ICastable nor to RTTI::Base to appear within the codebase. As the same time, one should not cast to ICastable as well (via e.g. ->to<ICastable>(). Use DECLARE_TYPEINFO without bases to specify the base class for a given herarchy.