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"
46 explicit Model(std::string name) : name_(name) {}
79 T
Get(std::function<T(
const Model&)> f)
const {
99 const size_t type_id = gtl::FastTypeId<T>();
100 auto find = singletons_.find(type_id);
101 if (find != singletons_.end()) {
102 return static_cast<T*
>(find->second);
107 T* new_t = MyNew<T>(0);
108 singletons_[type_id] = new_t;
118 template <
typename T>
120 return static_cast<const T*
>(
121 gtl::FindWithDefault(singletons_, gtl::FastTypeId<T>(),
nullptr));
127 template <
typename T>
129 return static_cast<T*
>(
130 gtl::FindWithDefault(singletons_, gtl::FastTypeId<T>(),
nullptr));
138 template <
typename T>
140 cleanup_list_.emplace_back(
new Delete<T>(t));
148 template <
typename T>
150 T* new_t = MyNew<T>(0);
160 template <
typename T>
162 const size_t type_id = gtl::FastTypeId<T>();
163 CHECK(!gtl::ContainsKey(singletons_, type_id));
164 singletons_[type_id] = non_owned_class;
167 const std::string&
Name()
const {
return name_; }
175 template <
typename T>
176 decltype(T(
static_cast<Model*
>(
nullptr)))* MyNew(
int) {
179 template <
typename T>
184 const std::string name_;
187 #if defined(__APPLE__)
188 std::map< size_t,
void*> singletons_;
190 absl::flat_hash_map< size_t,
void*> singletons_;
193 struct DeleteInterface {
194 virtual ~DeleteInterface() =
default;
196 template <
typename T>
197 class Delete :
public DeleteInterface {
199 explicit Delete(T* t) : to_delete_(t) {}
200 ~Delete()
override =
default;
203 std::unique_ptr<T> to_delete_;
211 std::vector<std::unique_ptr<DeleteInterface>> cleanup_list_;
213 DISALLOW_COPY_AND_ASSIGN(
Model);
219 #endif // OR_TOOLS_SAT_MODEL_H_
void TakeOwnership(T *t)
Gives ownership of a pointer to this model.
T * Create()
This returns a non-singleton object owned by the model and created with the T(Model* model) construct...
const T * Get() const
Likes GetOrCreate() but do not create the object if it is non-existing.
Class that owns everything related to a particular optimization model.
T Get(std::function< T(const Model &)> f) const
Similar to Add() but this is const.
T * Mutable() const
Same as Get(), but returns a mutable version of the object.
void Register(T *non_owned_class)
Register a non-owned class that will be "singleton" in the model.
Model(std::string name)
When there is more than one model in an application, it makes sense to name them for debugging or log...
T * GetOrCreate()
Returns an object of type T that is unique to this model (like a "local" singleton).
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: