OR-Tools  9.0
gscip_message_handler.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_GSCIP_GSCIP_MESSAGE_HANDLER_H_
15 #define OR_TOOLS_GSCIP_GSCIP_MESSAGE_HANDLER_H_
16 
17 #include <functional>
18 #include <memory>
19 
20 #include "absl/status/statusor.h"
21 #include "absl/strings/string_view.h"
22 #include "scip/type_message.h"
23 
24 namespace operations_research {
25 
26 // Scip message handlers have three methods to log messages. This enum enables
27 // using the same function for all three types of messages.
29 
30 // An optional callback function to redirect the SCIP logging messages.
31 //
32 // The input `message` usually ends with a newline character. This may not be
33 // the case though when the internal buffer of SCIP is full, in the case this
34 // function is called with a partial message. This will also happen when the
35 // last message the solve ends with an unfinished line.
37  std::function<void(GScipMessageType type, absl::string_view message)>;
38 
39 namespace internal {
40 
41 // A unique_ptr that releases a SCIP message handler when destroyed.
42 //
43 // Use CaptureMessageHandlerPtr() to create values of this type, capturing their
44 // input.
46  std::unique_ptr<SCIP_MESSAGEHDLR, void (*)(SCIP_MESSAGEHDLR*)>;
47 
48 // Captures the input handler and returns a unique pointer that will release it
49 // when destroyed.
50 MessageHandlerPtr CaptureMessageHandlerPtr(SCIP_MESSAGEHDLR* const handler);
51 
52 // Make a message handler for SCIP that calls the input function.
53 absl::StatusOr<MessageHandlerPtr> MakeSCIPMessageHandler(
54  const GScipMessageHandler gscip_message_handler);
55 
56 // Object to be instantiated on stack that, when destroyed, will disable the
57 // custom handler so that it does not call the GScipMessageHandler.
58 //
59 // It is used so that the GScipMessageHandler is not called after GScip::Solve()
60 // have returned, even if the handler has not been uninstalled and freed
61 // properly (when an error occurs).
63  public:
64  // The input handler must be the result of MakeSCIPMessageHandler(). If
65  // nullptr (initially or after being reset to null), nothing will happen.
66  //
67  // A reference is kept to the input so the caller must make sure this input
68  // MessageHandlerPtr will outlive this object.
69  explicit ScopedSCIPMessageHandlerDisabler(const MessageHandlerPtr& handler);
71 
72  private:
73  const MessageHandlerPtr& handler_;
74 };
75 
76 } // namespace internal
77 } // namespace operations_research
78 
79 #endif // OR_TOOLS_GSCIP_GSCIP_MESSAGE_HANDLER_H_
absl::StatusOr< MessageHandlerPtr > MakeSCIPMessageHandler(const GScipMessageHandler gscip_message_handler)
MessageHandlerPtr CaptureMessageHandlerPtr(SCIP_MESSAGEHDLR *const handler)
std::unique_ptr< SCIP_MESSAGEHDLR, void(*)(SCIP_MESSAGEHDLR *)> MessageHandlerPtr
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:398