OR-Tools  9.6
update_tracker.cc
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 
15 
16 #include <memory>
17 #include <optional>
18 
19 #include "absl/status/status.h"
20 #include "absl/status/statusor.h"
21 #include "ortools/base/logging.h"
22 #include "ortools/math_opt/model.pb.h"
24 
25 namespace operations_research {
26 namespace math_opt {
27 
29  const std::shared_ptr<ModelStorage> storage = storage_.lock();
30 
31  // If the model has already been destroyed, the update tracker has been
32  // automatically cleaned.
33  if (storage != nullptr) {
34  storage->DeleteUpdateTracker(update_tracker_);
35  }
36 }
37 
38 UpdateTracker::UpdateTracker(const std::shared_ptr<ModelStorage>& storage)
39  : storage_(ABSL_DIE_IF_NULL(storage)),
40  update_tracker_(storage->NewUpdateTracker()) {}
41 
42 absl::StatusOr<std::optional<ModelUpdateProto>>
44  const std::shared_ptr<ModelStorage> storage = storage_.lock();
45  if (storage == nullptr) {
46  return absl::InvalidArgumentError(internal::kModelIsDestroyed);
47  }
48  return storage->ExportModelUpdate(update_tracker_);
49 }
50 
52  const std::shared_ptr<ModelStorage> storage = storage_.lock();
53  if (storage == nullptr) {
54  return absl::InvalidArgumentError(internal::kModelIsDestroyed);
55  }
56  storage->AdvanceCheckpoint(update_tracker_);
57  return absl::OkStatus();
58 }
59 
60 absl::StatusOr<ModelProto> UpdateTracker::ExportModel() const {
61  const std::shared_ptr<ModelStorage> storage = storage_.lock();
62  if (storage == nullptr) {
63  return absl::InvalidArgumentError(internal::kModelIsDestroyed);
64  }
65  return storage->ExportModel();
66 }
67 
68 } // namespace math_opt
69 } // namespace operations_research
absl::StatusOr< ModelProto > ExportModel() const
UpdateTracker(const std::shared_ptr< ModelStorage > &storage)
absl::StatusOr< std::optional< ModelUpdateProto > > ExportModelUpdate()
constexpr absl::string_view kModelIsDestroyed
Collection of objects used to extend the Constraint Solver library.