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" 64 T
Get(std::function<T(
const Model&)> f)
const {
82 const size_t type_id = gtl::FastTypeId<T>();
83 if (!gtl::ContainsKey(singletons_, type_id)) {
85 T* new_t = MyNew<T>(0);
86 singletons_[type_id] = new_t;
90 return static_cast<T*>(gtl::FindOrDie(singletons_, type_id));
96 const T*
Get()
const {
97 return static_cast<const T*>(
98 gtl::FindWithDefault(singletons_, gtl::FastTypeId<T>(),
nullptr));
102 template <
typename T>
104 return static_cast<T*>(
105 gtl::FindWithDefault(singletons_, gtl::FastTypeId<T>(),
nullptr));
110 template <
typename T>
112 cleanup_list_.emplace_back(
new Delete<T>(t));
118 template <
typename T>
120 T* new_t = MyNew<T>(0);
127 template <
typename T>
129 const size_t type_id = gtl::FastTypeId<T>();
130 CHECK(!gtl::ContainsKey(singletons_, type_id));
131 singletons_[type_id] = non_owned_class;
140 template <
typename T>
141 decltype(T(static_cast<Model*>(
nullptr)))* MyNew(
int) {
144 template <
typename T>
150 std::map< size_t,
void*> singletons_;
152 struct DeleteInterface {
153 virtual ~DeleteInterface() =
default;
155 template <
typename T>
156 class Delete :
public DeleteInterface {
158 explicit Delete(T* t) : to_delete_(t) {}
159 ~Delete()
override =
default;
162 std::unique_ptr<T> to_delete_;
170 std::vector<std::unique_ptr<DeleteInterface>> cleanup_list_;
172 DISALLOW_COPY_AND_ASSIGN(
Model);
178 #endif // OR_TOOLS_SAT_MODEL_H_ T Get(std::function< T(const Model &)> f) const
Similar to Add() but this is const.
void TakeOwnership(T *t)
Gives ownership of a pointer to this model.
void Register(T *non_owned_class)
Register a non-owned class that will be "singleton" in the model.
T * Create()
This returns a non-singleton object owned by the model and created with the T(Model* model) construct...
Class that owns everything related to a particular optimization model.
const T * Get() const
Likes GetOrCreate() but do not create the object if it is non-existing.
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 allows to have a nicer API on the client side, and it allows both of these forms:
T * Mutable() const
Same as Get(), but returns a mutable version of the object.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...