Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
model.h
Go to the documentation of this file.
1// Copyright 2010-2025 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_SAT_MODEL_H_
15#define OR_TOOLS_SAT_MODEL_H_
16
17#include <cstddef>
18#include <cstdio>
19#include <ctime>
20#include <functional>
21#include <memory>
22#include <string>
23#include <vector>
24
25#include "absl/container/flat_hash_map.h"
26#include "absl/log/check.h"
27#include "absl/meta/type_traits.h"
29#include "ortools/base/typeid.h"
30
31namespace operations_research {
32namespace sat {
33
40class Model {
41 public:
42 Model() = default;
43
45 // The order of deletion seems to be platform dependent.
46 // We force a reverse order on the cleanup vector.
47 for (int i = cleanup_list_.size() - 1; i >= 0; --i) {
48 cleanup_list_[i].reset();
49 }
50 }
51
56 explicit Model(std::string name) : name_(name) {}
57
58 // This type is neither copyable nor movable.
59 Model(const Model&) = delete;
60 Model& operator=(const Model&) = delete;
61
86 template <typename T>
87 T Add(std::function<T(Model*)> f) {
88 return f(this);
89 }
90
92 template <typename T>
93 T Get(std::function<T(const Model&)> f) const {
94 return f(*this);
95 }
96
111 template <typename T>
113 const size_t type_id = gtl::FastTypeId<T>();
114 auto find = singletons_.find(type_id);
115 if (find != singletons_.end()) {
116 return static_cast<T*>(find->second);
117 }
118
119 // New element.
120 // TODO(user): directly store std::unique_ptr<> in singletons_?
121 T* new_t = MyNew<T>(0);
122 singletons_[type_id] = new_t;
123 TakeOwnership(new_t);
124 return new_t;
125 }
126
132 template <typename T>
133 const T* Get() const {
134 const auto& it = singletons_.find(gtl::FastTypeId<T>());
135 return it != singletons_.end() ? static_cast<const T*>(it->second)
136 : nullptr;
137 }
138
142 template <typename T>
143 T* Mutable() const {
144 const auto& it = singletons_.find(gtl::FastTypeId<T>());
145 return it != singletons_.end() ? static_cast<T*>(it->second) : nullptr;
146 }
147
153 template <typename T>
154 T* TakeOwnership(T* t) {
155 cleanup_list_.emplace_back(new Delete<T>(t));
156 return t;
157 }
158
164 template <typename T>
165 T* Create() {
166 T* new_t = MyNew<T>(0);
167 TakeOwnership(new_t);
168 return new_t;
169 }
170
176 template <typename T>
177 void Register(T* non_owned_class) {
178 const size_t type_id = gtl::FastTypeId<T>();
179 CHECK(!singletons_.contains(type_id));
180 singletons_[type_id] = non_owned_class;
181 }
182
183 const std::string& Name() const { return name_; }
184
185 private:
186 // We want to call the constructor T(model*) if it exists or just T() if
187 // it doesn't. For this we use some template "magic":
188 // - The first MyNew() will only be defined if the type in decltype() exist.
189 // - The second MyNew() will always be defined, but because of the ellipsis
190 // it has lower priority that the first one.
191 template <typename T>
192 decltype(T(static_cast<Model*>(nullptr)))* MyNew(int) {
193 return new T(this);
194 }
195 template <typename T>
196 T* MyNew(...) {
197 return new T();
198 }
199
200 const std::string name_;
201
202 // Map of FastTypeId<T> to a "singleton" of type T.
203 absl::flat_hash_map</*typeid*/ size_t, void*> singletons_;
204
205 struct DeleteInterface {
206 virtual ~DeleteInterface() = default;
207 };
208 template <typename T>
209 class Delete : public DeleteInterface {
210 public:
211 explicit Delete(T* t) : to_delete_(t) {}
212 ~Delete() override = default;
213
214 private:
215 std::unique_ptr<T> to_delete_;
216 };
217
218 // The list of items to delete.
219 //
220 // TODO(user): I don't think we need the two layers of unique_ptr, but we
221 // don't care too much about efficiency here and this was easier to get
222 // working.
223 std::vector<std::unique_ptr<DeleteInterface>> cleanup_list_;
224};
225
226} // namespace sat
227} // namespace operations_research
228
229#endif // OR_TOOLS_SAT_MODEL_H_
T Add(std::function< T(Model *)> f)
Definition model.h:87
T Get(std::function< T(const Model &)> f) const
Similar to Add() but this is const.
Definition model.h:93
Model(std::string name)
Definition model.h:56
const std::string & Name() const
Definition model.h:183
Model(const Model &)=delete
This type is neither copyable nor movable.
void Register(T *non_owned_class)
Definition model.h:177
Model & operator=(const Model &)=delete
const T * Get() const
Definition model.h:133
size_t FastTypeId()
Definition typeid.h:19
In SWIG mode, we don't want anything besides these top-level includes.