From 3b305d7b28c8fcafc8189f1ca104183bd797a5cc Mon Sep 17 00:00:00 2001 From: Jonathan Tatum Date: Fri, 10 May 2024 12:17:10 -0700 Subject: [PATCH] Draft: Update ManagedValueFactory after cel::Value rework. - Add additional documentation and lifetime bound attributes - Rename to RuntimeValueManager PiperOrigin-RevId: 632569265 --- common/values/legacy_value_manager.h | 1 - runtime/BUILD | 19 +++++++---- runtime/managed_value_factory.h | 30 +++------------- runtime/runtime.h | 13 ++++--- runtime/runtime_value_manager.h | 51 ++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 36 deletions(-) create mode 100644 runtime/runtime_value_manager.h diff --git a/common/values/legacy_value_manager.h b/common/values/legacy_value_manager.h index b80a42cb0..6c87ccf67 100644 --- a/common/values/legacy_value_manager.h +++ b/common/values/legacy_value_manager.h @@ -23,7 +23,6 @@ #include "common/types/legacy_type_manager.h" #include "common/value.h" #include "common/value_manager.h" -#include "common/values/legacy_type_reflector.h" namespace cel::common_internal { diff --git a/runtime/BUILD b/runtime/BUILD index eab471904..0af058641 100644 --- a/runtime/BUILD +++ b/runtime/BUILD @@ -181,8 +181,7 @@ cc_library( deps = [ ":activation_interface", ":runtime_issue", - "//base:ast", - "//base:data", + "//common:ast", "//common:native_type", "//common:value", "@com_google_absl//absl/functional:any_invocable", @@ -204,13 +203,20 @@ cc_library( ) cc_library( - name = "managed_value_factory", - hdrs = ["managed_value_factory.h"], + name = "runtime_value_manager", + hdrs = ["runtime_value_manager.h"], deps = [ - "//base:data", "//common:memory", - "//common:type", "//common:value", + "@com_google_absl//absl/base:core_headers", + ], +) + +cc_library( + name = "managed_value_factory", + hdrs = ["managed_value_factory.h"], + deps = [ + ":runtime_value_manager", ], ) @@ -252,6 +258,7 @@ cc_test( ":standard_runtime_builder_factory", "//common:memory", "//common:source", + "//common:type", "//common:value", "//common:value_testing", "//extensions:bindings_ext", diff --git a/runtime/managed_value_factory.h b/runtime/managed_value_factory.h index 8017ebbe2..0d5aab22e 100644 --- a/runtime/managed_value_factory.h +++ b/runtime/managed_value_factory.h @@ -15,34 +15,14 @@ #ifndef THIRD_PARTY_CEL_CPP_RUNTIME_MANAGED_VALUE_FACTORY_H_ #define THIRD_PARTY_CEL_CPP_RUNTIME_MANAGED_VALUE_FACTORY_H_ -#include "base/type_provider.h" -#include "common/memory.h" -#include "common/type_factory.h" -#include "common/type_manager.h" -#include "common/value_manager.h" -#include "common/values/legacy_value_manager.h" +#include "runtime/runtime_value_manager.h" // IWYU pragma: export namespace cel { -// A convenience class for managing objects associated with a ValueManager. -class ManagedValueFactory { - public: - // type_provider and memory_manager must outlive the ManagedValueFactory. - ManagedValueFactory(const TypeProvider& type_provider, - MemoryManagerRef memory_manager) - : value_manager_(memory_manager, type_provider) {} - - // Move-only - ManagedValueFactory(const ManagedValueFactory& other) = delete; - ManagedValueFactory& operator=(const ManagedValueFactory& other) = delete; - ManagedValueFactory(ManagedValueFactory&& other) = delete; - ManagedValueFactory& operator=(ManagedValueFactory&& other) = delete; - - ValueManager& get() { return value_manager_; } - - private: - common_internal::LegacyValueManager value_manager_; -}; +// A convenience class for managing objects associated with a value manager. +// +// Must not outlive the runtime or program that provides the TypeReflector. +using ManagedValueFactory = RuntimeValueManager; } // namespace cel diff --git a/runtime/runtime.h b/runtime/runtime.h index 5b1e654aa..25b3f1703 100644 --- a/runtime/runtime.h +++ b/runtime/runtime.h @@ -25,9 +25,9 @@ #include "absl/functional/any_invocable.h" #include "absl/status/status.h" #include "absl/status/statusor.h" -#include "base/ast.h" -#include "base/type_provider.h" +#include "common/ast.h" #include "common/native_type.h" +#include "common/type_reflector.h" #include "common/value.h" #include "common/value_manager.h" #include "runtime/activation_interface.h" @@ -75,7 +75,9 @@ class Program { virtual absl::StatusOr Evaluate(const ActivationInterface& activation, ValueManager& value_factory) const = 0; - virtual const TypeProvider& GetTypeProvider() const = 0; + // Returns the type provider associated with this program. + virtual const TypeReflector& GetTypeProvider() const + ABSL_ATTRIBUTE_LIFETIME_BOUND = 0; }; // Representation for a traceable CEL expression. @@ -142,7 +144,10 @@ class Runtime { CreateTraceableProgram(std::unique_ptr ast, const CreateProgramOptions& options) const = 0; - virtual const TypeProvider& GetTypeProvider() const = 0; + // Returns the type provider associated with this runtime, used for + // inspecting and creating extended types. + virtual const TypeReflector& GetTypeProvider() const + ABSL_ATTRIBUTE_LIFETIME_BOUND = 0; private: friend class runtime_internal::RuntimeFriendAccess; diff --git a/runtime/runtime_value_manager.h b/runtime/runtime_value_manager.h new file mode 100644 index 000000000..1ccf9dc23 --- /dev/null +++ b/runtime/runtime_value_manager.h @@ -0,0 +1,51 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_CEL_CPP_RUNTIME_RUNTIME_VALUE_MANAGER_H_ +#define THIRD_PARTY_CEL_CPP_RUNTIME_RUNTIME_VALUE_MANAGER_H_ + +#include "absl/base/attributes.h" +#include "common/memory.h" +#include "common/type_reflector.h" +#include "common/value_manager.h" +#include "common/values/legacy_value_manager.h" + +namespace cel { + +// A convenience class for managing objects associated with a value manager. +// +// Must not outlive the runtime or program that provides the TypeReflector. +class RuntimeValueManager { + public: + // type_provider and memory_manager must outlive the ManagedValueFactory. + RuntimeValueManager(const TypeReflector& type_provider + ABSL_ATTRIBUTE_LIFETIME_BOUND, + MemoryManagerRef memory_manager) + : value_manager_(memory_manager, type_provider) {} + + // Neither moveable nor copyable. + RuntimeValueManager(const RuntimeValueManager& other) = delete; + RuntimeValueManager& operator=(const RuntimeValueManager& other) = delete; + RuntimeValueManager(RuntimeValueManager&& other) = delete; + RuntimeValueManager& operator=(RuntimeValueManager&& other) = delete; + + ValueManager& get() { return value_manager_; } + + private: + common_internal::LegacyValueManager value_manager_; +}; + +} // namespace cel + +#endif // THIRD_PARTY_CEL_CPP_RUNTIME_RUNTIME_VALUE_MANAGER_H_