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