14#ifndef OR_TOOLS_SAT_MODEL_H_
15#define OR_TOOLS_SAT_MODEL_H_
25#include "absl/container/flat_hash_map.h"
26#include "absl/log/check.h"
27#include "absl/meta/type_traits.h"
47 for (
int i = cleanup_list_.size() - 1;
i >= 0; --
i) {
48 cleanup_list_[
i].reset();
56 explicit Model(std::string name) : name_(name) {}
93 T
Get(std::function<T(
const Model&)> f)
const {
111 template <
typename T>
114 auto find = singletons_.find(type_id);
115 if (find != singletons_.end()) {
116 return static_cast<T*
>(find->second);
121 T* new_t = MyNew<T>(0);
122 singletons_[type_id] = new_t;
132 template <
typename T>
135 return it != singletons_.end() ?
static_cast<const T*
>(it->second)
142 template <
typename T>
145 return it != singletons_.end() ?
static_cast<T*
>(it->second) :
nullptr;
153 template <
typename T>
155 cleanup_list_.emplace_back(
new Delete<T>(t));
164 template <
typename T>
166 T* new_t = MyNew<T>(0);
176 template <
typename T>
179 CHECK(!singletons_.contains(type_id));
180 singletons_[type_id] = non_owned_class;
183 const std::string&
Name()
const {
return name_; }
191 template <
typename T>
192 decltype(T(
static_cast<Model*
>(
nullptr)))* MyNew(
int) {
195 template <
typename T>
200 const std::string name_;
203 absl::flat_hash_map< size_t,
void*> singletons_;
205 struct DeleteInterface {
206 virtual ~DeleteInterface() =
default;
208 template <
typename T>
209 class Delete :
public DeleteInterface {
211 explicit Delete(T* t) : to_delete_(t) {}
212 ~Delete()
override =
default;
215 std::unique_ptr<T> to_delete_;
223 std::vector<std::unique_ptr<DeleteInterface>> cleanup_list_;
T Add(std::function< T(Model *)> f)
T Get(std::function< T(const Model &)> f) const
Similar to Add() but this is const.
const std::string & Name() const
Model(const Model &)=delete
This type is neither copyable nor movable.
void Register(T *non_owned_class)
Model & operator=(const Model &)=delete
In SWIG mode, we don't want anything besides these top-level includes.