Go to the documentation of this file.
14 #ifndef OR_TOOLS_SAT_MODEL_H_
15 #define OR_TOOLS_SAT_MODEL_H_
23 #include "absl/container/flat_hash_map.h"
24 #include "ortools/base/logging.h"
25 #include "ortools/base/macros.h"
26 #include "ortools/base/map_util.h"
27 #include "ortools/base/typeid.h"
45 for (
int i = cleanup_list_.size() - 1; i >= 0; --i) {
46 cleanup_list_[i].reset();
54 explicit Model(std::string name) : name_(name) {}
87 T
Get(std::function<T(
const Model&)> f)
const {
105 template <
typename T>
107 const size_t type_id = gtl::FastTypeId<T>();
108 auto find = singletons_.find(type_id);
109 if (find != singletons_.end()) {
110 return static_cast<T*
>(find->second);
115 T* new_t = MyNew<T>(0);
116 singletons_[type_id] = new_t;
126 template <
typename T>
128 return static_cast<const T*
>(
129 gtl::FindWithDefault(singletons_, gtl::FastTypeId<T>(),
nullptr));
135 template <
typename T>
137 return static_cast<T*
>(
138 gtl::FindWithDefault(singletons_, gtl::FastTypeId<T>(),
nullptr));
146 template <
typename T>
148 cleanup_list_.emplace_back(
new Delete<T>(t));
156 template <
typename T>
158 T* new_t = MyNew<T>(0);
168 template <
typename T>
170 const size_t type_id = gtl::FastTypeId<T>();
171 CHECK(!gtl::ContainsKey(singletons_, type_id));
172 singletons_[type_id] = non_owned_class;
175 const std::string&
Name()
const {
return name_; }
183 template <
typename T>
184 decltype(T(
static_cast<Model*
>(
nullptr)))* MyNew(
int) {
187 template <
typename T>
192 const std::string name_;
195 absl::flat_hash_map< size_t,
void*> singletons_;
197 struct DeleteInterface {
198 virtual ~DeleteInterface() =
default;
200 template <
typename T>
201 class Delete :
public DeleteInterface {
203 explicit Delete(T* t) : to_delete_(t) {}
204 ~Delete()
override =
default;
207 std::unique_ptr<T> to_delete_;
215 std::vector<std::unique_ptr<DeleteInterface>> cleanup_list_;
217 DISALLOW_COPY_AND_ASSIGN(
Model);
223 #endif // OR_TOOLS_SAT_MODEL_H_
const T * Get() const
Likes GetOrCreate() but do not create the object if it is non-existing.
Model(std::string name)
When there is more than one model in an application, it makes sense to name them for debugging or log...
Class that owns everything related to a particular optimization model.
T * GetOrCreate()
Returns an object of type T that is unique to this model (like a "local" singleton).
T * Create()
This returns a non-singleton object owned by the model and created with the T(Model* model) construct...
T * Mutable() const
Same as Get(), but returns a mutable version of the object.
void TakeOwnership(T *t)
Gives ownership of a pointer to this model.
const std::string & Name() const
T Add(std::function< T(Model *)> f)
This makes it possible to have a nicer API on the client side, and it allows both of these forms:
T Get(std::function< T(const Model &)> f) const
Similar to Add() but this is const.
void Register(T *non_owned_class)
Register a non-owned class that will be "singleton" in the model.