//------------------------------------------------------------------------------
/*
    This file is part of Beast: https://github.com/vinniefalco/Beast
    Copyright 2014, Howard Hinnant <howard.hinnant@gmail.com>

    Permission to use, copy, modify, and/or distribute this software for any
    purpose  with  or without fee is hereby granted, provided that the above
    copyright notice and this permission notice appear in all copies.

    THE  SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    WITH  REGARD  TO  THIS  SOFTWARE  INCLUDING  ALL  IMPLIED  WARRANTIES  OF
    MERCHANTABILITY  AND  FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    ANY  SPECIAL ,  DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    WHATSOEVER  RESULTING  FROM  LOSS  OF USE, DATA OR PROFITS, WHETHER IN AN
    ACTION  OF  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#ifndef BEAST_TYPE_NAME_H_INCLUDED
#define BEAST_TYPE_NAME_H_INCLUDED

#include <cstdlib>
#include <string>
#include <type_traits>
#include <typeinfo>

#ifndef _MSC_VER
#include <cxxabi.h>
#endif

namespace beast {

template <typename T>
std::string
type_name()
{
    using TR = typename std::remove_reference<T>::type;

    std::string name = typeid(TR).name();

#ifndef _MSC_VER
    if (auto s = abi::__cxa_demangle(name.c_str(), nullptr, nullptr, nullptr))
    {
        name = s;
        std::free(s);
    }
#endif

    if (std::is_const<TR>::value)
        name += " const";
    if (std::is_volatile<TR>::value)
        name += " volatile";
    if (std::is_lvalue_reference<T>::value)
        name += "&";
    else if (std::is_rvalue_reference<T>::value)
        name += "&&";

    return name;
}

}  // namespace beast

#endif
