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"
73 T
Get(std::function<T(
const Model&)> f)
const {
93 const size_t type_id = gtl::FastTypeId<T>();
94 auto find = singletons_.find(type_id);
95 if (find != singletons_.end()) {
96 return static_cast<T*
>(find->second);
101 T* new_t = MyNew<T>(0);
102 singletons_[type_id] = new_t;
112 template <
typename T>
114 return static_cast<const T*
>(
115 gtl::FindWithDefault(singletons_, gtl::FastTypeId<T>(),
nullptr));
121 template <
typename T>
123 return static_cast<T*
>(
124 gtl::FindWithDefault(singletons_, gtl::FastTypeId<T>(),
nullptr));
132 template <
typename T>
134 cleanup_list_.emplace_back(
new Delete<T>(t));
142 template <
typename T>
144 T* new_t = MyNew<T>(0);
154 template <
typename T>
156 const size_t type_id = gtl::FastTypeId<T>();
157 CHECK(!gtl::ContainsKey(singletons_, type_id));
158 singletons_[type_id] = non_owned_class;
167 template <
typename T>
168 decltype(T(
static_cast<Model*
>(
nullptr)))* MyNew(
int) {
171 template <
typename T>
177 #if defined(__APPLE__)
178 std::map< size_t,
void*> singletons_;
180 absl::flat_hash_map< size_t,
void*> singletons_;
183 struct DeleteInterface {
184 virtual ~DeleteInterface() =
default;
186 template <
typename T>
187 class Delete :
public DeleteInterface {
189 explicit Delete(T* t) : to_delete_(t) {}
190 ~Delete()
override =
default;
193 std::unique_ptr<T> to_delete_;
201 std::vector<std::unique_ptr<DeleteInterface>> cleanup_list_;
203 DISALLOW_COPY_AND_ASSIGN(
Model);
209 #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.
T * GetOrCreate()
Returns an object of type T that is unique to this model (like a "local" singleton).
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: