Go to the documentation of this file.
14 #ifndef OR_TOOLS_SAT_MODEL_H_
15 #define OR_TOOLS_SAT_MODEL_H_
23 #include "ortools/base/logging.h"
24 #include "ortools/base/macros.h"
25 #include "ortools/base/map_util.h"
26 #include "ortools/base/typeid.h"
72 T
Get(std::function<T(
const Model&)> f)
const {
92 const size_t type_id = gtl::FastTypeId<T>();
93 if (!gtl::ContainsKey(singletons_, type_id)) {
95 T* new_t = MyNew<T>(0);
96 singletons_[type_id] = new_t;
100 return static_cast<T*>(gtl::FindOrDie(singletons_, type_id));
108 template <
typename T>
110 return static_cast<const T*>(
111 gtl::FindWithDefault(singletons_, gtl::FastTypeId<T>(),
nullptr));
117 template <
typename T>
119 return static_cast<T*>(
120 gtl::FindWithDefault(singletons_, gtl::FastTypeId<T>(),
nullptr));
128 template <
typename T>
130 cleanup_list_.emplace_back(
new Delete<T>(t));
138 template <
typename T>
140 T* new_t = MyNew<T>(0);
150 template <
typename T>
152 const size_t type_id = gtl::FastTypeId<T>();
153 CHECK(!gtl::ContainsKey(singletons_, type_id));
154 singletons_[type_id] = non_owned_class;
163 template <
typename T>
164 decltype(T(static_cast<Model*>(
nullptr)))* MyNew(
int) {
167 template <
typename T>
173 std::map< size_t,
void*> singletons_;
175 struct DeleteInterface {
176 virtual ~DeleteInterface() =
default;
178 template <
typename T>
179 class Delete :
public DeleteInterface {
181 explicit Delete(T* t) : to_delete_(t) {}
182 ~Delete()
override =
default;
185 std::unique_ptr<T> to_delete_;
193 std::vector<std::unique_ptr<DeleteInterface>> cleanup_list_;
195 DISALLOW_COPY_AND_ASSIGN(
Model);
201 #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: