OR-Tools  9.1
message_callback_data.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_MATH_OPT_SOLVERS_MESSAGE_CALLBACK_DATA_H_
15 #define OR_TOOLS_MATH_OPT_SOLVERS_MESSAGE_CALLBACK_DATA_H_
16 
17 #include <string>
18 
19 #include "absl/strings/string_view.h"
20 #include "absl/types/optional.h"
21 #include "ortools/math_opt/callback.pb.h"
22 
23 namespace operations_research {
24 namespace math_opt {
25 
26 // Buffer for solvers messages that enforces the contract of
27 // CallbackDataProto.messages for CALLBACK_EVENT_MESSAGE events.
28 //
29 // This contract mandates that each message is a full finished line. As a
30 // consequence, if the solver calls the callback with a partial last line, this
31 // one must not be passed immediately in CallbackDataProto.messages but kept
32 // until the end of the line is reached (or the solve is done).
33 //
34 // To implement that this class has two methods:
35 //
36 // - Parse() that is to be called for each received message from the solver.
37 //
38 // - Flush() that must be called at the end of the solve to generate the data
39 // corresponding the last message sent by the solver if it was an unfinished -
40 // line.
42  public:
43  MessageCallbackData() = default;
44 
47 
48  // Parses the input message, returning a message callback data proto with all
49  // finished lines. Returns nullopt if the input message did not contained any
50  // '\n'.
51  //
52  // It updates this object with the last unfinished line to use it to complete
53  // the next message.
54  //
55  // Note that CallbackDataProto.runtime field is not set. It must be set by the
56  // caller appropriately.
57  absl::optional<CallbackDataProto> Parse(absl::string_view message);
58 
59  // Returns a message callback data proto with the last unfinished line if it
60  // exists.
61  //
62  // Note that CallbackDataProto.runtime field is not set. It must be set by the
63  // caller appropriately.
64  absl::optional<CallbackDataProto> Flush();
65 
66  private:
67  // The last message line not ending with '\n'.
68  std::string unfinished_line_;
69 };
70 
71 } // namespace math_opt
72 } // namespace operations_research
73 
74 #endif // OR_TOOLS_MATH_OPT_SOLVERS_MESSAGE_CALLBACK_DATA_H_
absl::optional< CallbackDataProto > Parse(absl::string_view message)
std::string message
Definition: trace.cc:398
MessageCallbackData & operator=(const MessageCallbackData &)=delete
Collection of objects used to extend the Constraint Solver library.