Internal change
PiperOrigin-RevId: 532934867
This commit is contained in:
parent
1fb98f5ebd
commit
02230f65d1
|
@ -1099,6 +1099,7 @@ cc_library(
|
||||||
"//mediapipe/framework/port:ret_check",
|
"//mediapipe/framework/port:ret_check",
|
||||||
"//mediapipe/framework/port:status",
|
"//mediapipe/framework/port:status",
|
||||||
],
|
],
|
||||||
|
alwayslink = True, # Defines TestServiceCalculator
|
||||||
)
|
)
|
||||||
|
|
||||||
cc_library(
|
cc_library(
|
||||||
|
|
|
@ -44,7 +44,6 @@ class GraphServiceBase {
|
||||||
|
|
||||||
constexpr GraphServiceBase(const char* key) : key(key) {}
|
constexpr GraphServiceBase(const char* key) : key(key) {}
|
||||||
|
|
||||||
virtual ~GraphServiceBase() = default;
|
|
||||||
inline virtual absl::StatusOr<Packet> CreateDefaultObject() const {
|
inline virtual absl::StatusOr<Packet> CreateDefaultObject() const {
|
||||||
return DefaultInitializationUnsupported();
|
return DefaultInitializationUnsupported();
|
||||||
}
|
}
|
||||||
|
@ -52,14 +51,32 @@ class GraphServiceBase {
|
||||||
const char* key;
|
const char* key;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
// `GraphService<T>` objects, deriving `GraphServiceBase` are designed to be
|
||||||
|
// global constants and not ever deleted through `GraphServiceBase`. Hence,
|
||||||
|
// protected and non-virtual destructor which helps to make `GraphService<T>`
|
||||||
|
// trivially destructible and properly defined as global constants.
|
||||||
|
//
|
||||||
|
// A class with any virtual functions should have a destructor that is either
|
||||||
|
// public and virtual or else protected and non-virtual.
|
||||||
|
// https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-dtor-virtual
|
||||||
|
~GraphServiceBase() = default;
|
||||||
|
|
||||||
absl::Status DefaultInitializationUnsupported() const {
|
absl::Status DefaultInitializationUnsupported() const {
|
||||||
return absl::UnimplementedError(absl::StrCat(
|
return absl::UnimplementedError(absl::StrCat(
|
||||||
"Graph service '", key, "' does not support default initialization"));
|
"Graph service '", key, "' does not support default initialization"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// A global constant to refer a service:
|
||||||
|
// - Requesting `CalculatorContract::UseService` from calculator
|
||||||
|
// - Accessing `Calculator/SubgraphContext::Service`from calculator/subgraph
|
||||||
|
// - Setting before graph initialization `CalculatorGraph::SetServiceObject`
|
||||||
|
//
|
||||||
|
// NOTE: In headers, define your graph service reference safely as following:
|
||||||
|
// `inline constexpr GraphService<YourService> kYourService("YourService");`
|
||||||
|
//
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class GraphService : public GraphServiceBase {
|
class GraphService final : public GraphServiceBase {
|
||||||
public:
|
public:
|
||||||
using type = T;
|
using type = T;
|
||||||
using packet_type = std::shared_ptr<T>;
|
using packet_type = std::shared_ptr<T>;
|
||||||
|
@ -68,7 +85,7 @@ class GraphService : public GraphServiceBase {
|
||||||
kDisallowDefaultInitialization)
|
kDisallowDefaultInitialization)
|
||||||
: GraphServiceBase(my_key), default_init_(default_init) {}
|
: GraphServiceBase(my_key), default_init_(default_init) {}
|
||||||
|
|
||||||
absl::StatusOr<Packet> CreateDefaultObject() const override {
|
absl::StatusOr<Packet> CreateDefaultObject() const final {
|
||||||
if (default_init_ != kAllowDefaultInitialization) {
|
if (default_init_ != kAllowDefaultInitialization) {
|
||||||
return DefaultInitializationUnsupported();
|
return DefaultInitializationUnsupported();
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
namespace mediapipe {
|
namespace mediapipe {
|
||||||
namespace {
|
namespace {
|
||||||
const GraphService<int> kIntService("mediapipe::IntService");
|
constexpr GraphService<int> kIntService("mediapipe::IntService");
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
TEST(GraphServiceManager, SetGetServiceObject) {
|
TEST(GraphServiceManager, SetGetServiceObject) {
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
|
|
||||||
#include "mediapipe/framework/graph_service.h"
|
#include "mediapipe/framework/graph_service.h"
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
#include "mediapipe/framework/calculator_contract.h"
|
#include "mediapipe/framework/calculator_contract.h"
|
||||||
#include "mediapipe/framework/calculator_framework.h"
|
#include "mediapipe/framework/calculator_framework.h"
|
||||||
#include "mediapipe/framework/port/canonical_errors.h"
|
#include "mediapipe/framework/port/canonical_errors.h"
|
||||||
|
@ -159,7 +161,7 @@ TEST_F(GraphServiceTest, CreateDefault) {
|
||||||
|
|
||||||
struct TestServiceData {};
|
struct TestServiceData {};
|
||||||
|
|
||||||
const GraphService<TestServiceData> kTestServiceAllowDefaultInitialization(
|
constexpr GraphService<TestServiceData> kTestServiceAllowDefaultInitialization(
|
||||||
"kTestServiceAllowDefaultInitialization",
|
"kTestServiceAllowDefaultInitialization",
|
||||||
GraphServiceBase::kAllowDefaultInitialization);
|
GraphServiceBase::kAllowDefaultInitialization);
|
||||||
|
|
||||||
|
@ -272,9 +274,13 @@ TEST(AllowDefaultInitializationGraphServiceTest,
|
||||||
HasSubstr("Service is unavailable.")));
|
HasSubstr("Service is unavailable.")));
|
||||||
}
|
}
|
||||||
|
|
||||||
const GraphService<TestServiceData> kTestServiceDisallowDefaultInitialization(
|
constexpr GraphService<TestServiceData>
|
||||||
"kTestServiceDisallowDefaultInitialization",
|
kTestServiceDisallowDefaultInitialization(
|
||||||
GraphServiceBase::kDisallowDefaultInitialization);
|
"kTestServiceDisallowDefaultInitialization",
|
||||||
|
GraphServiceBase::kDisallowDefaultInitialization);
|
||||||
|
|
||||||
|
static_assert(std::is_trivially_destructible_v<GraphService<TestServiceData>>,
|
||||||
|
"GraphService is not trivially destructible");
|
||||||
|
|
||||||
class FailOnUnavailableOptionalDisallowDefaultInitServiceCalculator
|
class FailOnUnavailableOptionalDisallowDefaultInitServiceCalculator
|
||||||
: public CalculatorBase {
|
: public CalculatorBase {
|
||||||
|
|
|
@ -16,15 +16,6 @@
|
||||||
|
|
||||||
namespace mediapipe {
|
namespace mediapipe {
|
||||||
|
|
||||||
const GraphService<TestServiceObject> kTestService(
|
|
||||||
"test_service", GraphServiceBase::kDisallowDefaultInitialization);
|
|
||||||
const GraphService<int> kAnotherService(
|
|
||||||
"another_service", GraphServiceBase::kAllowDefaultInitialization);
|
|
||||||
const GraphService<NoDefaultConstructor> kNoDefaultService(
|
|
||||||
"no_default_service", GraphServiceBase::kAllowDefaultInitialization);
|
|
||||||
const GraphService<NeedsCreateMethod> kNeedsCreateService(
|
|
||||||
"needs_create_service", GraphServiceBase::kAllowDefaultInitialization);
|
|
||||||
|
|
||||||
absl::Status TestServiceCalculator::GetContract(CalculatorContract* cc) {
|
absl::Status TestServiceCalculator::GetContract(CalculatorContract* cc) {
|
||||||
cc->Inputs().Index(0).Set<int>();
|
cc->Inputs().Index(0).Set<int>();
|
||||||
cc->Outputs().Index(0).SetSameAs(&cc->Inputs().Index(0));
|
cc->Outputs().Index(0).SetSameAs(&cc->Inputs().Index(0));
|
||||||
|
|
|
@ -22,14 +22,17 @@ namespace mediapipe {
|
||||||
|
|
||||||
using TestServiceObject = std::map<std::string, int>;
|
using TestServiceObject = std::map<std::string, int>;
|
||||||
|
|
||||||
extern const GraphService<TestServiceObject> kTestService;
|
inline constexpr GraphService<TestServiceObject> kTestService(
|
||||||
extern const GraphService<int> kAnotherService;
|
"test_service", GraphServiceBase::kDisallowDefaultInitialization);
|
||||||
|
inline constexpr GraphService<int> kAnotherService(
|
||||||
|
"another_service", GraphServiceBase::kAllowDefaultInitialization);
|
||||||
|
|
||||||
class NoDefaultConstructor {
|
class NoDefaultConstructor {
|
||||||
public:
|
public:
|
||||||
NoDefaultConstructor() = delete;
|
NoDefaultConstructor() = delete;
|
||||||
};
|
};
|
||||||
extern const GraphService<NoDefaultConstructor> kNoDefaultService;
|
inline constexpr GraphService<NoDefaultConstructor> kNoDefaultService(
|
||||||
|
"no_default_service", GraphServiceBase::kAllowDefaultInitialization);
|
||||||
|
|
||||||
class NeedsCreateMethod {
|
class NeedsCreateMethod {
|
||||||
public:
|
public:
|
||||||
|
@ -40,7 +43,8 @@ class NeedsCreateMethod {
|
||||||
private:
|
private:
|
||||||
NeedsCreateMethod() = default;
|
NeedsCreateMethod() = default;
|
||||||
};
|
};
|
||||||
extern const GraphService<NeedsCreateMethod> kNeedsCreateService;
|
inline constexpr GraphService<NeedsCreateMethod> kNeedsCreateService(
|
||||||
|
"needs_create_service", GraphServiceBase::kAllowDefaultInitialization);
|
||||||
|
|
||||||
// Use a service.
|
// Use a service.
|
||||||
class TestServiceCalculator : public CalculatorBase {
|
class TestServiceCalculator : public CalculatorBase {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user