从 type_id_with_cv<>()::p retty_name() 中删除命名空间

Remove namespace from type_id_with_cv<>()::pretty_name()

本文关键字:name 命名空间 删除 retty gt id type with cv lt      更新时间:2023-10-16

我使用以下代码来检索类的名称:

template <typename T>
string GetName(const T& object) {
    using type = typename remove_const<typename remove_reference<decltype(object)>::type>::type;
    return boost::typeindex::type_id_with_cvr<type>().pretty_name();
}

代码运行良好。但是,返回的字符串还包含命名空间。是否有仅返回类名的 boost 函数?我知道我可以自己写,关键是不要重新发明轮子。

这是轮子的另一项发明,基本上,它利用了命名空间结构和目录结构之间的相似性,但可怕的但快速的解决方案。

#include<boost/type_index.hpp>
#include<boost/filesystem/path.hpp>
namespace A{
    namespace B{
        struct C{};
    }
}

int main(){   
    std::string s = boost::typeindex::type_id_with_cvr<A::B::C const*&>().pretty_name();
    replace(begin(s), end(s), ':', '/');
    replace(begin(s), end(s), ' ', '.');
    using namespace boost::filesystem;
    assert( path(s).string() == "A//B//C.const*&" );
    assert( path(s).filename() == "C.const*&" );
    assert( path(s).extension() == ".const*&" );
    assert( path(s).stem() == "C" );
    assert( path(s).parent_path() == "A//B");
    assert( path(s).parent_path().parent_path() == "A");
}

或者把它打包成一个简单的丑陋类:

#include<boost/type_index.hpp>
#include<boost/filesystem/path.hpp>
namespace A{
    namespace B{
        struct C{};
    }
}
template<class T>
class typename_info_t{
    boost::filesystem::path p_;
public:
    typename_info_t(){
        std::string s = boost::typeindex::type_id_with_cvr<T>().pretty_name();
        replace(begin(s), end(s), ':', '/');
        replace(begin(s), end(s), ' ', '.');
        p_ = s;
    }
    std::string full_name() const{
        std::string s = p_.string();
        replace(begin(s), end(s), '/', ':');
        replace(begin(s), end(s), '.', ' ');
        return s;
    }
    std::string namespace_name() const{
        std::string s = p_.parent_path().string();
        replace(begin(s), end(s), '/', ':');
        return s;
    }
    std::string class_name() const{
        std::string s = p_.stem().string();
        replace(begin(s), end(s), '/', ':');
        return s;
    }
    std::string class_name_with_cvr() const{
        std::string s = p_.filename().string();
        replace(begin(s), end(s), '/', ':');
        replace(begin(s), end(s), '.', ' ');
        return s;
    }
    std::string cvr() const{ // begins with space
        std::string s = p_.extension().string();
        replace(begin(s), end(s), '.', ' ');
        return s;
    }
};
template<class TT> typename_info_t<TT> typename_info(){return {};}

int main(){
    assert( typename_info<A::B::C const*&>().full_name() == "A::B::C const*&" );
    assert( typename_info<A::B::C const*&>().namespace_name() == "A::B" );
    assert( typename_info<A::B::C const*&>().class_name() == "C" );
    assert( typename_info<A::B::C const*&>().class_name_with_cvr() == "C const*&" );
    assert( typename_info<A::B::C const*&>().cvr() == " const*&");
}

(限制:"*"指针限定符不是此处类名的一部分,不幸的是,这强制使用非仅标头库的#include<boost/filesystem.hpp>#include<experimental/filesystem>