jaulib v1.3.8
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
jau::type_info Class Reference

Generic type information using either Runtime type information (RTTI) or Compile time type information (CTTI) More...

#include <type_info.hpp>

Collaboration diagram for jau::type_info:

Public Member Functions

 type_info () noexcept
 Constructor for an empty type_info instance, i.e.
 
 type_info (const char *signature_, bool identity_instance=false, bool identity_signature=false) noexcept
 Constructor using a const char* signature with a static storage duration.
 
 type_info (const std::type_info &info_, bool identity_instance=false) noexcept
 Constructor using an RTTI std::type_info reference, i.e.
 
constexpr size_t hash_code () const noexcept
 Returns an unspecified hash code of this instance.
 
constexpr bool identInst () const noexcept
 Returns true if this instance has a unique address (for same type_info).
 
constexpr bool identName () const noexcept
 Returns true if internal_name() has a unique identity address (for same signature strings).
 
constexpr const char * internal_name () const noexcept
 Returns the type name, compiler implementation specific.
 
std::string name () const noexcept
 Returns the demangled name of internal_name().
 
bool operator!= (const type_info &rhs) const noexcept
 
constexpr bool operator== (const type_info &rhs) const noexcept
 Return true if both instances are equal.
 
std::string toString () const noexcept
 

Static Public Member Functions

static void abort_invalid (const char *signature) noexcept
 Aborts program execution if given signature is nullptr or has a string length == 0.
 
static constexpr bool is_valid (const char *signature) noexcept
 Returns true if given signature is not nullptr and has a string length > 0, otherwise false.
 

Static Public Attributes

static constexpr const bool limited_lambda_id
 Static constexpr boolean indicating whether resulting type_info uniqueness is limited for lambda function types.
 

Detailed Description

Generic type information using either Runtime type information (RTTI) or Compile time type information (CTTI)

jau::type_info exposes same properties as RTTI std::type_index, i.e. can be used as index in associative and unordered associative containers and is CopyConstructible and CopyAssignable.

jau::type_info is compatible with std::type_index operations.

jau::type_info can be utilized w/o RTTI using Compile time type information (CTTI) information, i.e. JAU_PRETTY_FUNCTION via jau::ctti_name<R, L, A...>().

Consider using jau::make_ctti<R, L, A...>() for construction, as it removes the RTTI and CTTI code path differences.

RTTI and Compile time type information (CTTI) Natures

If RTTI is being used, see cxx_rtti_available, jau::type_info may be instantiated with a std::type_info reference as returned from typeid(T).

Without RTTI jau::type_info may be instantiated using JAU_PRETTY_FUNCTION. Hence utilizes Compile time type information (CTTI) only.

In both cases, jau::type_info will solely operate on the const char* signature and its hash value, aligning memory footprint and operations.

Use jau::make_ctti<R, L, A...>() for construction, as it removes the RTTI and CTTI code path differences.

Notes about lifecycle and identity

Prologue

