Files
ortools-clone/ortools/math_opt/solvers/message_callback_data.cc

101 lines
2.9 KiB
C++
Raw Normal View History

2024-01-04 13:43:15 +01:00
// Copyright 2010-2024 Google LLC
2021-04-11 12:05:38 +02:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/math_opt/solvers/message_callback_data.h"
#include <cstddef>
2022-01-12 16:01:42 +01:00
#include <optional>
2021-04-11 12:05:38 +02:00
#include <string>
#include <utility>
2022-01-12 16:01:42 +01:00
#include <vector>
2021-04-11 12:05:38 +02:00
2023-04-03 18:22:22 +02:00
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
2021-04-11 12:05:38 +02:00
namespace operations_research {
namespace math_opt {
2022-01-12 16:01:42 +01:00
std::vector<std::string> MessageCallbackData::Parse(
2023-04-03 18:22:22 +02:00
const absl::string_view message) {
2022-01-12 16:01:42 +01:00
std::vector<std::string> strings;
2021-04-11 12:05:38 +02:00
// Iterate on all complete lines (lines ending with a '\n').
2023-04-03 18:22:22 +02:00
absl::string_view remainder = message;
2021-04-11 12:05:38 +02:00
for (std::size_t end = 0; end = remainder.find('\n'), end != remainder.npos;
remainder = remainder.substr(end + 1)) {
const auto line = remainder.substr(0, end);
if (!unfinished_line_.empty()) {
2022-01-12 16:01:42 +01:00
std::string new_message = std::move(unfinished_line_);
2021-04-11 12:05:38 +02:00
unfinished_line_.clear();
2023-04-03 18:22:22 +02:00
absl::StrAppend(&new_message, line);
2022-01-12 16:01:42 +01:00
strings.push_back(std::move(new_message));
2021-04-11 12:05:38 +02:00
} else {
2022-01-12 16:01:42 +01:00
strings.emplace_back(line);
2021-04-11 12:05:38 +02:00
}
}
2023-04-03 18:22:22 +02:00
// At the end of the loop, the remainder may contain the last unfinished line.
// This could be the first line too if the entire message does not contain
// '\n'.
absl::StrAppend(&unfinished_line_, remainder);
2021-04-11 12:05:38 +02:00
2022-01-12 16:01:42 +01:00
return strings;
2021-04-11 12:05:38 +02:00
}
2022-01-12 16:01:42 +01:00
std::vector<std::string> MessageCallbackData::Flush() {
2021-04-11 12:05:38 +02:00
if (unfinished_line_.empty()) {
2022-01-12 16:01:42 +01:00
return {};
2021-04-11 12:05:38 +02:00
}
2022-01-12 16:01:42 +01:00
std::vector<std::string> strings = {std::move(unfinished_line_)};
2021-04-11 12:05:38 +02:00
unfinished_line_.clear();
2022-01-12 16:01:42 +01:00
return strings;
2021-04-11 12:05:38 +02:00
}
2023-04-03 18:22:22 +02:00
BufferedMessageCallback::BufferedMessageCallback(
SolverInterface::MessageCallback user_message_callback)
: user_message_callback_(std::move(user_message_callback)) {}
void BufferedMessageCallback::OnMessage(absl::string_view message) {
if (!has_user_message_callback()) {
return;
}
std::vector<std::string> messages;
{
absl::MutexLock lock(&mutex_);
messages = message_callback_data_.Parse(message);
}
// Do not hold lock during callback to user code.
if (!messages.empty()) {
user_message_callback_(messages);
}
}
void BufferedMessageCallback::Flush() {
if (!has_user_message_callback()) {
return;
}
std::vector<std::string> messages;
{
absl::MutexLock lock(&mutex_);
messages = message_callback_data_.Flush();
}
// Do not hold lock during callback to user code.
if (!messages.empty()) {
user_message_callback_(messages);
}
}
2021-04-11 12:05:38 +02:00
} // namespace math_opt
} // namespace operations_research