Caffe2 - C++ API
A deep learning, cross platform ML framework
typeid.cc
1 #include "caffe2/core/typeid.h"
2 #include "caffe2/core/scope_guard.h"
3 
4 #if !defined(_MSC_VER)
5 #include <cxxabi.h>
6 #endif
7 
8 namespace caffe2 {
9 std::map<CaffeTypeId, string>& gTypeNames() {
10  static std::map<CaffeTypeId, string> g_type_names;
11  return g_type_names;
12 }
13 
14 std::set<string>& gRegisteredTypeNames() {
15  static std::set<string> g_registered_type_names;
16  return g_registered_type_names;
17 }
18 
19 std::mutex& gCaffe2TypeRegistrationMutex() {
20  static std::mutex g_caffe2_type_registration_mutex;
21  return g_caffe2_type_registration_mutex;
22 }
23 
24 #if defined(_MSC_VER)
25 // Windows does not have cxxabi.h, so we will simply return the original.
26 string Demangle(const char* name) {
27  return string(name);
28 }
29 #else
30 string Demangle(const char* name) {
31  int status = 0;
32  auto demangled = ::abi::__cxa_demangle(name, nullptr, nullptr, &status);
33  if (demangled) {
34  auto guard = MakeGuard([demangled]() { free(demangled); });
35  return string(demangled);
36  }
37  return name;
38 }
39 #endif
40 
41 string GetExceptionString(const std::exception& e) {
42 #ifdef __GXX_RTTI
43  return Demangle(typeid(e).name()) + ": " + e.what();
44 #else
45  return string("Exception (no RTTI available): ") + e.what();
46 #endif // __GXX_RTTI
47 }
48 
49 namespace {
50 // This single registerer exists solely for us to be able to name a TypeMeta
51 // for unintializied blob. You should not use this struct yourself - it is
52 // intended to be only instantiated once here.
53 struct UninitializedTypeNameRegisterer {
54  UninitializedTypeNameRegisterer() {
55  gTypeNames()[0] = "nullptr (uninitialized)";
56  }
57 };
58 static UninitializedTypeNameRegisterer g_uninitialized_type_name_registerer;
59 
60 } // namespace
61 } // namespace caffe2
detail::ScopeGuardImplDecay< F > MakeGuard(F &&f) noexcept(noexcept(detail::ScopeGuardImplDecay< F >(static_cast< F && >(f))))
ScopeGuard is a general implementation of the "Initialization is Resource Acquisition" idiom...
Definition: scope_guard.h:153
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...