We assume block scope and static storage duration (runtime lifecycle) of

  • (1) [typeid(T)]((https://en.cppreference.com/w/cpp/language/typeid)
    • (1.1) The typeid expression is an lvalue expression which refers to an object with static storage duration.
    • (1.2) There is no guarantee that the same std::type_info instance will be referred to by all evaluations of the typeid expression on the same type, although they would compare equal.
  • (2) JAU_PRETTY_FUNCTION, aka PRETTY_FUNCTION with properties of func
    • (2.1) This variable has block scope and static storage duration.

Equality Comparison

  • compare the static name storage references (pointer) and return true if equal (fast path)
  • due to (1.2), see above, the static name storage strings must be compared
    • compare the names' hash value and return false if not matching (fast path)
    • compare the static names' string and return the result (slow strcmp() equality)
      • this avoids a potential hash collision.

Limitations

Non unique lambda type names without RTTI using gcc or non clang compiler

Due to limitations of jau::make_ctti<R, L, A...>(), not using RTTI on gcc or non clang compiler will erroneously mistake different lambda functions defined within one function and using same function prototype R<A...> to be the same.

jau::type_info::limited_lambda_id exposes the potential limitation.

See also
Identity
jau::make_ctti<R, L, A...>

Definition at line 224 of file type_info.hpp.

Constructor & Destructor Documentation

◆ type_info() [1/3]

jau::type_info::type_info ( )
inlinenoexcept

Constructor for an empty type_info instance, i.e.

empty name() signature.

Definition at line 276 of file type_info.hpp.

Here is the caller graph for this function:

◆ type_info() [2/3]

jau::type_info::type_info ( const std::type_info & info_,
bool identity_instance = false )
inlinenoexcept

Constructor using an RTTI std::type_info reference, i.e.

typeid(T) result.

Consider using jau::make_ctti<R, L, A...>() for construction, as it removes the RTTI and CTTI code path differences.

Parameters
info_RTTI std::type_info reference
See also
jau::make_ctti<R, L, A...>

Definition at line 291 of file type_info.hpp.

◆ type_info() [3/3]

jau::type_info::type_info ( const char * signature_,
bool identity_instance = false,
bool identity_signature = false )
inlinenoexcept

Constructor using a const char* signature with a static storage duration.

Aborts program execution if given signature is nullptr or has a string length == 0.

Parameters
signature_valid string signature of type with length > 0 with static storage duration.
See also
jau::make_ctti<R, L, A...>

Definition at line 305 of file type_info.hpp.

Member Function Documentation

◆ is_valid()

static constexpr bool jau::type_info::is_valid ( const char * signature)
inlinestaticconstexprnoexcept

Returns true if given signature is not nullptr and has a string length > 0, otherwise false.

Examples
test_functional.hpp.

Definition at line 258 of file type_info.hpp.

Here is the caller graph for this function:

◆ abort_invalid()

static void jau::type_info::abort_invalid ( const char * signature)
inlinestaticnoexcept

Aborts program execution if given signature is nullptr or has a string length == 0.

Definition at line 263 of file type_info.hpp.

◆ operator==()

bool jau::type_info::operator== ( const type_info & rhs) const
inlineconstexprnoexcept

Return true if both instances are equal.

Parameters
rhs
Returns
See also
Identity

Definition at line 322 of file type_info.hpp.

Here is the caller graph for this function:

◆ operator!=()

bool jau::type_info::operator!= ( const type_info & rhs) const
inlinenoexcept

Definition at line 336 of file type_info.hpp.

◆ hash_code()

size_t jau::type_info::hash_code ( ) const
inlineconstexprnoexcept

Returns an unspecified hash code of this instance.

Properties

  • for all type_info objects referring to the same type, their hash code is the same.
  • type_info objects referring to different types may have the same hash code, i.e. due to hash collision.

Compatible with std::type_info definition.

Returns
Unspecified hash code of this instance.
See also
Identity

Definition at line 352 of file type_info.hpp.

Here is the caller graph for this function:

◆ identInst()

bool jau::type_info::identInst ( ) const
inlineconstexprnoexcept

Returns true if this instance has a unique address (for same type_info).

Definition at line 355 of file type_info.hpp.

Here is the caller graph for this function:

◆ identName()

bool jau::type_info::identName ( ) const
inlineconstexprnoexcept

Returns true if internal_name() has a unique identity address (for same signature strings).

Definition at line 358 of file type_info.hpp.

Here is the caller graph for this function:

◆ internal_name()

const char * jau::type_info::internal_name ( ) const
inlineconstexprnoexcept

Returns the type name, compiler implementation specific.

Examples
test_functional.hpp.

Definition at line 361 of file type_info.hpp.

Here is the caller graph for this function:

◆ name()

std::string jau::type_info::name ( ) const
inlinenoexcept

Returns the demangled name of internal_name().

Definition at line 365 of file type_info.hpp.

Here is the caller graph for this function:

◆ toString()

std::string jau::type_info::toString ( ) const
noexcept

Definition at line 919 of file basic_types.cpp.

Member Data Documentation

◆ limited_lambda_id

const bool jau::type_info::limited_lambda_id
staticconstexpr
Initial value:
=
true

Static constexpr boolean indicating whether resulting type_info uniqueness is limited for lambda function types.

Always is false if rtti_available == true, i.e. lambda function types are always unique using RTTI.

May return true if:

  • no RTTI and using gcc
  • no RTTI and not using clang
See also
CTTI lambda name limitations
Examples
test_functional.hpp, and test_functional_perf.hpp.

Definition at line 244 of file type_info.hpp.


The documentation for this class was generated from the following files: