Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
solver_resources.cc
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <ostream>
17#include <string>
18#include <utility>
19
20#include "absl/status/statusor.h"
21#include "absl/strings/str_cat.h"
22#include "absl/strings/string_view.h"
24
26
27std::ostream& operator<<(std::ostream& out, const SolverResources& resources) {
28 out << '{' << AbslUnparseFlag(resources) << '}';
29 return out;
30}
31
34 if (cpu.has_value()) {
35 ret.set_cpu(cpu.value());
36 }
37 if (ram.has_value()) {
38 ret.set_ram(ram.value());
39 }
40 return ret;
41}
42
43absl::StatusOr<SolverResources> SolverResources::FromProto(
44 const SolverResourcesProto& proto) {
46 if (proto.has_cpu()) {
47 ret.cpu = proto.cpu();
48 }
49 if (proto.has_ram()) {
50 ret.ram = proto.ram();
51 }
52 return ret;
53}
54
55bool AbslParseFlag(const absl::string_view text,
56 SolverResources* const solver_resources,
57 std::string* const error) {
59 if (!ProtobufParseTextProtoForFlag(text, &proto, error)) {
60 return false;
61 }
62 absl::StatusOr<SolverResources> resources = SolverResources::FromProto(proto);
63 if (!resources.ok()) {
64 *error = absl::StrCat(
65 "SolverResourcesProto was invalid and could not convert to "
66 "SolverResources: ",
67 resources.status().ToString());
68 return false;
69 }
70 *solver_resources = *std::move(resources);
71 return true;
72}
73
74std::string AbslUnparseFlag(const SolverResources& solver_resources) {
75 return ProtobufTextFormatPrintToStringForFlag(solver_resources.Proto());
76}
77
78} // namespace operations_research::math_opt
bool AbslParseFlag(const absl::string_view text, SolverType *const value, std::string *const error)
std::ostream & operator<<(std::ostream &ostr, const SecondOrderConeConstraint &constraint)
std::string AbslUnparseFlag(const SolverType value)
std::string ProtobufTextFormatPrintToStringForFlag(const google::protobuf::Message &proto)
bool ProtobufParseTextProtoForFlag(absl::string_view text, ProtoType *message_out, std::string *error_out)
static absl::StatusOr< SolverResources > FromProto(const SolverResourcesProto &proto)