#ifndef MEDIAPIPE_FRAMEWORK_TOOL_OPTIONS_MAP_H_ #define MEDIAPIPE_FRAMEWORK_TOOL_OPTIONS_MAP_H_ #include #include #include #include "mediapipe/framework/calculator.pb.h" #include "mediapipe/framework/port/any_proto.h" #include "mediapipe/framework/port/status.h" #include "mediapipe/framework/tool/type_util.h" namespace mediapipe { namespace tool { // A compile-time detector for the constant |T::ext|. template struct IsExtension { private: template static char test(decltype(&U::ext)); template static int test(...); public: static constexpr bool value = (sizeof(test(0)) == sizeof(char)); }; template ::value, int>::type = 0> void GetExtension(const CalculatorOptions& options, T* result) { if (options.HasExtension(T::ext)) { *result = options.GetExtension(T::ext); } } template ::value, int>::type = 0> void GetExtension(const CalculatorOptions& options, T* result) {} template void GetNodeOptions(const CalculatorGraphConfig::Node& node_config, T* result) { #if defined(MEDIAPIPE_PROTO_LITE) && defined(MEDIAPIPE_PROTO_THIRD_PARTY) // protobuf::Any is unavailable with third_party/protobuf:protobuf-lite. #else for (const mediapipe::protobuf::Any& options : node_config.node_options()) { if (options.Is()) { options.UnpackTo(result); } } #endif } // A map from object type to object. class TypeMap { public: template bool Has() const { return content_.count(TypeId()) > 0; } template T* Get() const { if (!Has()) { content_[TypeId()] = std::make_shared(); } return static_cast(content_[TypeId()].get()); } private: mutable std::map> content_; }; // Extracts the options message of a specified type from a // CalculatorGraphConfig::Node. class OptionsMap { public: OptionsMap& Initialize(const CalculatorGraphConfig::Node& node_config) { node_config_ = &node_config; return *this; } // Returns the options data for a CalculatorGraphConfig::Node, from // either "options" or "node_options" using either GetExtension or UnpackTo. template const T& Get() const { if (options_.Has()) { return *options_.Get(); } T* result = options_.Get(); if (node_config_->has_options()) { GetExtension(node_config_->options(), result); } else { GetNodeOptions(*node_config_, result); } return *result; } const CalculatorGraphConfig::Node* node_config_; TypeMap options_; }; } // namespace tool } // namespace mediapipe #endif // MEDIAPIPE_FRAMEWORK_TOOL_OPTIONS_MAP_H_