Internal change

PiperOrigin-RevId: 522524565
This commit is contained in:
MediaPipe Team 2023-04-06 22:11:16 -07:00 committed by Copybara-Service
parent 4e5f20f212
commit bae14a83b2
4 changed files with 27 additions and 3 deletions

View File

@ -160,6 +160,7 @@ cc_test(
deps = [
":port",
"//mediapipe/framework/port:gtest_main",
"@com_google_absl//absl/strings",
],
)

View File

@ -713,12 +713,12 @@ class Graph {
}
}
std::string TaggedName(const TagIndexLocation& loc, const std::string& name) {
std::string TaggedName(const TagIndexLocation& loc, absl::string_view name) {
if (loc.tag.empty()) {
// ParseTagIndexName does not allow using explicit indices without tags,
// while ParseTagIndex does.
// TODO: decide whether we should just allow it.
return name;
return std::string(name);
} else {
if (loc.count <= 1) {
return absl::StrCat(loc.tag, ":", name);

View File

@ -21,6 +21,7 @@
#include <utility>
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "mediapipe/framework/api2/const_str.h"
#include "mediapipe/framework/api2/packet.h"
#include "mediapipe/framework/calculator_context.h"
@ -36,6 +37,13 @@ namespace api2 {
// directly by node code.
class PortBase {
public:
constexpr PortBase(absl::string_view tag, TypeId type_id, bool optional,
bool multiple)
: tag_(tag.size(), tag.data()),
optional_(optional),
multiple_(multiple),
type_id_(type_id) {}
constexpr PortBase(std::size_t tag_size, const char* tag, TypeId type_id,
bool optional, bool multiple)
: tag_(tag_size, tag),
@ -123,7 +131,7 @@ auto GetCollection(CC* cc, const SideOutputBase& port)
}
template <class Collection>
auto GetOrNull(Collection& collection, const std::string& tag, int index)
auto GetOrNull(Collection& collection, const absl::string_view& tag, int index)
-> decltype(&collection.Get(std::declval<CollectionItemId>())) {
CollectionItemId id = collection.GetId(tag, index);
return id.IsValid() ? &collection.Get(id) : nullptr;
@ -332,6 +340,9 @@ class PortCommon : public Base {
using Multiple = PortCommon<Base, ValueT, IsOptionalV, true>;
using SideFallback = SideFallbackT<Base, ValueT, IsOptionalV, IsMultipleV>;
explicit constexpr PortCommon(absl::string_view tag)
: Base(tag, kTypeId<ValueT>, IsOptionalV, IsMultipleV) {}
template <std::size_t N>
explicit constexpr PortCommon(const char (&tag)[N])
: Base(N, tag, kTypeId<ValueT>, IsOptionalV, IsMultipleV) {}

View File

@ -1,11 +1,15 @@
#include "mediapipe/framework/api2/port.h"
#include "absl/strings/string_view.h"
#include "mediapipe/framework/port/gtest.h"
namespace mediapipe {
namespace api2 {
namespace {
constexpr absl::string_view kInputTag{"INPUT"};
constexpr absl::string_view kOutputTag{"OUTPUT"};
TEST(PortTest, IntInput) {
static constexpr auto port = Input<int>("FOO");
EXPECT_EQ(port.type_id(), kTypeId<int>);
@ -40,6 +44,14 @@ TEST(PortTest, DeletedCopyConstructorInput) {
EXPECT_EQ(std::string(kSideOutputPort.Tag()), "SIDE_OUTPUT");
}
TEST(PortTest, DeletedCopyConstructorStringView) {
static constexpr Input<DeletedCopyType> kInputPort(kInputTag);
EXPECT_EQ(std::string(kInputPort.Tag()), kInputTag);
static constexpr Output<DeletedCopyType> kOutputPort(kOutputTag);
EXPECT_EQ(std::string(kOutputPort.Tag()), kOutputTag);
}
class AbstractBase {
public:
virtual ~AbstractBase() = default;