Coverage Report

Created: 2026-05-27 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/runtime/runtime_builder.h
Line
Count
Source
1
// Copyright 2023 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef THIRD_PARTY_CEL_CPP_RUNTIME_RUNTIME_BUILDER_H_
16
#define THIRD_PARTY_CEL_CPP_RUNTIME_RUNTIME_BUILDER_H_
17
18
#include <memory>
19
#include <utility>
20
21
#include "absl/base/nullability.h"
22
#include "absl/log/absl_check.h"
23
#include "absl/status/statusor.h"
24
#include "runtime/function_registry.h"
25
#include "runtime/runtime.h"
26
#include "runtime/runtime_options.h"
27
#include "runtime/type_registry.h"
28
#include "google/protobuf/descriptor.h"
29
30
namespace cel {
31
32
// Forward declare for friend access to avoid requiring a link dependency on the
33
// standard implementation and some extensions.
34
namespace runtime_internal {
35
class RuntimeFriendAccess;
36
}  // namespace runtime_internal
37
38
class RuntimeBuilder;
39
absl::StatusOr<RuntimeBuilder> CreateRuntimeBuilder(
40
    absl_nonnull std::shared_ptr<const google::protobuf::DescriptorPool>,
41
    const RuntimeOptions&);
42
43
// RuntimeBuilder provides mutable accessors to configure a new runtime.
44
//
45
// Instances of this class are consumed when built.
46
class RuntimeBuilder {
47
 public:
48
  // Move-only
49
  RuntimeBuilder(const RuntimeBuilder&) = delete;
50
  RuntimeBuilder& operator=(const RuntimeBuilder&) = delete;
51
  RuntimeBuilder(RuntimeBuilder&&) = default;
52
  RuntimeBuilder& operator=(RuntimeBuilder&&) = default;
53
54
0
  TypeRegistry& type_registry() {
55
0
    ABSL_DCHECK(runtime_ != nullptr);
56
0
    return *type_registry_;
57
0
  }
58
59
0
  FunctionRegistry& function_registry() {
60
0
    ABSL_DCHECK(runtime_ != nullptr);
61
0
    return *function_registry_;
62
0
  }
63
64
  // Return the built runtime.
65
  //
66
  // The builder is left in an undefined state after this call and cannot be
67
  // reused.
68
0
  absl::StatusOr<std::unique_ptr<Runtime>> Build() && {
69
0
    return std::move(runtime_);
70
0
  }
71
72
 private:
73
  friend class runtime_internal::RuntimeFriendAccess;
74
  friend absl::StatusOr<RuntimeBuilder> CreateRuntimeBuilder(
75
      absl_nonnull std::shared_ptr<const google::protobuf::DescriptorPool>,
76
      const RuntimeOptions&);
77
78
  // Constructor for a new runtime builder.
79
  //
80
  // It's assumed that the type registry and function registry are managed by
81
  // the runtime.
82
  //
83
  // CEL users should use one of the factory functions for a new builder.
84
  // See standard_runtime_builder_factory.h and runtime_builder_factory.h
85
  RuntimeBuilder(TypeRegistry& type_registry,
86
                 FunctionRegistry& function_registry,
87
                 std::unique_ptr<Runtime> runtime)
88
      : type_registry_(&type_registry),
89
        function_registry_(&function_registry),
90
0
        runtime_(std::move(runtime)) {}
91
92
0
  Runtime& runtime() { return *runtime_; }
93
94
  TypeRegistry* type_registry_;
95
  FunctionRegistry* function_registry_;
96
  std::unique_ptr<Runtime> runtime_;
97
};
98
99
}  // namespace cel
100
101
#endif  // THIRD_PARTY_CEL_CPP_RUNTIME_RUNTIME_BUILDER_H_