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:status",
|
||||
],
|
||||
alwayslink = True, # Defines TestServiceCalculator
|
||||
)
|
||||
|
||||
cc_library(
|
||||
|
|
|
@ -44,7 +44,6 @@ class GraphServiceBase {
|
|||
|
||||
constexpr GraphServiceBase(const char* key) : key(key) {}
|
||||
|
||||
virtual ~GraphServiceBase() = default;
|
||||
inline virtual absl::StatusOr<Packet> CreateDefaultObject() const {
|
||||
return DefaultInitializationUnsupported();
|
||||
}
|
||||
|
@ -52,14 +51,32 @@ class GraphServiceBase {
|
|||
const char* key;
|
||||
|
||||
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 {
|
||||
return absl::UnimplementedError(absl::StrCat(
|
||||
"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>
|
||||
class GraphService : public GraphServiceBase {
|
||||
class GraphService final : public GraphServiceBase {
|
||||
public:
|
||||
using type = T;
|
||||
using packet_type = std::shared_ptr<T>;
|
||||
|
@ -68,7 +85,7 @@ class GraphService : public GraphServiceBase {
|
|||
kDisallowDefaultInitialization)
|
||||
: GraphServiceBase(my_key), default_init_(default_init) {}
|
||||
|
||||
absl::StatusOr<Packet> CreateDefaultObject() const override {
|
||||
absl::StatusOr<Packet> CreateDefaultObject() const final {
|
||||
if (default_init_ != kAllowDefaultInitialization) {
|
||||
return DefaultInitializationUnsupported();
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
namespace mediapipe {
|
||||
namespace {
|
||||
const GraphService<int> kIntService("mediapipe::IntService");
|
||||
constexpr GraphService<int> kIntService("mediapipe::IntService");
|
||||
} // namespace
|
||||
|
||||
TEST(GraphServiceManager, SetGetServiceObject) {
|
||||
|
|
|
@ -14,6 +14,8 @@
|
|||
|
||||
#include "mediapipe/framework/graph_service.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "mediapipe/framework/calculator_contract.h"
|
||||
#include "mediapipe/framework/calculator_framework.h"
|
||||
#include "mediapipe/framework/port/canonical_errors.h"
|
||||
|
@ -159,7 +161,7 @@ TEST_F(GraphServiceTest, CreateDefault) {
|
|||
|
||||
struct TestServiceData {};
|
||||
|
||||
const GraphService<TestServiceData> kTestServiceAllowDefaultInitialization(
|
||||
constexpr GraphService<TestServiceData> kTestServiceAllowDefaultInitialization(
|
||||
"kTestServiceAllowDefaultInitialization",
|
||||
GraphServiceBase::kAllowDefaultInitialization);
|
||||
|
||||
|
@ -272,9 +274,13 @@ TEST(AllowDefaultInitializationGraphServiceTest,
|
|||
HasSubstr("Service is unavailable.")));
|
||||
}
|
||||
|
||||
const GraphService<TestServiceData> kTestServiceDisallowDefaultInitialization(
|
||||
"kTestServiceDisallowDefaultInitialization",
|
||||
GraphServiceBase::kDisallowDefaultInitialization);
|
||||
constexpr GraphService<TestServiceData>
|
||||
kTestServiceDisallowDefaultInitialization(
|
||||
"kTestServiceDisallowDefaultInitialization",
|
||||
GraphServiceBase::kDisallowDefaultInitialization);
|
||||
|
||||
static_assert(std::is_trivially_destructible_v<GraphService<TestServiceData>>,
|
||||
"GraphService is not trivially destructible");
|
||||
|
||||
class FailOnUnavailableOptionalDisallowDefaultInitServiceCalculator
|
||||
: public CalculatorBase {
|
||||
|
|
|
@ -16,15 +16,6 @@
|
|||
|
||||
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) {
|
||||
cc->Inputs().Index(0).Set<int>();
|
||||
cc->Outputs().Index(0).SetSameAs(&cc->Inputs().Index(0));
|
||||
|
|
|
@ -22,14 +22,17 @@ namespace mediapipe {
|
|||
|
||||
using TestServiceObject = std::map<std::string, int>;
|
||||
|
||||
extern const GraphService<TestServiceObject> kTestService;
|
||||
extern const GraphService<int> kAnotherService;
|
||||
inline constexpr GraphService<TestServiceObject> kTestService(
|
||||
"test_service", GraphServiceBase::kDisallowDefaultInitialization);
|
||||
inline constexpr GraphService<int> kAnotherService(
|
||||
"another_service", GraphServiceBase::kAllowDefaultInitialization);
|
||||
|
||||
class NoDefaultConstructor {
|
||||
public:
|
||||
NoDefaultConstructor() = delete;
|
||||
};
|
||||
extern const GraphService<NoDefaultConstructor> kNoDefaultService;
|
||||
inline constexpr GraphService<NoDefaultConstructor> kNoDefaultService(
|
||||
"no_default_service", GraphServiceBase::kAllowDefaultInitialization);
|
||||
|
||||
class NeedsCreateMethod {
|
||||
public:
|
||||
|
@ -40,7 +43,8 @@ class NeedsCreateMethod {
|
|||
private:
|
||||
NeedsCreateMethod() = default;
|
||||
};
|
||||
extern const GraphService<NeedsCreateMethod> kNeedsCreateService;
|
||||
inline constexpr GraphService<NeedsCreateMethod> kNeedsCreateService(
|
||||
"needs_create_service", GraphServiceBase::kAllowDefaultInitialization);
|
||||
|
||||
// Use a service.
|
||||
class TestServiceCalculator : public CalculatorBase {
|
||||
|
|
Loading…
Reference in New Issue
Block a user