OR-Tools  9.2
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.
36 using GScipMessageHandler =
37  std::function<void(GScipMessageType type, absl::string_view message)>;
38 
39 namespace internal {
40 
41 // Functor that releases the input message handler if not nullptr. It is used as
42 // the deleter for the MessageHandlerPtr unique_ptr.
43 //
44 // It is a wrapper around SCIPmessagehdlrRelease.
46  void operator()(SCIP_MESSAGEHDLR* handler) const;
47 };
48 
49 // A unique_ptr that releases a SCIP message handler when destroyed.
50 //
51 // Use CaptureMessageHandlerPtr() to create to capture an existing message
52 // handler and creates this automatic pointer that will released it on
53 // destruction.
54 using MessageHandlerPtr =
55  std::unique_ptr<SCIP_MESSAGEHDLR, ReleaseSCIPMessageHandler>;
56 
57 // Captures the input handler and returns a unique pointer that will release it
58 // when destroyed.
59 MessageHandlerPtr CaptureMessageHandlerPtr(SCIP_MESSAGEHDLR* const handler);
60 
61 // Make a message handler for SCIP that calls the input function.
62 absl::StatusOr<MessageHandlerPtr> MakeSCIPMessageHandler(
63  const GScipMessageHandler gscip_message_handler);
64 
65 // Object to be instantiated on stack that, when destroyed, will disable the
66 // custom handler so that it does not call the GScipMessageHandler.
67 //
68 // It is used so that the GScipMessageHandler is not called after GScip::Solve()
69 // have returned, even if the handler has not been uninstalled and freed
70 // properly (when an error occurs).
72  public:
73  // The input handler must be the result of MakeSCIPMessageHandler(). If
74  // nullptr (initially or after being reset to null), nothing will happen.
75  //
76  // A reference is kept to the input so the caller must make sure this input
77  // MessageHandlerPtr will outlive this object.
78  explicit ScopedSCIPMessageHandlerDisabler(const MessageHandlerPtr& handler);
80 
81  private:
82  const MessageHandlerPtr& handler_;
83 };
84 
85 } // namespace internal
86 } // namespace operations_research
87 
88 #endif // OR_TOOLS_GSCIP_GSCIP_MESSAGE_HANDLER_H_
MessageHandlerPtr CaptureMessageHandlerPtr(SCIP_MESSAGEHDLR *const handler)
absl::StatusOr< MessageHandlerPtr > MakeSCIPMessageHandler(const GScipMessageHandler gscip_message_handler)
std::string message
Definition: trace.cc:398
std::unique_ptr< SCIP_MESSAGEHDLR, ReleaseSCIPMessageHandler > MessageHandlerPtr
Collection of objects used to extend the Constraint Solver library.
std::function< void(GScipMessageType type, absl::string_view message)> GScipMessageHandler