#ifndef _TILE_BASE_DOWN_CAST_H
#define _TILE_BASE_DOWN_CAST_H

#pragma once

#include "tile/base/internal/logging.h"

#include <type_traits>

namespace tile {
template<typename To,
         typename From,
         typename R = typename std::conditional<std::is_const<From>::value, const To *, To *>::type>
inline R
down_cast(From *ptr)
{
    TILE_DCHECK(dynamic_cast<R>(ptr));
    return static_cast<R>(ptr);
}

template<typename To, typename From>
inline auto
down_cast(From &ref) -> decltype(down_cast(&ref))
{
    return down_cast<To>(&ref);
}
}// namespace tile

#endif// _TILE_BASE_DOWN_CAST_H
