Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ortools/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,12 @@ cc_library(
deps = [":base"],
)

cc_library(
name = "string_view_migration",
hdrs = ["string_view_migration.h"],
deps = ["@abseil-cpp//absl/strings"],
)

cc_library(
name = "strong_int",
hdrs = ["strong_int.h"],
Expand Down
34 changes: 34 additions & 0 deletions ortools/base/string_view_migration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2010-2025 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
//
// http://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_ORTOOLS_ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_
#define THIRD_PARTY_ORTOOLS_ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_

#include <string>

#include "absl/strings/string_view.h"

// This file contains helpers for various string_view migration efforts. These
// are not intended to be stable long-term.

namespace google::protobuf {

inline std::string StringCopy(absl::string_view str) {
return std::string(str);
}

inline std::string StringCopy(const std::string& str) { return str; }

} // namespace google::protobuf

#endif // THIRD_PARTY_ORTOOLS_ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_
1 change: 1 addition & 0 deletions ortools/linear_solver/wrappers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"//ortools/base:file",
"//ortools/base:string_view_migration",
"//ortools/linear_solver:linear_solver_cc_proto",
"//ortools/linear_solver:model_exporter",
"//ortools/linear_solver:solve_mp_model",
Expand Down
12 changes: 7 additions & 5 deletions ortools/linear_solver/wrappers/model_builder_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "absl/strings/str_join.h"
#include "ortools/base/helpers.h"
#include "ortools/base/options.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/linear_solver/gurobi_util.h"
#include "ortools/linear_solver/linear_solver.h"
#include "ortools/linear_solver/linear_solver.pb.h"
Expand Down Expand Up @@ -183,7 +184,7 @@ double ModelBuilderHelper::VarObjectiveCoefficient(int var_index) const {
}

std::string ModelBuilderHelper::VarName(int var_index) const {
return std::string(model_.variable(var_index).name());
return google::protobuf::StringCopy(model_.variable(var_index).name());
}

int ModelBuilderHelper::AddLinearConstraint() {
Expand Down Expand Up @@ -259,7 +260,7 @@ double ModelBuilderHelper::ConstraintUpperBound(int ct_index) const {
}

std::string ModelBuilderHelper::ConstraintName(int ct_index) const {
return std::string(model_.constraint(ct_index).name());
return google::protobuf::StringCopy(model_.constraint(ct_index).name());
}

std::vector<int> ModelBuilderHelper::ConstraintVarIndices(int ct_index) const {
Expand Down Expand Up @@ -399,7 +400,8 @@ double ModelBuilderHelper::EnforcedConstraintUpperBound(int ct_index) const {

std::string ModelBuilderHelper::EnforcedConstraintName(int ct_index) const {
DCHECK(IsEnforcedConstraint(ct_index));
return std::string(model_.general_constraint(ct_index).name());
return google::protobuf::StringCopy(
model_.general_constraint(ct_index).name());
}

std::vector<int> ModelBuilderHelper::EnforcedConstraintVarIndices(
Expand Down Expand Up @@ -437,7 +439,7 @@ int ModelBuilderHelper::num_constraints() const {
}

std::string ModelBuilderHelper::name() const {
return std::string(model_.name());
return google::protobuf::StringCopy(model_.name());
}

void ModelBuilderHelper::SetName(const std::string& name) {
Expand Down Expand Up @@ -743,7 +745,7 @@ double ModelSolverHelper::activity(int ct_index) {

std::string ModelSolverHelper::status_string() const {
if (!has_response()) return "";
return std::string(response_.value().status_str());
return google::protobuf::StringCopy(response_.value().status_str());
}

double ModelSolverHelper::wall_time() const {
Expand Down
1 change: 1 addition & 0 deletions ortools/math_opt/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ cc_library(
"//ortools/base:status_macros",
"//ortools/math_opt:model_cc_proto",
"//ortools/math_opt:model_update_cc_proto",
"//ortools/base:string_view_migration",
"@abseil-cpp//absl/algorithm:container",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/log:check",
Expand Down
4 changes: 3 additions & 1 deletion ortools/math_opt/core/model_summary.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "absl/types/span.h"
#include "ortools/base/linked_hash_map.h"
#include "ortools/base/status_macros.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/math_opt/model.pb.h"
#include "ortools/math_opt/model_update.pb.h"

Expand Down Expand Up @@ -256,7 +257,8 @@ absl::Status UpdateBiMapFromMappedData(
}
absl::c_sort(new_ids);
for (const int64_t id : new_ids) {
RETURN_IF_ERROR(bimap.Insert(id, std::string(proto_map.at(id).name())));
RETURN_IF_ERROR(bimap.Insert(
id, google::protobuf::StringCopy(proto_map.at(id).name())));
}
return absl::OkStatus();
}
Expand Down
2 changes: 2 additions & 0 deletions ortools/math_opt/cpp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ cc_library(
":variable_and_expressions",
"//ortools/base:protoutil",
"//ortools/base:status_macros",
"//ortools/base:string_view_migration",
"//ortools/gscip:gscip_cc_proto",
"//ortools/math_opt:result_cc_proto",
"//ortools/math_opt:solution_cc_proto",
Expand Down Expand Up @@ -386,6 +387,7 @@ cc_library(
srcs = ["streamable_solver_init_arguments.cc"],
hdrs = ["streamable_solver_init_arguments.h"],
deps = [
"//ortools/base:string_view_migration",
"//ortools/math_opt:parameters_cc_proto",
"//ortools/math_opt/solvers:gurobi_cc_proto",
"@abseil-cpp//absl/status:statusor",
Expand Down
3 changes: 2 additions & 1 deletion ortools/math_opt/cpp/solve_result.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "absl/types/span.h"
#include "ortools/base/protoutil.h"
#include "ortools/base/status_macros.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/math_opt/core/math_opt_proto_utils.h"
#include "ortools/math_opt/cpp/linear_constraint.h"
#include "ortools/math_opt/cpp/variable_and_expressions.h"
Expand Down Expand Up @@ -373,7 +374,7 @@ absl::StatusOr<Termination> Termination::FromProto(
return absl::InvalidArgumentError("reason must be specified");
}
Termination result(/*is_maximize=*/false, *reason,
std::string(termination_proto.detail()));
google::protobuf::StringCopy(termination_proto.detail()));
result.limit = EnumFromProto(termination_proto.limit());
OR_ASSIGN_OR_RETURN3(
result.problem_status,
Expand Down
8 changes: 5 additions & 3 deletions ortools/math_opt/cpp/streamable_solver_init_arguments.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <optional>

#include "absl/status/statusor.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/math_opt/parameters.pb.h"
#include "ortools/math_opt/solvers/gurobi.pb.h"

Expand All @@ -34,10 +35,11 @@ GurobiInitializerProto::ISVKey GurobiISVKey::Proto() const {
GurobiISVKey GurobiISVKey::FromProto(
const GurobiInitializerProto::ISVKey& key_proto) {
return GurobiISVKey{
.name = std::string(key_proto.name()),
.application_name = std::string(key_proto.application_name()),
.name = google::protobuf::StringCopy(key_proto.name()),
.application_name =
google::protobuf::StringCopy(key_proto.application_name()),
.expiration = key_proto.expiration(),
.key = std::string(key_proto.key()),
.key = google::protobuf::StringCopy(key_proto.key()),
};
}

Expand Down
1 change: 1 addition & 0 deletions ortools/math_opt/elemental/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ cc_library(
":symmetry",
":thread_safe_id_map",
"//ortools/base:status_macros",
"//ortools/base:string_view_migration",
"//ortools/math_opt:model_cc_proto",
"//ortools/math_opt:model_update_cc_proto",
"//ortools/math_opt:sparse_containers_cc_proto",
Expand Down
5 changes: 3 additions & 2 deletions ortools/math_opt/elemental/elemental_from_proto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "google/protobuf/repeated_ptr_field.h"
#include "ortools/base/status_builder.h"
#include "ortools/base/status_macros.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/math_opt/core/model_summary.h"
#include "ortools/math_opt/elemental/attr_key.h"
#include "ortools/math_opt/elemental/attributes.h"
Expand Down Expand Up @@ -253,8 +254,8 @@ absl::StatusOr<Elemental> ElementalFromModelProtoImpl(const ModelProto& proto) {
return absl::UnimplementedError(
"Elemental does not support sos2 constraints yet");
}
Elemental elemental(std::string(proto.name()),
std::string(proto.objective().name()));
Elemental elemental(google::protobuf::StringCopy(proto.name()),
google::protobuf::StringCopy(proto.objective().name()));
AddVariables(proto.variables(), elemental);
{
const ObjectiveProto& objective = proto.objective();
Expand Down
1 change: 1 addition & 0 deletions ortools/sat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cc_library(
":model",
":sat_parameters_cc_proto",
"//ortools/util:sorted_interval_list",
"//ortools/base:string_view_migration",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/log:check",
"@abseil-cpp//absl/strings",
Expand Down
7 changes: 5 additions & 2 deletions ortools/sat/cp_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_utils.h"
#include "ortools/util/sorted_interval_list.h"
Expand Down Expand Up @@ -125,7 +126,8 @@ IntVar IntVar::WithName(absl::string_view name) {

std::string IntVar::Name() const {
if (builder_ == nullptr) return "null";
return std::string(builder_->Proto().variables(index_).name());
return google::protobuf::StringCopy(
builder_->Proto().variables(index_).name());
}

::operations_research::Domain IntVar::Domain() const {
Expand Down Expand Up @@ -619,7 +621,8 @@ BoolVar IntervalVar::PresenceBoolVar() const {

std::string IntervalVar::Name() const {
if (builder_ == nullptr) return "null";
return std::string(builder_->Proto().constraints(index_).name());
return google::protobuf::StringCopy(
builder_->Proto().constraints(index_).name());
}

std::string IntervalVar::DebugString() const {
Expand Down
1 change: 1 addition & 0 deletions ortools/sat/python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pybind_extension(
":linear_expr",
":linear_expr_doc",
":proto_builder_pybind11",
"//ortools/base:string_view_migration",
"//ortools/sat:cp_model_cc_proto",
"//ortools/sat:cp_model_utils",
"//ortools/sat:sat_parameters_cc_proto",
Expand Down
3 changes: 2 additions & 1 deletion ortools/sat/python/cp_model_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "absl/functional/any_invocable.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/port/proto_utils.h" // IWYU: keep
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_utils.h"
Expand Down Expand Up @@ -131,7 +132,7 @@ class ResponseWrapper {
}

std::string SolutionInfo() const {
return std::string(response_.solution_info());
return google::protobuf::StringCopy(response_.solution_info());
}

std::vector<int> SufficientAssumptionsForInfeasibility() const {
Expand Down
Loading