forked from rte-france/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto_tools.h
74 lines (65 loc) · 3.01 KB
/
proto_tools.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2010-2022 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 OR_TOOLS_UTIL_PROTO_TOOLS_H_
#define OR_TOOLS_UTIL_PROTO_TOOLS_H_
#include <string>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_format.h"
#include "google/protobuf/message.h"
namespace operations_research {
// Casts a generic google::protobuf::Message* to a specific proto type, or
// returns an InvalidArgumentError if it doesn't seem to be of the right type.
// Comes in non-const and const versions.
// NOTE(user): You should rather use DynamicCastToGenerated() from message.h
// if you don't need the fancy error message or the absl::Status.
template <class Proto>
absl::StatusOr<Proto*> SafeProtoDownCast(google::protobuf::Message* proto);
template <class Proto>
absl::StatusOr<const Proto*> SafeProtoConstDownCast(
const google::protobuf::Message* proto);
// Prints a proto2 message as a string, it behaves like TextFormat::Print()
// but also prints the default values of unset fields which is useful for
// printing parameters.
std::string FullProtocolMessageAsString(
const google::protobuf::Message& message, int indent_level);
// =============================================================================
// Implementation of function templates.
template <class Proto>
absl::StatusOr<Proto*> SafeProtoDownCast(google::protobuf::Message* proto) {
const google::protobuf::Descriptor* expected_descriptor =
Proto::default_instance().GetDescriptor();
const google::protobuf::Descriptor* actual_descriptor =
proto->GetDescriptor();
if (actual_descriptor == expected_descriptor)
return reinterpret_cast<Proto*>(proto);
return absl::InvalidArgumentError(absl::StrFormat(
"Expected message type '%s', but got type '%s'",
expected_descriptor->full_name(), actual_descriptor->full_name()));
}
template <class Proto>
absl::StatusOr<const Proto*> SafeProtoConstDownCast(
const google::protobuf::Message* proto) {
const google::protobuf::Descriptor* expected_descriptor =
Proto::default_instance().GetDescriptor();
const google::protobuf::Descriptor* actual_descriptor =
proto->GetDescriptor();
if (actual_descriptor == expected_descriptor) {
return reinterpret_cast<const Proto*>(proto);
}
return absl::InvalidArgumentError(absl::StrFormat(
"Expected message type '%s', but got type '%s'",
expected_descriptor->full_name(), actual_descriptor->full_name()));
}
} // namespace operations_research
#endif // OR_TOOLS_UTIL_PROTO_TOOLS_H_