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();
24 std::set<string>& gRegisteredTypeNames();
28 string Demangle(
const char* name);
32 string GetExceptionString(
const std::exception& e);
38 string name = Demangle(
typeid(T).name());
39 gTypeNames()[id] = name;
46 if (gRegisteredTypeNames().count(name)) {
47 std::cerr <<
"Type name " << name
48 <<
" registered twice. This should " 49 "not happen. Do you have duplicated CAFFE_KNOWN_TYPE?" 51 throw std::runtime_error(
"TypeNameRegisterer error with type " + name);
53 gRegisteredTypeNames().insert(name);
55 gTypeNames()[id] =
"(RTTI disabled, cannot show name)";
68 typedef void (*PlacementNew)(
void*, size_t);
69 typedef void (*TypedCopy)(
const void*,
void*, size_t);
70 typedef void (*TypedDestructor)(
void*, size_t);
75 : id_(0), itemsize_(0), ctor_(nullptr), copy_(nullptr), dtor_(nullptr) {}
82 itemsize_(src.itemsize_),
93 itemsize_ = src.itemsize_;
108 TypedDestructor dtor)
109 : id_(i), itemsize_(s), ctor_(ctor), copy_(copy), dtor_(dtor) {}
115 inline const CaffeTypeId&
id()
const {
127 inline PlacementNew
ctor()
const {
133 inline TypedCopy
copy()
const {
139 inline TypedDestructor
dtor()
const {
145 inline const char*
name()
const {
146 auto it = gTypeNames().find(id_);
147 assert(it != gTypeNames().end());
148 return it->second.c_str();
150 inline bool operator==(
const TypeMeta& m)
const {
151 return (id_ == m.id_);
153 inline bool operator!=(
const TypeMeta& m)
const {
154 return (id_ != m.id_);
157 template <
typename T>
158 inline bool Match()
const {
159 return (id_ == Id<T>());
171 template <
typename T>
172 CAFFE2_EXPORT
static CaffeTypeId Id();
177 template <
typename T>
187 template <
typename T>
190 static const string name = Demangle(
typeid(T).name());
193 return "(RTTI disabled, cannot show name)";
200 template <
typename T>
201 static void _Ctor(
void* ptr,
size_t n) {
202 T* typed_ptr =
static_cast<T*
>(ptr);
203 for (
int i = 0; i < n; ++i) {
204 new (typed_ptr + i) T;
211 template <
typename T>
212 static void _Copy(
const void* src,
void* dst,
size_t n) {
213 const T* typed_src =
static_cast<const T*
>(src);
214 T* typed_dst =
static_cast<T*
>(dst);
215 for (
int i = 0; i < n; ++i) {
216 typed_dst[i] = typed_src[i];
223 template <
typename T>
225 std::cerr <<
"Type " << Name<T>() <<
" does not allow assignment.";
233 template <
typename T>
234 static void _Dtor(
void* ptr,
size_t n) {
235 T* typed_ptr =
static_cast<T*
>(ptr);
236 for (
int i = 0; i < n; ++i) {
244 template <
typename T>
245 static typename std::enable_if<std::is_fundamental<T>::value,
TypeMeta>::type
247 return TypeMeta(Id<T>(), ItemSize<T>(),
nullptr,
nullptr,
nullptr);
252 typename std::enable_if<
253 !std::is_fundamental<T>::value &&
254 std::is_copy_assignable<T>::value>::type* =
nullptr>
256 return TypeMeta(Id<T>(), ItemSize<T>(), _Ctor<T>, _Copy<T>, _Dtor<T>);
259 template <
typename T>
261 typename std::enable_if<
262 !std::is_fundamental<T>::value &&
263 !std::is_copy_assignable<T>::value>::type* = 0) {
265 Id<T>(), ItemSize<T>(), _Ctor<T>, _CopyNotAllowed<T>, _Dtor<T>);
273 TypedDestructor dtor_;
289 #define CAFFE_KNOWN_TYPE(T) \ 291 CaffeTypeId TypeMeta::Id<T>() { \ 292 static bool type_id_bit[1]; \ 293 static TypeNameRegisterer<T> registerer( \ 294 reinterpret_cast<CaffeTypeId>(type_id_bit)); \ 295 return reinterpret_cast<CaffeTypeId>(type_id_bit); \ 300 #endif // CAFFE2_CORE_TYPEID_H_
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...