Files
ortools-clone/ortools/math_opt/cpp/message_callback.cc

118 lines
4.0 KiB
C++
Raw Normal View History

2022-06-17 08:40:20 +02:00
// Copyright 2010-2022 Google LLC
2022-01-31 18:44:25 +01: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/cpp/message_callback.h"
2023-05-31 14:27:19 +02:00
#include <algorithm>
2022-01-31 18:44:25 +01:00
#include <memory>
#include <ostream>
#include <string>
#include <vector>
#include "absl/base/thread_annotations.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
2023-05-31 14:27:19 +02:00
#include "google/protobuf/repeated_field.h"
#include "google/protobuf/repeated_ptr_field.h"
2022-01-31 18:44:25 +01:00
#include "ortools/base/logging.h"
#include "ortools/base/source_location.h"
namespace operations_research::math_opt {
namespace {
class PrinterMessageCallbackImpl {
public:
PrinterMessageCallbackImpl(std::ostream& output_stream,
const absl::string_view prefix)
: output_stream_(output_stream), prefix_(prefix) {}
void Call(const std::vector<std::string>& messages) {
const absl::MutexLock lock(&mutex_);
for (const std::string& message : messages) {
output_stream_ << prefix_ << message << '\n';
}
output_stream_.flush();
}
private:
absl::Mutex mutex_;
std::ostream& output_stream_ ABSL_GUARDED_BY(mutex_);
const std::string prefix_;
};
2023-05-31 14:27:19 +02:00
void PushBack(const std::vector<std::string>& messages,
std::vector<std::string>* const sink) {
sink->insert(sink->end(), messages.begin(), messages.end());
}
void PushBack(const std::vector<std::string>& messages,
google::protobuf::RepeatedPtrField<std::string>* const sink) {
std::copy(messages.begin(), messages.end(),
google::protobuf::RepeatedFieldBackInserter(sink));
}
template <typename Sink>
class VectorLikeMessageCallbackImpl {
2023-04-03 18:22:22 +02:00
public:
2023-05-31 14:27:19 +02:00
explicit VectorLikeMessageCallbackImpl(Sink* const sink)
2023-04-03 18:22:22 +02:00
: sink_(ABSL_DIE_IF_NULL(sink)) {}
void Call(const std::vector<std::string>& messages) {
const absl::MutexLock lock(&mutex_);
2023-05-31 14:27:19 +02:00
PushBack(messages, sink_);
2023-04-03 18:22:22 +02:00
}
private:
absl::Mutex mutex_;
2023-05-31 14:27:19 +02:00
Sink* const sink_;
2023-04-03 18:22:22 +02:00
};
2022-01-31 18:44:25 +01:00
} // namespace
MessageCallback PrinterMessageCallback(std::ostream& output_stream,
const absl::string_view prefix) {
// Here we must use an std::shared_ptr since std::function requires that its
// input is copyable. And PrinterMessageCallbackImpl can't be copyable since
// it uses an absl::Mutex that is not.
const auto impl =
std::make_shared<PrinterMessageCallbackImpl>(output_stream, prefix);
return
[=](const std::vector<std::string>& messages) { impl->Call(messages); };
}
2023-04-03 18:22:22 +02:00
MessageCallback VectorMessageCallback(std::vector<std::string>* sink) {
CHECK(sink != nullptr);
// Here we must use an std::shared_ptr since std::function requires that its
// input is copyable. And VectorMessageCallbackImpl can't be copyable since it
// uses an absl::Mutex that is not.
2023-05-31 14:27:19 +02:00
const auto impl =
std::make_shared<VectorLikeMessageCallbackImpl<std::vector<std::string>>>(
sink);
return
[=](const std::vector<std::string>& messages) { impl->Call(messages); };
}
MessageCallback RepeatedPtrFieldMessageCallback(
google::protobuf::RepeatedPtrField<std::string>* sink) {
CHECK(sink != nullptr);
// Here we must use an std::shared_ptr since std::function requires that its
// input is copyable. And VectorMessageCallbackImpl can't be copyable since
// it uses an absl::Mutex that is not.
const auto impl = std::make_shared<VectorLikeMessageCallbackImpl<
google::protobuf::RepeatedPtrField<std::string>>>(sink);
2023-04-03 18:22:22 +02:00
return
[=](const std::vector<std::string>& messages) { impl->Call(messages); };
}
2022-01-31 18:44:25 +01:00
} // namespace operations_research::math_opt