/src/brpc/src/butil/class_name.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | // Date: Mon. Nov 7 14:47:36 CST 2011 |
19 | | |
20 | | // Get name of a class. For example, class_name<T>() returns the name of T |
21 | | // (with namespace prefixes). This is useful in template classes. |
22 | | |
23 | | #ifndef BUTIL_CLASS_NAME_H |
24 | | #define BUTIL_CLASS_NAME_H |
25 | | |
26 | | #include <typeinfo> |
27 | | #include <string> // std::string |
28 | | |
29 | | namespace butil { |
30 | | |
31 | | std::string demangle(const char* name); |
32 | | |
33 | | namespace { |
34 | | template <typename T> struct ClassNameHelper { static std::string name; }; |
35 | | template <typename T> std::string ClassNameHelper<T>::name = demangle(typeid(T).name()); |
36 | | } |
37 | | |
38 | | // Get name of class |T|, in std::string. |
39 | 0 | template <typename T> const std::string& class_name_str() { |
40 | | // We don't use static-variable-inside-function because before C++11 |
41 | | // local static variable is not guaranteed to be thread-safe. |
42 | 0 | return ClassNameHelper<T>::name; |
43 | 0 | } Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<long>() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<bvar::detail::MaxTo<long> >() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<bvar::detail::AddTo<long> >() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<int>() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<bvar::detail::AddTo<int> >() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<brpc::Socket>() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<brpc::IOEventData>() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<bvar::Collected*>() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<bvar::CombineCollected>() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<bvar::detail::Sampler*>() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<bvar::detail::CombineSampler>() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<unsigned long>() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<bvar::detail::AddTo<unsigned long> >() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<brpc::BaiduMasterService>() Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& butil::class_name_str<brpc::NsheadService>() |
44 | | |
45 | | // Get name of class |T|, in const char*. |
46 | | // Address of returned name never changes. |
47 | 0 | template <typename T> const char* class_name() { |
48 | 0 | return class_name_str<T>().c_str(); |
49 | 0 | } Unexecuted instantiation: char const* butil::class_name<brpc::Socket>() Unexecuted instantiation: char const* butil::class_name<brpc::IOEventData>() Unexecuted instantiation: char const* butil::class_name<brpc::BaiduMasterService>() |
50 | | |
51 | | // Get typename of |obj|, in std::string |
52 | 0 | template <typename T> std::string class_name_str(T const& obj) { |
53 | 0 | return demangle(typeid(obj).name()); |
54 | 0 | } Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > butil::class_name_str<brpc::Describable>(brpc::Describable const&) Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > butil::class_name_str<brpc::NonConstDescribable>(brpc::NonConstDescribable const&) Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > butil::class_name_str<brpc::SocketUser>(brpc::SocketUser const&) Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > butil::class_name_str<brpc::Destroyable>(brpc::Destroyable const&) Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > butil::class_name_str<google::protobuf::Service>(google::protobuf::Service const&) Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > butil::class_name_str<brpc::NsheadService>(brpc::NsheadService const&) Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > butil::class_name_str<brpc::RtmpService>(brpc::RtmpService const&) Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > butil::class_name_str<brpc::RedisService>(brpc::RedisService const&) Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > butil::class_name_str<brpc::BaiduMasterService>(brpc::BaiduMasterService const&) Unexecuted instantiation: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > butil::class_name_str<brpc::NamingService>(brpc::NamingService const&) |
55 | | |
56 | | } // namespace butil |
57 | | |
58 | | #endif // BUTIL_CLASS_NAME_H |