Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
parse_text_proto.h
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
14// emulates g3/net/proto2/contrib/parse_proto/parse_text_proto.h
15#ifndef ORTOOLS_BASE_PARSE_TEXT_PROTO_H_
16#define ORTOOLS_BASE_PARSE_TEXT_PROTO_H_
17
18#include <string>
19
20#include "absl/log/check.h"
21#include "absl/status/status.h"
22#include "absl/strings/string_view.h"
23#include "google/protobuf/message.h"
24#include "google/protobuf/text_format.h"
26
28
29template <typename T>
30absl::Status ParseTextProtoInto(absl::string_view input, T* proto) {
31 if (google::protobuf::TextFormat::ParseFromString(input, proto))
32 return absl::OkStatus();
33 return absl::Status(absl::StatusCode::kInvalidArgument,
34 "Could not parse the text proto\n");
35}
36
37template <typename T>
38absl::StatusOr<T> ParseTextProto(absl::string_view asciipb) {
39 T msg;
40 RETURN_IF_ERROR(ParseTextProtoInto(asciipb, &msg));
41 return msg;
42}
43
44template <typename T>
45T ParseTextOrDie(absl::string_view input) {
46 T result;
47 CHECK(google::protobuf::TextFormat::ParseFromString(input, &result));
48 return result;
49}
50
52
54 public:
55 explicit ParseProtoHelper(absl::string_view asciipb) : asciipb_(asciipb) {}
56 template <class T>
57 operator T() { // NOLINT(runtime/explicit)
58 T result;
59 const bool ok = google::protobuf::TextFormat::TextFormat::ParseFromString(
60 asciipb_, &result);
61 CHECK(ok) << "Failed to parse text proto: " << asciipb_;
62 return result;
63 }
64
65 private:
66 const std::string asciipb_;
67};
68
69} // namespace text_proto_internal
70
75
76} // namespace google::protobuf::contrib::parse_proto
77
78#endif // ORTOOLS_BASE_PARSE_TEXT_PROTO_H_
#define RETURN_IF_ERROR(expr)
absl::Status ParseTextProtoInto(absl::string_view input, T *proto)
text_proto_internal::ParseProtoHelper ParseTextProtoOrDie(absl::string_view input)
absl::StatusOr< T > ParseTextProto(absl::string_view asciipb)
T ParseTextOrDie(absl::string_view input)
static int input(yyscan_t yyscanner)