Program Listing for File convertible.h

Return to documentation for file (include\playback\resource-info\detail\convertible.h)

/******************************************************************************

© Intel Corporation.

This software and the related documents are Intel copyrighted materials,
and your use of them is governed by the express license under which they
were provided to you ("License"). Unless the License provides otherwise,
you may not use, modify, copy, publish, distribute, disclose or transmit
this software or the related documents without Intel's prior written
permission.

 This software and the related documents are provided as is, with no express
or implied warranties, other than those that are expressly stated in the
License.

******************************************************************************/

#pragma once

#include "playback/resource-info/detail/type-registry.h"

#include <type_traits>

namespace gpa {
namespace playback {
namespace detail {

template<typename BaseType, typename... DerivedTypes>
class Convertible
{
public:
    virtual ~Convertible() = 0;

    inline virtual TypeId GetTypeId() const
    {
        return 0;
    }

    template<typename DerivedType>
    inline DerivedType const* As() const
    {
        static_assert(
            std::is_base_of<BaseType, DerivedType>::value,
            "As<>() may only be used with derived types");
        return GetTypeId() == GetTypeId<DerivedType>() ? (DerivedType const*)this : nullptr;
    }

    template<typename DerivedType>
    inline DerivedType* As()
    {
        static_assert(
            std::is_base_of<BaseType, DerivedType>::value,
            "As<>() may only be used with derived types");
        return GetTypeId() == GetTypeId<DerivedType>() ? (DerivedType*)this : nullptr;
    }

    template<typename DerivedType, typename ProcessAsFunctionType>
    inline bool ProcessAs(ProcessAsFunctionType processAsFunction) const
    {
        auto pDerived = this->As<DerivedType>();
        if (pDerived) {
            processAsFunction(*pDerived);
        }
        return pDerived != nullptr;
    }

    template<typename DerivedType>
    inline static constexpr TypeId GetTypeId()
    {
        static_assert(
            std::is_base_of<BaseType, DerivedType>::value,
            "GetTypeId<>() may only be used with derived types");
        return detail::GetTypeId<DerivedType>(TypeRegistry<DerivedTypes...>{});
    }
};

template<typename BaseType, typename... DerivedTypes>
inline Convertible<BaseType, DerivedTypes...>::~Convertible()
{
}

}  // namespace detail
}  // namespace playback
}  // namespace gpa