OR-Tools  8.2
proto_tools.h
Go to the documentation of this file.
1// Copyright 2010-2018 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#ifndef OR_TOOLS_UTIL_PROTO_TOOLS_H_
15#define OR_TOOLS_UTIL_PROTO_TOOLS_H_
16
17#include <string>
18
19#include "absl/status/status.h"
20#include "absl/status/statusor.h"
21#include "absl/strings/str_format.h"
22#include "google/protobuf/message.h"
23
24namespace operations_research {
25
26// Casts a generic google::protobuf::Message* to a specific proto type, or
27// returns an InvalidArgumentError if it doesn't seem to be of the right type.
28// Comes in non-const and const versions.
29// NOTE(user): You should rather use DynamicCastToGenerated() from message.h
30// if you don't need the fancy error message or the absl::Status.
31template <class Proto>
32absl::StatusOr<Proto*> SafeProtoDownCast(google::protobuf::Message* proto);
33template <class Proto>
34absl::StatusOr<const Proto*> SafeProtoConstDownCast(
35 const google::protobuf::Message* proto);
36
37// Prints a proto2 message as a string, it behaves like TextFormat::Print()
38// but also prints the default values of unset fields which is useful for
39// printing parameters.
41 const google::protobuf::Message& message, int indent_level);
42
43// =============================================================================
44// Implementation of function templates.
45
46template <class Proto>
47absl::StatusOr<Proto*> SafeProtoDownCast(google::protobuf::Message* proto) {
48 const google::protobuf::Descriptor* expected_descriptor =
49 Proto::default_instance().GetDescriptor();
50 const google::protobuf::Descriptor* actual_descriptor =
51 proto->GetDescriptor();
52 if (actual_descriptor == expected_descriptor)
53 return reinterpret_cast<Proto*>(proto);
54 return absl::InvalidArgumentError(absl::StrFormat(
55 "Expected message type '%s', but got type '%s'",
56 expected_descriptor->full_name(), actual_descriptor->full_name()));
57}
58
59template <class Proto>
60absl::StatusOr<const Proto*> SafeProtoConstDownCast(
61 const google::protobuf::Message* proto) {
62 const google::protobuf::Descriptor* expected_descriptor =
63 Proto::default_instance().GetDescriptor();
64 const google::protobuf::Descriptor* actual_descriptor =
65 proto->GetDescriptor();
66 if (actual_descriptor == expected_descriptor) {
67 return reinterpret_cast<const Proto*>(proto);
68 }
69 return absl::InvalidArgumentError(absl::StrFormat(
70 "Expected message type '%s', but got type '%s'",
71 expected_descriptor->full_name(), actual_descriptor->full_name()));
72}
73
74} // namespace operations_research
75#endif // OR_TOOLS_UTIL_PROTO_TOOLS_H_
CpModelProto proto
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
absl::StatusOr< Proto * > SafeProtoDownCast(google::protobuf::Message *proto)
Definition: proto_tools.h:47
absl::StatusOr< const Proto * > SafeProtoConstDownCast(const google::protobuf::Message *proto)
Definition: proto_tools.h:60
std::string FullProtocolMessageAsString(const google::protobuf::Message &message, int indent_level)
Definition: proto_tools.cc:58
std::string message
Definition: trace.cc:395