1 #ifndef CAFFE2_CORE_TYPEID_H_ 2 #define CAFFE2_CORE_TYPEID_H_ 17 #include "caffe2/core/common.h" 21 typedef intptr_t CaffeTypeId;
22 std::map<CaffeTypeId, string>& gTypeNames();
23 std::set<string>& gRegisteredTypeNames();
26 string Demangle(
const char* name);
34 static const char* DemangleType() {
36 static const string name = Demangle(
typeid(T).name());
39 return "(RTTI disabled, cannot show name)";
45 string GetExceptionString(
const std::exception& e);
47 std::mutex& gCaffe2TypeRegistrationMutex();
52 std::lock_guard<std::mutex> guard(gCaffe2TypeRegistrationMutex());
56 string name = Demangle(
typeid(T).name());
63 if (gRegisteredTypeNames().count(name)) {
64 std::cerr <<
"Type name " << name
65 <<
" registered twice. This should " 66 "not happen. Do you have duplicated CAFFE_KNOWN_TYPE?" 68 throw std::runtime_error(
"TypeNameRegisterer error with type " + name);
70 gRegisteredTypeNames().insert(name);
71 gTypeNames()[id] = name;
73 if (literal_name.empty()) {
74 gTypeNames()[id] =
"(RTTI disabled, cannot show name)";
76 gTypeNames()[id] = literal_name;
90 typedef void (*PlacementNew)(
void*, size_t);
91 typedef void (*TypedCopy)(
const void*,
void*, size_t);
92 typedef void (*TypedDestructor)(
void*, size_t);
97 : id_(0), itemsize_(0), ctor_(nullptr), copy_(nullptr), dtor_(nullptr) {}
104 itemsize_(src.itemsize_),
115 itemsize_ = src.itemsize_;
130 TypedDestructor dtor)
131 : id_(i), itemsize_(s), ctor_(ctor), copy_(copy), dtor_(dtor) {}
137 inline const CaffeTypeId&
id()
const {
149 inline PlacementNew
ctor()
const {
155 inline TypedCopy
copy()
const {
161 inline TypedDestructor
dtor()
const {
167 inline const char*
name()
const {
168 auto it = gTypeNames().find(id_);
169 assert(it != gTypeNames().end());
170 return it->second.c_str();
172 inline bool operator==(
const TypeMeta& m)
const {
173 return (id_ == m.id_);
175 inline bool operator!=(
const TypeMeta& m)
const {
176 return (id_ != m.id_);
179 template <
typename T>
180 inline bool Match()
const {
181 return (id_ == Id<T>());
193 template <
typename T>
194 CAFFE2_API
static CaffeTypeId Id();
199 template <
typename T>
209 template <
typename T>
211 auto it = gTypeNames().find(Id<T>());
212 assert(it != gTypeNames().end());
213 return it->second.c_str();
219 template <
typename T>
220 static void _Ctor(
void* ptr,
size_t n) {
221 T* typed_ptr =
static_cast<T*
>(ptr);
222 for (
int i = 0; i < n; ++i) {
223 new (typed_ptr + i) T;
230 template <
typename T>
231 static void _Copy(
const void* src,
void* dst,
size_t n) {
232 const T* typed_src =
static_cast<const T*
>(src);
233 T* typed_dst =
static_cast<T*
>(dst);
234 for (
int i = 0; i < n; ++i) {
235 typed_dst[i] = typed_src[i];
242 template <
typename T>
245 std::cerr <<
"Type " << DemangleType<T>() <<
" does not allow assignment.";
253 template <
typename T>
254 static void _Dtor(
void* ptr,
size_t n) {
255 T* typed_ptr =
static_cast<T*
>(ptr);
256 for (
int i = 0; i < n; ++i) {
264 template <
typename T>
265 static typename std::enable_if<
266 std::is_fundamental<T>::value || std::is_pointer<T>::value,
269 return TypeMeta(Id<T>(), ItemSize<T>(),
nullptr,
nullptr,
nullptr);
274 typename std::enable_if<
275 !(std::is_fundamental<T>::value || std::is_pointer<T>::value) &&
276 std::is_copy_assignable<T>::value>::type* =
nullptr>
278 return TypeMeta(Id<T>(), ItemSize<T>(), _Ctor<T>, _Copy<T>, _Dtor<T>);
281 template <
typename T>
283 typename std::enable_if<
284 !(std::is_fundamental<T>::value || std::is_pointer<T>::value) &&
285 !std::is_copy_assignable<T>::value>::type* = 0) {
287 Id<T>(), ItemSize<T>(), _Ctor<T>, _CopyNotAllowed<T>, _Dtor<T>);
295 TypedDestructor dtor_;
319 #define CAFFE_KNOWN_TYPE(T) \ 321 CAFFE2_EXPORT CaffeTypeId TypeMeta::Id<T>() { \ 322 static bool type_id_bit[1]; \ 323 static TypeNameRegisterer<T> registerer( \ 324 reinterpret_cast<CaffeTypeId>(type_id_bit), #T); \ 325 return reinterpret_cast<CaffeTypeId>(type_id_bit); \ 328 #define CAFFE_KNOWN_TYPE(T) \ 330 CaffeTypeId TypeMeta::Id<T>() { \ 331 static bool type_id_bit[1]; \ 332 static TypeNameRegisterer<T> registerer( \ 333 reinterpret_cast<CaffeTypeId>(type_id_bit), #T); \ 334 return reinterpret_cast<CaffeTypeId>(type_id_bit); \ 340 #endif // CAFFE2_CORE_TYPEID_H_
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...