Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
proto_tools.h
Go to the documentation of this file.
1// Copyright 2010-2024 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/base/casts.h"
20#include "absl/status/status.h"
21#include "absl/status/statusor.h"
22#include "absl/strings/str_format.h"
23#include "google/protobuf/descriptor.h"
24#include "google/protobuf/message.h"
25
26namespace operations_research {
27
28// Casts a generic google::protobuf::Message* to a specific proto type, or
29// returns an InvalidArgumentError if it doesn't seem to be of the right type.
30// Comes in non-const and const versions.
31// NOTE(user): You should rather use DynamicCastToGenerated() from message.h
32// if you don't need the fancy error message or the absl::Status.
33template <class Proto>
34absl::StatusOr<Proto*> SafeProtoDownCast(google::protobuf::Message* proto);
35template <class Proto>
36absl::StatusOr<const Proto*> SafeProtoConstDownCast(
37 const google::protobuf::Message* proto);
38
39// Prints a proto2 message as a string, it behaves like TextFormat::Print()
40// but also prints the default values of unset fields which is useful for
41// printing parameters.
43 const google::protobuf::Message& message, int indent_level);
44
45// =============================================================================
46// Implementation of function templates.
47
48template <class Proto>
49absl::StatusOr<Proto*> SafeProtoDownCast(google::protobuf::Message* proto) {
50 const google::protobuf::Descriptor* expected_descriptor =
51 Proto::default_instance().GetDescriptor();
52 const google::protobuf::Descriptor* actual_descriptor =
53 proto->GetDescriptor();
54 if (actual_descriptor == expected_descriptor)
55 return reinterpret_cast<Proto*>(proto);
56 return absl::InvalidArgumentError(absl::StrFormat(
57 "Expected message type '%s', but got type '%s'",
58 expected_descriptor->full_name(), actual_descriptor->full_name()));
59}
60
61template <class Proto>
62absl::StatusOr<const Proto*> SafeProtoConstDownCast(
63 const google::protobuf::Message* proto) {
64 const google::protobuf::Descriptor* expected_descriptor =
65 Proto::default_instance().GetDescriptor();
66 const google::protobuf::Descriptor* actual_descriptor =
67 proto->GetDescriptor();
68 if (actual_descriptor == expected_descriptor) {
69 return reinterpret_cast<const Proto*>(proto);
70 }
71 return absl::InvalidArgumentError(absl::StrFormat(
72 "Expected message type '%s', but got type '%s'",
73 expected_descriptor->full_name(), actual_descriptor->full_name()));
74}
75
76} // namespace operations_research
77#endif // OR_TOOLS_UTIL_PROTO_TOOLS_H_
CpModelProto proto
The output proto.
In SWIG mode, we don't want anything besides these top-level includes.
std::string FullProtocolMessageAsString(const google::protobuf::Message &message, int indent_level)
absl::StatusOr< Proto * > SafeProtoDownCast(google::protobuf::Message *proto)
Implementation of function templates.
Definition proto_tools.h:50
absl::StatusOr< const Proto * > SafeProtoConstDownCast(const google::protobuf::Message *proto)
Definition proto_tools.h:63
std::string message
Definition trace.cc:397