Skip to content

Commit a24efe8

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Clean up C++ syntax
PiperOrigin-RevId: 852488447
1 parent eaf71b4 commit a24efe8

19 files changed

Lines changed: 80 additions & 49 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ static absl::StatusOr<cel::StringValue> Translate(
264264
absl::Status ConfigureRuntime(cel::RuntimeBuilder& runtime_builder,
265265
const cel::RuntimeOptions& opts) override {
266266
using TranslateFunctionAdapter =
267-
::cel::TernaryFunctionAdapter<absl::StatusOr<StringValue>,
267+
cel::TernaryFunctionAdapter<absl::StatusOr<StringValue>,
268268
const StringValue&, const StringValue&,
269269
const StringValue&>;
270270
auto status = TranslateFunctionAdapter::RegisterMemberOverload(
@@ -343,7 +343,7 @@ If the extension is written in C++, use the `RegisterLazyFunction` function:
343343
absl::Status ConfigureRuntime(cel::RuntimeBuilder& runtime_builder,
344344
const cel::RuntimeOptions& opts) override {
345345
using MyFunctionAdapter =
346-
::cel::UnaryFunctionAdapter<absl::StatusOr<cel::IntValue>,
346+
cel::UnaryFunctionAdapter<absl::StatusOr<cel::IntValue>,
347347
const cel::IntValue&>;
348348
PY_CEL_RETURN_IF_ERROR(
349349
runtime_builder.function_registry().RegisterLazyFunction(

py_cel.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
namespace cel_python {
3838

39-
namespace py = pybind11;
39+
namespace py = ::pybind11;
4040

4141
void PyCel::DefinePythonBindings(pybind11::module& m) {
4242
py::class_<PyCel, std::shared_ptr<PyCel>> cel_class(m, "Cel");
@@ -85,7 +85,7 @@ void PyCel::DefinePythonBindings(pybind11::module& m) {
8585
functions,
8686
std::shared_ptr<PyCelArena> arena = nullptr) {
8787
if (!arena) {
88-
arena = cel_python::NewArena();
88+
arena = NewArena();
8989
}
9090
std::unordered_map<std::string, PyObject*> data_ptrs;
9191
if (data) {
@@ -121,12 +121,12 @@ std::shared_ptr<PyCelActivation> PyCel::NewActivation(
121121
return std::make_shared<PyCelActivation>(env_, data, functions, arena);
122122
}
123123

124-
absl::StatusOr<std::unique_ptr<PyCelExpression>> PyCel::Compile(
125-
const std::string& cel_expr, bool disable_check) {
124+
absl::StatusOr<PyCelExpression> PyCel::Compile(const std::string& cel_expr,
125+
bool disable_check) {
126126
return PyCelExpression::Compile(env_, cel_expr, disable_check);
127127
}
128128

129-
absl::StatusOr<std::unique_ptr<PyCelExpression>> PyCel::Deserialize(
129+
absl::StatusOr<PyCelExpression> PyCel::Deserialize(
130130
const std::string& serialized_expr) {
131131
return PyCelExpression::Deserialize(env_, serialized_expr);
132132
}

py_cel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class PyCel {
5757
std::string container = "");
5858
~PyCel();
5959

60-
absl::StatusOr<std::unique_ptr<PyCelExpression>> Compile(
61-
const std::string& cel_expr, bool disable_check = false);
62-
absl::StatusOr<std::unique_ptr<PyCelExpression>> Deserialize(
60+
absl::StatusOr<PyCelExpression> Compile(const std::string& cel_expr,
61+
bool disable_check = false);
62+
absl::StatusOr<PyCelExpression> Deserialize(
6363
const std::string& serialized_expr);
6464
std::shared_ptr<PyCelActivation> NewActivation(
6565
const std::unordered_map<std::string, PyObject*>& data,

py_cel_activation.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
namespace cel_python {
3737

38-
namespace py = pybind11;
38+
namespace py = ::pybind11;
3939

4040
void PyCelActivation::DefinePythonBindings(py::module& m) {
4141
py::class_<PyCelActivation, std::shared_ptr<PyCelActivation>>(m,

py_cel_arena.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
namespace cel_python {
3131

32-
namespace py = pybind11;
32+
namespace py = ::pybind11;
3333

3434
void PyCelArena::DefinePythonBindings(py::module_& m) {
3535
py::class_<PyCelArena, std::shared_ptr<PyCelArena>>(
@@ -53,7 +53,7 @@ GetArenaMap() {
5353
}
5454

5555
std::shared_ptr<PyCelArena> NewArena() {
56-
std::shared_ptr<PyCelArena> anArena = std::make_shared<PyCelArena>();
56+
std::shared_ptr<PyCelArena> anArena(new PyCelArena());
5757
GetArenaMap()[anArena->GetArena()] = anArena;
5858
return anArena;
5959
}

py_cel_arena.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class PyCelArena {
2929
public:
3030
static void DefinePythonBindings(pybind11::module& m);
3131

32-
PyCelArena();
3332
~PyCelArena();
3433

3534
// Visible for testing
@@ -43,6 +42,8 @@ class PyCelArena {
4342
friend std::shared_ptr<PyCelArena> NewArena();
4443

4544
private:
45+
// Private constructor. Use `NewArena()` to obtain an instance.
46+
PyCelArena();
4647
google::protobuf::Arena arena_;
4748
};
4849

py_cel_env.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ absl::StatusOr<const cel::Compiler*> PyCelEnv::GetCompiler(
102102

103103
absl::StatusOr<const cel::Runtime*> PyCelEnv::GetRuntime(
104104
const std::shared_ptr<PyCelEnv>& env, RuntimeMode runtime_mode) {
105-
if (env->runtimes_.contains(runtime_mode)) {
106-
return env->runtimes_[runtime_mode].get();
105+
if (auto it = env->runtimes_.find(runtime_mode); it != env->runtimes_.end()) {
106+
return it->second.get();
107107
}
108108

109109
cel::RuntimeOptions opts;
@@ -129,9 +129,11 @@ absl::StatusOr<const cel::Runtime*> PyCelEnv::GetRuntime(
129129
extension_handle->GetExtension(env));
130130
PY_CEL_RETURN_IF_ERROR(extension->ConfigureRuntime(builder, opts));
131131
}
132-
PY_CEL_ASSIGN_OR_RETURN(env->runtimes_[runtime_mode],
132+
PY_CEL_ASSIGN_OR_RETURN(std::unique_ptr<cel::Runtime> runtime,
133133
std::move(builder).Build());
134-
return env->runtimes_[runtime_mode].get();
134+
const cel::Runtime* runtime_ptr = runtime.get();
135+
env->runtimes_[runtime_mode] = std::move(runtime);
136+
return runtime_ptr;
135137
}
136138

137139
const PyCelType& PyCelEnv::GetVariableType(const std::string& name) const {
@@ -171,7 +173,7 @@ absl::StatusOr<PyCelExtension*> PyCelExtensionHandle::GetExtension(
171173
try {
172174
pybind11::handle handle = pybind11::handle(py_extension_);
173175
PyCelPythonExtension* py_cel_extension =
174-
handle.cast<cel_python::PyCelPythonExtension*>();
176+
handle.cast<PyCelPythonExtension*>();
175177
status_py_cel_extension = py_cel_extension->SetEnv(env);
176178
return py_cel_extension;
177179
} catch (const pybind11::cast_error& e) {
@@ -183,7 +185,7 @@ absl::StatusOr<PyCelExtension*> PyCelExtensionHandle::GetExtension(
183185
absl::Status status_cc_cel_extension;
184186
try {
185187
pybind11::handle handle = pybind11::handle(py_extension_);
186-
return handle.cast<cel_python::PyCelExtension*>();
188+
return handle.cast<PyCelExtension*>();
187189
} catch (const pybind11::cast_error& e) {
188190
status_cc_cel_extension = absl::InvalidArgumentError(e.what());
189191
}

py_cel_env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "runtime/runtime.h"
3030
#include "py_cel.h"
3131
#include "py_cel_extension.h"
32+
#include "py_cel_type.h"
3233
#include "py_descriptor_database.h"
3334
#include "py_message_factory.h"
3435
#include "google/protobuf/descriptor.h"

py_cel_expression.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@
4949
#include <pybind11/pybind11.h>
5050
#include "pybind11_abseil/status_casters.h"
5151

52-
using ::cel::expr::CheckedExpr;
53-
using ::cel::expr::ParsedExpr;
5452

5553
namespace cel_python {
5654

57-
namespace py = pybind11;
55+
using ::cel::expr::CheckedExpr;
56+
using ::cel::expr::ParsedExpr;
57+
58+
namespace py = ::pybind11;
5859

5960
void PyCelExpression::DefinePythonBindings(py::module& m) {
60-
py::class_<PyCelExpression, std::unique_ptr<PyCelExpression>>(m, "Expression")
61+
py::class_<PyCelExpression>(m, "Expression")
6162
.def("return_type", &PyCelExpression::GetReturnType)
6263
.def("serialize",
6364
[](PyCelExpression& self) {
@@ -68,7 +69,7 @@ void PyCelExpression::DefinePythonBindings(py::module& m) {
6869
.def("eval", &PyCelExpression::Eval, py::arg("activation"));
6970
}
7071

71-
absl::StatusOr<std::unique_ptr<PyCelExpression>> PyCelExpression::Compile(
72+
absl::StatusOr<PyCelExpression> PyCelExpression::Compile(
7273
const std::shared_ptr<PyCelEnv>& env, const std::string& cel_expr,
7374
bool disable_check) {
7475
ABSL_CHECK(PyGILState_Check());
@@ -81,7 +82,7 @@ absl::StatusOr<std::unique_ptr<PyCelExpression>> PyCelExpression::Compile(
8182
CEL_PYTHON_ASSIGN_OR_RETURN(auto ast, compiler->GetParser().Parse(*s));
8283
ParsedExpr parsed_expr;
8384
PY_CEL_RETURN_IF_ERROR(cel::AstToParsedExpr(*ast, &parsed_expr));
84-
return std::make_unique<PyCelExpression>(parsed_expr, env);
85+
return PyCelExpression(parsed_expr, env);
8586
}
8687

8788
CEL_PYTHON_ASSIGN_OR_RETURN(auto validation, compiler->Compile(cel_expr));
@@ -92,7 +93,7 @@ absl::StatusOr<std::unique_ptr<PyCelExpression>> PyCelExpression::Compile(
9293
validation.ReleaseAst());
9394
CheckedExpr checked_expr;
9495
PY_CEL_RETURN_IF_ERROR(cel::AstToCheckedExpr(*ast, &checked_expr));
95-
return std::make_unique<PyCelExpression>(checked_expr, env);
96+
return PyCelExpression(checked_expr, env);
9697
}
9798

9899
PyCelType PyCelExpression::GetReturnType() {
@@ -112,7 +113,7 @@ PyCelType PyCelExpression::GetReturnType() {
112113
return PyCelType::FromTypeProto(it->second);
113114
}
114115

115-
absl::StatusOr<std::unique_ptr<PyCelValue>> PyCelExpression::Eval(
116+
absl::StatusOr<PyCelValue> PyCelExpression::Eval(
116117
const PyCelActivation& activation) {
117118
ABSL_CHECK(PyGILState_Check());
118119
if (cel_program_ == nullptr) {
@@ -138,7 +139,7 @@ absl::StatusOr<std::unique_ptr<PyCelValue>> PyCelExpression::Eval(
138139
cel::Value result,
139140
cel_program_->Evaluate(arena->GetArena(), env->GetMessageFactory(),
140141
*activation.GetActivation()));
141-
return std::make_unique<PyCelValue>(result, arena, std::move(env));
142+
return PyCelValue(result, arena, std::move(env));
142143
}
143144

144145
std::string PyCelExpression::Serialize() const {
@@ -151,7 +152,7 @@ std::string PyCelExpression::Serialize() const {
151152
return any.SerializeAsString();
152153
}
153154

154-
absl::StatusOr<std::unique_ptr<PyCelExpression>> PyCelExpression::Deserialize(
155+
absl::StatusOr<PyCelExpression> PyCelExpression::Deserialize(
155156
const std::shared_ptr<PyCelEnv>& env, const std::string& serialized_expr) {
156157
ABSL_CHECK(PyGILState_Check());
157158
google::protobuf::Any any;
@@ -163,11 +164,11 @@ absl::StatusOr<std::unique_ptr<PyCelExpression>> PyCelExpression::Deserialize(
163164

164165
CheckedExpr checked_expr;
165166
if (any.UnpackTo(&checked_expr)) {
166-
return std::make_unique<PyCelExpression>(checked_expr, env);
167+
return PyCelExpression(checked_expr, env);
167168
}
168169
ParsedExpr parsed_expr;
169170
if (any.UnpackTo(&parsed_expr)) {
170-
return std::make_unique<PyCelExpression>(parsed_expr, env);
171+
return PyCelExpression(parsed_expr, env);
171172
}
172173

173174
return absl::InvalidArgumentError(

py_cel_expression.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class PyCelExpression {
3838
public:
3939
static void DefinePythonBindings(pybind11::module& m);
4040

41+
PyCelExpression(PyCelExpression&& other) = default;
42+
4143
PyCelExpression(const cel::expr::ParsedExpr& parsed_expr,
4244
std::shared_ptr<PyCelEnv> env)
4345
: expr_(std::move(parsed_expr)), env_(std::move(env)) {}
@@ -47,16 +49,15 @@ class PyCelExpression {
4749

4850
PyCelType GetReturnType();
4951

50-
absl::StatusOr<std::unique_ptr<PyCelValue>> Eval(
51-
const PyCelActivation& activation);
52+
absl::StatusOr<PyCelValue> Eval(const PyCelActivation& activation);
5253

5354
std::string Serialize() const;
5455

55-
static absl::StatusOr<std::unique_ptr<PyCelExpression>> Compile(
56+
static absl::StatusOr<PyCelExpression> Compile(
5657
const std::shared_ptr<PyCelEnv>& env, const std::string& cel_expr,
5758
bool disable_check);
5859

59-
static absl::StatusOr<std::unique_ptr<PyCelExpression>> Deserialize(
60+
static absl::StatusOr<PyCelExpression> Deserialize(
6061
const std::shared_ptr<PyCelEnv>& env, const std::string& serialized_expr);
6162

6263
private:

0 commit comments

Comments
 (0)