Project import generated by Copybara.
GitOrigin-RevId: b2062656e5b3d33264e28ed0cbca31c4b93fe1bf
This commit is contained in:
@@ -167,6 +167,7 @@ cc_library(
|
||||
"//mediapipe/framework:packet_generator_cc_proto",
|
||||
"//mediapipe/framework:packet_set",
|
||||
"//mediapipe/framework/port:any_proto",
|
||||
"//mediapipe/framework/tool:type_util",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -371,6 +372,9 @@ cc_library(
|
||||
name = "type_util",
|
||||
hdrs = ["type_util.h"],
|
||||
visibility = ["//mediapipe/framework:mediapipe_internal"],
|
||||
deps = [
|
||||
"//mediapipe/framework:port",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
|
||||
@@ -15,13 +15,12 @@
|
||||
#ifndef MEDIAPIPE_FRAMEWORK_TOOL_OPTIONS_UTIL_H_
|
||||
#define MEDIAPIPE_FRAMEWORK_TOOL_OPTIONS_UTIL_H_
|
||||
|
||||
#include <typeindex>
|
||||
|
||||
#include "mediapipe/framework/calculator.pb.h"
|
||||
#include "mediapipe/framework/packet.h"
|
||||
#include "mediapipe/framework/packet_generator.pb.h"
|
||||
#include "mediapipe/framework/packet_set.h"
|
||||
#include "mediapipe/framework/port/any_proto.h"
|
||||
#include "mediapipe/framework/tool/type_util.h"
|
||||
|
||||
namespace mediapipe {
|
||||
|
||||
@@ -54,18 +53,18 @@ class TypeMap {
|
||||
public:
|
||||
template <class T>
|
||||
bool Has() const {
|
||||
return content_.count(typeid(T)) > 0;
|
||||
return content_.count(TypeId<T>()) > 0;
|
||||
}
|
||||
template <class T>
|
||||
T* Get() const {
|
||||
if (!Has<T>()) {
|
||||
content_[typeid(T)] = std::make_shared<T>();
|
||||
content_[TypeId<T>()] = std::make_shared<T>();
|
||||
}
|
||||
return static_cast<T*>(content_[typeid(T)].get());
|
||||
return static_cast<T*>(content_[TypeId<T>()].get());
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::map<std::type_index, std::shared_ptr<void>> content_;
|
||||
mutable std::map<TypeIndex, std::shared_ptr<void>> content_;
|
||||
};
|
||||
|
||||
template <class T,
|
||||
|
||||
@@ -16,10 +16,76 @@
|
||||
#define MEDIAPIPE_FRAMEWORK_TOOL_TYPE_UTIL_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <typeindex>
|
||||
#include <typeinfo>
|
||||
|
||||
#include "mediapipe/framework/port.h"
|
||||
|
||||
namespace mediapipe {
|
||||
namespace tool {
|
||||
|
||||
#if !MEDIAPIPE_HAS_RTTI
|
||||
// A unique identifier for type T.
|
||||
class TypeInfo {
|
||||
public:
|
||||
size_t hash_code() const { return reinterpret_cast<size_t>(this); }
|
||||
bool operator==(const TypeInfo& other) const { return &other == this; }
|
||||
bool operator<(const TypeInfo& other) const { return &other < this; }
|
||||
const char* name() const { return "<unknown>"; }
|
||||
template <typename T>
|
||||
static const TypeInfo& Get() {
|
||||
static TypeInfo* static_type_info = new TypeInfo;
|
||||
return *static_type_info;
|
||||
}
|
||||
|
||||
private:
|
||||
TypeInfo() {}
|
||||
TypeInfo(const TypeInfo&) = delete;
|
||||
};
|
||||
|
||||
#else // MEDIAPIPE_HAS_RTTI
|
||||
// The std unique identifier for type T.
|
||||
class TypeInfo {
|
||||
public:
|
||||
size_t hash_code() const { return info_.hash_code(); }
|
||||
bool operator==(const TypeInfo& o) const { return info_ == o.info_; }
|
||||
bool operator<(const TypeInfo& o) const { return info_.before(o.info_); }
|
||||
const char* name() const { return info_.name(); }
|
||||
template <typename T>
|
||||
static const TypeInfo& Get() {
|
||||
static TypeInfo* static_type_info = new TypeInfo(typeid(T));
|
||||
return *static_type_info;
|
||||
}
|
||||
|
||||
private:
|
||||
TypeInfo(const std::type_info& info) : info_(info) {}
|
||||
TypeInfo(const TypeInfo&) = delete;
|
||||
|
||||
private:
|
||||
const std::type_info& info_;
|
||||
friend class TypeIndex;
|
||||
};
|
||||
#endif
|
||||
|
||||
// An associative key for TypeInfo.
|
||||
class TypeIndex {
|
||||
public:
|
||||
TypeIndex(const TypeInfo& info) : info_(info) {}
|
||||
size_t hash_code() const { return info_.hash_code(); }
|
||||
bool operator==(const TypeIndex& other) const { return info_ == other.info_; }
|
||||
bool operator<(const TypeIndex& other) const { return info_ < other.info_; }
|
||||
|
||||
private:
|
||||
const TypeInfo& info_;
|
||||
};
|
||||
|
||||
// Returns a unique identifier for type T.
|
||||
template <typename T>
|
||||
const TypeInfo& TypeId() {
|
||||
return TypeInfo::Get<T>();
|
||||
}
|
||||
|
||||
// Helper method that returns a hash code of the given type. This allows for
|
||||
// typeid testing across multiple binaries, unlike FastTypeId which used a
|
||||
// memory location that only works within the same binary. Moreover, we use this
|
||||
@@ -30,7 +96,7 @@ namespace tool {
|
||||
// as much as possible.
|
||||
template <typename T>
|
||||
size_t GetTypeHash() {
|
||||
return typeid(T).hash_code();
|
||||
return TypeId<T>().hash_code();
|
||||
}
|
||||
|
||||
} // namespace tool
|
||||
|
||||
Reference in New Issue
Block a user