Caffe2 - C++ API
A deep learning, cross platform ML framework
typeid.cc
1 
17 #include "caffe2/core/typeid.h"
18 #include "caffe2/core/scope_guard.h"
19 
20 #if !defined(_MSC_VER)
21 #include <cxxabi.h>
22 #endif
23 
24 namespace caffe2 {
25 std::map<CaffeTypeId, string>& gTypeNames() {
26  static std::map<CaffeTypeId, string> g_type_names;
27  return g_type_names;
28 }
29 
30 std::set<string>& gRegisteredTypeNames() {
31  static std::set<string> g_registered_type_names;
32  return g_registered_type_names;
33 }
34 
35 std::mutex& gCaffe2TypeRegistrationMutex() {
36  static std::mutex g_caffe2_type_registration_mutex;
37  return g_caffe2_type_registration_mutex;
38 }
39 
40 #if defined(_MSC_VER)
41 // Windows does not have cxxabi.h, so we will simply return the original.
42 string Demangle(const char* name) {
43  return string(name);
44 }
45 #else
46 string Demangle(const char* name) {
47  int status = 0;
48  auto demangled = ::abi::__cxa_demangle(name, nullptr, nullptr, &status);
49  if (demangled) {
50  auto guard = MakeGuard([demangled]() { free(demangled); });
51  return string(demangled);
52  }
53  return name;
54 }
55 #endif
56 
57 string GetExceptionString(const std::exception& e) {
58 #ifdef __GXX_RTTI
59  return Demangle(typeid(e).name()) + ": " + e.what();
60 #else
61  return string("Exception (no RTTI available): ") + e.what();
62 #endif // __GXX_RTTI
63 }
64 
65 namespace {
66 // This single registerer exists solely for us to be able to name a TypeMeta
67 // for unintializied blob. You should not use this struct yourself - it is
68 // intended to be only instantiated once here.
69 struct UninitializedTypeNameRegisterer {
70  UninitializedTypeNameRegisterer() {
71  gTypeNames()[0] = "nullptr (uninitialized)";
72  }
73 };
74 static UninitializedTypeNameRegisterer g_uninitialized_type_name_registerer;
75 
76 } // namespace
77 } // 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:169
Copyright (c) 2016-present, Facebook, Inc.