OR-Tools  9.6
gscip_solver_message_callback_handler.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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_GSCIP_SOLVER_MESSAGE_CALLBACK_HANDLER_H_
15 #define OR_TOOLS_MATH_OPT_SOLVERS_GSCIP_SOLVER_MESSAGE_CALLBACK_HANDLER_H_
16 
17 #include "absl/base/thread_annotations.h"
18 #include "absl/strings/string_view.h"
19 #include "absl/synchronization/mutex.h"
20 #include "ortools/gscip/gscip.h"
23 
24 namespace operations_research {
25 namespace math_opt {
26 
27 // Handler for message callbacks.
28 //
29 // The message callback is called on calls to MessageHandler() and when this
30 // object is destroyed (i.e. when we flush the message callback data). Doing so
31 // in the destructor ensures that even in case of solver failure we do call the
32 // message callback with the last pending messages before returning the error.
33 //
34 // Usage:
35 //
36 // std:unique_ptr<GScipSolverMessageCallbackHandler> message_callback_handler;
37 // if (message_callback != nullptr) {
38 // message_callback_handler =
39 // std::make_unique<GScipSolverMessageCallbackHandler>(message_callback);
40 // }
41 //
42 // GScip* gscip = ...;
43 // RETURN_IF_ERROR(
44 // gscip->Solve(...,
45 // message_callback_handler != nullptr
46 // ? message_callback_handler.MessageHandler()
47 // : nullptr);
48 //
49 // // Flush the last unset message as soon as the solve is done. GScip won't
50 // // call the MessageHandler() after the end of the solve so there is no need
51 // // to wait here.
52 // message_callback_handler.reset();
53 //
54 // ...
56  public:
57  // The input callback must not be null.
59  SolverInterface::MessageCallback message_callback);
60 
61  // Calls the message callback with the last unfinished line if it exists.
63 
65  delete;
67  const GScipSolverMessageCallbackHandler&) = delete;
68 
69  // Returns the handler to pass to GScip::Solve().
71 
72  private:
73  // Updates message_callback_data_ and makes the call to the message callback
74  // if necessary. This method has the expected signature for a
75  // GScipMessageHandler.
76  void MessageCallback(GScipMessageType, absl::string_view message);
77 
78  // Mutex serializing access to message_callback_data_ and the serialization of
79  // calls to the message callback.
80  absl::Mutex message_mutex_;
81 
82  // The message callback; never nullptr. The message_mutex_ should be held
83  // while calling it to ensure proper ordering of the messages.
84  const SolverInterface::MessageCallback message_callback_
85  ABSL_GUARDED_BY(message_mutex_);
86 
87  // The buffer used to generate the message events.
88  MessageCallbackData message_callback_data_ ABSL_GUARDED_BY(message_mutex_);
89 };
90 
91 } // namespace math_opt
92 } // namespace operations_research
93 
94 #endif // OR_TOOLS_MATH_OPT_SOLVERS_GSCIP_SOLVER_MESSAGE_CALLBACK_HANDLER_H_
GScipSolverMessageCallbackHandler & operator=(const GScipSolverMessageCallbackHandler &)=delete
GScipSolverMessageCallbackHandler(const GScipSolverMessageCallbackHandler &)=delete
GScipSolverMessageCallbackHandler(SolverInterface::MessageCallback message_callback)
std::function< void(const std::vector< std::string > &)> MessageCallback
std::function< void(const std::vector< std::string > &)> MessageCallback
Collection of objects used to extend the Constraint Solver library.
std::function< void(GScipMessageType type, absl::string_view message)> GScipMessageHandler
std::string message
Definition: trace.cc:399