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"
25
#include "
ortools/base/status_macros.h
"
26
27
namespace
google::protobuf::contrib::parse_proto
{
28
29
template
<
typename
T>
30
absl::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
37
template
<
typename
T>
38
absl::StatusOr<T>
ParseTextProto
(absl::string_view asciipb) {
39
T msg;
40
RETURN_IF_ERROR
(
ParseTextProtoInto
(asciipb, &msg));
41
return
msg;
42
}
43
44
template
<
typename
T>
45
T
ParseTextOrDie
(absl::string_view
input
) {
46
T result;
47
CHECK(google::protobuf::TextFormat::ParseFromString(
input
, &result));
48
return
result;
49
}
50
51
namespace
text_proto_internal
{
52
53
class
ParseProtoHelper
{
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
71
inline
text_proto_internal::ParseProtoHelper
ParseTextProtoOrDie
(
72
absl::string_view
input
) {
73
return
text_proto_internal::ParseProtoHelper
(
input
);
74
}
75
76
}
// namespace google::protobuf::contrib::parse_proto
77
78
#endif
// ORTOOLS_BASE_PARSE_TEXT_PROTO_H_
status_macros.h
RETURN_IF_ERROR
#define RETURN_IF_ERROR(expr)
Definition
status_macros.h:27
google::protobuf::contrib::parse_proto::text_proto_internal::ParseProtoHelper
Definition
parse_text_proto.h:53
google::protobuf::contrib::parse_proto::text_proto_internal::ParseProtoHelper::ParseProtoHelper
ParseProtoHelper(absl::string_view asciipb)
Definition
parse_text_proto.h:55
google::protobuf::contrib::parse_proto::text_proto_internal
Definition
parse_text_proto.h:51
google::protobuf::contrib::parse_proto
Definition
parse_test_proto.h:24
google::protobuf::contrib::parse_proto::ParseTextProtoInto
absl::Status ParseTextProtoInto(absl::string_view input, T *proto)
Definition
parse_text_proto.h:30
google::protobuf::contrib::parse_proto::ParseTextProtoOrDie
text_proto_internal::ParseProtoHelper ParseTextProtoOrDie(absl::string_view input)
Definition
parse_text_proto.h:71
google::protobuf::contrib::parse_proto::ParseTextProto
absl::StatusOr< T > ParseTextProto(absl::string_view asciipb)
Definition
parse_text_proto.h:38
google::protobuf::contrib::parse_proto::ParseTextOrDie
T ParseTextOrDie(absl::string_view input)
Definition
parse_text_proto.h:45
input
static int input(yyscan_t yyscanner)
ortools
base
parse_text_proto.h
Generated by
1.15.0