Caffe2 - C++ API
A deep learning, cross platform ML framework
typeid.cpp
1 #include <c10/util/typeid.h>
2 #include <c10/util/Exception.h>
3 
4 #include <atomic>
5 
6 #if !defined(_MSC_VER)
7 #include <cxxabi.h>
8 #endif
9 
10 using std::string;
11 
12 namespace caffe2 {
13 namespace detail {
14 C10_EXPORT void _ThrowRuntimeTypeLogicError(const string& msg) {
15  // In earlier versions it used to be std::abort() but it's a bit hard-core
16  // for a library
17  AT_ERROR(msg);
18 }
19 
20 const TypeMetaData _typeMetaDataInstance_uninitialized_ = detail::TypeMetaData(0, nullptr, nullptr, nullptr, nullptr, nullptr, TypeIdentifier::uninitialized(), "nullptr (uninitialized)");
21 
22 } // namespace detail
23 
24 // TODO Inlineable on non-MSVC like other preallocated ids?
25 template<>
26 C10_EXPORT const detail::TypeMetaData* TypeMeta::_typeMetaDataInstance<detail::_Uninitialized>() noexcept {
27  return &detail::_typeMetaDataInstance_uninitialized_;
28 }
29 
30 
31 TypeIdentifier TypeIdentifier::createTypeId() {
32  static std::atomic<TypeIdentifier::underlying_type> counter(
33  TypeMeta::Id<_CaffeHighestPreallocatedTypeId>().underlyingId());
34  const TypeIdentifier::underlying_type new_value = ++counter;
35  if (new_value ==
36  std::numeric_limits<TypeIdentifier::underlying_type>::max()) {
37  throw std::logic_error(
38  "Ran out of available type ids. If you need more than 2^16 CAFFE_KNOWN_TYPEs, we need to increase TypeIdentifier to use more than 16 bit.");
39  }
40  return TypeIdentifier(new_value);
41 }
42 
43 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(0, uint8_t)
44 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(1, int8_t)
45 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(2, int16_t)
46 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(3, int)
47 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(4, int64_t)
48 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(5, at::Half)
49 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(6, float)
50 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(7, double)
51 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(8, at::ComplexHalf)
52 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(9, std::complex<float>)
53 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(10, std::complex<double>)
54 // 11 = undefined type id
55 // 12 = Tensor (defined in tensor.cc)
56 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(13, std::string)
57 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(14, bool)
58 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(15, uint16_t)
59 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(16, char)
60 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(17, std::unique_ptr<std::mutex>)
61 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(18, std::unique_ptr<std::atomic<bool>>)
62 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(19, std::vector<int32_t>)
63 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(20, std::vector<int64_t>)
64 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(21, std::vector<unsigned long>)
65 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(22, bool*)
66 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(23, char*)
67 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(24, int*)
68 
69 // see typeid.h for details.
70 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(25, detail::_guard_long_unique<long>);
71 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(
72  26,
73  detail::_guard_long_unique<std::vector<long>>);
74 
75 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(27, _CaffeHighestPreallocatedTypeId)
76 
77 } // namespace caffe2
A type id is a unique id for a given C++ type.
Definition: typeid.h:60
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13