| Home | Libraries | People | FAQ | More

## Class template type_index_facade

boost::typeindex::type_index_facade

## Synopsis

    // In header: <boost/type_index/type_index_facade.hpp>

    template<typename Derived, typename TypeInfo>
    class type_index_facade {
    public:
      // types
      typedef TypeInfo type_info_t;

      // public member functions
      const char * name() const noexcept;
      std::string pretty_name() const;
      bool equal(const Derived &) const noexcept;
      bool before(const Derived &) const noexcept;
      std::size_t hash_code() const noexcept;

      // protected member functions
      const char * raw_name() const noexcept;
      const type_info_t & type_info() const noexcept;

      // protected static functions
      template<typename T> static Derived type_id() noexcept;
      template<typename T> static Derived type_id_with_cvr() noexcept;
      template<typename T> static Derived type_id_runtime(const T &) noexcept;
    };

## Description

This class takes care about the comparison operators, hash functions and ostream operators. Use this class as a public base class for defining new type_info-conforming classes.

Example:

    class stl_type_index: public type_index_facade<stl_type_index, std::type_info>
    {
    public:
        typedef std::type_info type_info_t;
    private:
        const type_info_t* data_;

    public:
        stl_type_index(const type_info_t& data) noexcept
            : data_(&data)
        {}
    // ...
    };

| Note
Take a look at the protected methods. They are not defined in type_index_facade. Protected member functions raw_name() must be defined in Derived class. All the other methods are mandatory.

See Also:

'Making a custom type_index' section for more information about creating your own type_index using type_index_facade.

### Template Parameters

1.     typename Derived

   Class derived from `type_index_facade`.

2.     typename TypeInfo

   Class that will be used as a base type_info class.

### `type_index_facade` public member functions

1.     const char * name() const noexcept;

   Override: This function may be redefined in Derived class. Overrides must not throw.

   Returns: | Name of a type. By default returns Derived::raw_name().

2.     std::string pretty_name() const;

   Override: This function may be redefined in Derived class. Overrides may throw.

   Returns: | Human readable type name. By default returns Derived::name().

3.     bool equal(const Derived & rhs) const noexcept;

   Override: This function may be redefined in Derived class. Overrides must not throw.

   Returns: | True if two types are equal. By default compares types by raw_name().

4.     bool before(const Derived & rhs) const noexcept;

   Override: This function may be redefined in Derived class. Overrides must not throw.

   Returns: | True if rhs is greater than this. By default compares types by raw_name().

5.     std::size_t hash_code() const noexcept;

   Override: This function may be redefined in Derived class. Overrides must not throw.

   | Note
   <boost/functional/hash.hpp> has to be included if this function is used.

   Returns: | Hash code of a type. By default hashes types by raw_name().

### `type_index_facade` protected member functions

1.     const char * raw_name() const noexcept;

   Override: This function must be redefined in Derived class. Overrides must not throw.

   Returns: | Pointer to unredable/raw type name.

2.     const type_info_t & type_info() const noexcept;

   Override: This function may be redefined in Derived class. Overrides must not throw.

   Returns: | Const reference to underlying low level type_info_t.

### `type_index_facade` protected static functions

1.     template<typename T> static Derived type_id() noexcept;

   This is a factory method that is used to create instances of Derived classes. boost::typeindex::type_id() will call this method, if Derived has same type as boost::typeindex::type_index.

   Override: This function may be redefined and made public in Derived class. Overrides must not throw. Overrides must remove const, volatile && and & modifiers from T.

   Template Parameters:

   `T` | Type for which type_index must be created.

   Returns: | type_index for type T.

2.     template<typename T> static Derived type_id_with_cvr() noexcept;

   This is a factory method that is used to create instances of Derived classes. boost::typeindex::type_id_with_cvr() will call this method, if Derived has same type as boost::typeindex::type_index.

   Override: This function may be redefined and made public in Derived class. Overrides must not throw. Overrides must not remove const, volatile && and & modifiers from T.

   Template Parameters:

   `T` | Type for which type_index must be created.

   Returns: | type_index for type T.

3.     template<typename T>
         static Derived type_id_runtime(const T & variable) noexcept;

   This is a factory method that is used to create instances of Derived classes. boost::typeindex::type_id_runtime(const T&) will call this method, if Derived has same type as boost::typeindex::type_index.

   Override: This function may be redefined and made public in Derived class.

   Parameters:

   `variable` | Variable which runtime type will be stored in type_index.

   Returns: | type_index with runtime type of variable.