Files
ortools-clone/docs/cpp_sat/model_8h_source.html
Laurent Perron 0ac3fa17eb more linear doc
2019-07-15 17:42:24 -07:00

72 lines
35 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>OR-Tools</title>
<meta http-equiv="Content-Type" content="text/html;"/>
<meta charset="utf-8"/>
<!--<link rel='stylesheet' type='text/css' href="https://fonts.googleapis.com/css?family=Ubuntu:400,700,400italic"/>-->
<link rel="stylesheet" type="text/css" href="ortools.css" title="default" media="screen,print" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
</head>
<body>
<div id="banner-container">
<div id="banner">
<span id="sfml">OR-Tools 7.2</span>
</div>
</div>
<link rel="icon" href="https://developers.google.com/optimization/images/orLogo.png">
<div id="content">
<!-- Generated by Doxygen 1.8.15 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li class="current"><a href="files.html"><span>Files</span></a></li>
</ul>
</div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('model_8h_source.html','');});
/* @license-end */
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle">
<div class="title">model.h</div> </div>
</div><!--header-->
<div class="contents">
<a href="model_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span>&#160;<span class="comment">// Copyright 2010-2018 Google LLC</span></div><div class="line"><a name="l00002"></a><span class="lineno"> 2</span>&#160;<span class="comment">// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span></div><div class="line"><a name="l00003"></a><span class="lineno"> 3</span>&#160;<span class="comment">// you may not use this file except in compliance with the License.</span></div><div class="line"><a name="l00004"></a><span class="lineno"> 4</span>&#160;<span class="comment">// You may obtain a copy of the License at</span></div><div class="line"><a name="l00005"></a><span class="lineno"> 5</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00006"></a><span class="lineno"> 6</span>&#160;<span class="comment">// http://www.apache.org/licenses/LICENSE-2.0</span></div><div class="line"><a name="l00007"></a><span class="lineno"> 7</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00008"></a><span class="lineno"> 8</span>&#160;<span class="comment">// Unless required by applicable law or agreed to in writing, software</span></div><div class="line"><a name="l00009"></a><span class="lineno"> 9</span>&#160;<span class="comment">// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span></div><div class="line"><a name="l00010"></a><span class="lineno"> 10</span>&#160;<span class="comment">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></div><div class="line"><a name="l00011"></a><span class="lineno"> 11</span>&#160;<span class="comment">// See the License for the specific language governing permissions and</span></div><div class="line"><a name="l00012"></a><span class="lineno"> 12</span>&#160;<span class="comment">// limitations under the License.</span></div><div class="line"><a name="l00013"></a><span class="lineno"> 13</span>&#160;</div><div class="line"><a name="l00014"></a><span class="lineno"> 14</span>&#160;<span class="preprocessor">#ifndef OR_TOOLS_SAT_MODEL_H_</span></div><div class="line"><a name="l00015"></a><span class="lineno"> 15</span>&#160;<span class="preprocessor">#define OR_TOOLS_SAT_MODEL_H_</span></div><div class="line"><a name="l00016"></a><span class="lineno"> 16</span>&#160;</div><div class="line"><a name="l00017"></a><span class="lineno"> 17</span>&#160;<span class="preprocessor">#include &lt;cstddef&gt;</span></div><div class="line"><a name="l00018"></a><span class="lineno"> 18</span>&#160;<span class="preprocessor">#include &lt;functional&gt;</span></div><div class="line"><a name="l00019"></a><span class="lineno"> 19</span>&#160;<span class="preprocessor">#include &lt;map&gt;</span></div><div class="line"><a name="l00020"></a><span class="lineno"> 20</span>&#160;<span class="preprocessor">#include &lt;memory&gt;</span></div><div class="line"><a name="l00021"></a><span class="lineno"> 21</span>&#160;<span class="preprocessor">#include &lt;vector&gt;</span></div><div class="line"><a name="l00022"></a><span class="lineno"> 22</span>&#160;</div><div class="line"><a name="l00023"></a><span class="lineno"> 23</span>&#160;<span class="preprocessor">#include &quot;ortools/base/logging.h&quot;</span></div><div class="line"><a name="l00024"></a><span class="lineno"> 24</span>&#160;<span class="preprocessor">#include &quot;ortools/base/macros.h&quot;</span></div><div class="line"><a name="l00025"></a><span class="lineno"> 25</span>&#160;<span class="preprocessor">#include &quot;ortools/base/map_util.h&quot;</span></div><div class="line"><a name="l00026"></a><span class="lineno"> 26</span>&#160;<span class="preprocessor">#include &quot;ortools/base/typeid.h&quot;</span></div><div class="line"><a name="l00027"></a><span class="lineno"> 27</span>&#160;</div><div class="line"><a name="l00028"></a><span class="lineno"> 28</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespaceoperations__research.html">operations_research</a> {</div><div class="line"><a name="l00029"></a><span class="lineno"> 29</span>&#160;<span class="keyword">namespace </span>sat {</div><div class="line"><a name="l00030"></a><span class="lineno"> 30</span>&#160;</div><div class="line"><a name="l00031"></a><span class="lineno"> 31</span>&#160;<span class="comment">// Class that owns everything related to a particular optimization model.</span></div><div class="line"><a name="l00032"></a><span class="lineno"> 32</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00033"></a><span class="lineno"> 33</span>&#160;<span class="comment">// This class is actually a fully generic wrapper that can hold any type of</span></div><div class="line"><a name="l00034"></a><span class="lineno"> 34</span>&#160;<span class="comment">// constraints, watchers, solvers and provide a mecanism to wire them together.</span></div><div class="line"><a name="l00035"></a><span class="lineno"><a class="line" href="classoperations__research_1_1sat_1_1Model.html"> 35</a></span>&#160;<span class="keyword">class </span><a class="code" href="classoperations__research_1_1sat_1_1Model.html">Model</a> {</div><div class="line"><a name="l00036"></a><span class="lineno"> 36</span>&#160; <span class="keyword">public</span>:</div><div class="line"><a name="l00037"></a><span class="lineno"><a class="line" href="classoperations__research_1_1sat_1_1Model.html#a7d97534275c629f2917ed5a029e2e2c5"> 37</a></span>&#160; <a class="code" href="classoperations__research_1_1sat_1_1Model.html#a7d97534275c629f2917ed5a029e2e2c5">Model</a>() {}</div><div class="line"><a name="l00038"></a><span class="lineno"> 38</span>&#160;</div><div class="line"><a name="l00039"></a><span class="lineno"> 39</span>&#160; <span class="comment">// This allows to have a nicer API on the client side, and it allows both of</span></div><div class="line"><a name="l00040"></a><span class="lineno"> 40</span>&#160; <span class="comment">// these forms:</span></div><div class="line"><a name="l00041"></a><span class="lineno"> 41</span>&#160; <span class="comment">// - ConstraintCreationFunction(contraint_args, &amp;model);</span></div><div class="line"><a name="l00042"></a><span class="lineno"> 42</span>&#160; <span class="comment">// - model.Add(ConstraintCreationFunction(contraint_args));</span></div><div class="line"><a name="l00043"></a><span class="lineno"> 43</span>&#160; <span class="comment">//</span></div><div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160; <span class="comment">// The second form is a bit nicer for the client and it also allows to store</span></div><div class="line"><a name="l00045"></a><span class="lineno"> 45</span>&#160; <span class="comment">// constraints and add them later. However, the function creating the</span></div><div class="line"><a name="l00046"></a><span class="lineno"> 46</span>&#160; <span class="comment">// constraint is slighly more involved:</span></div><div class="line"><a name="l00047"></a><span class="lineno"> 47</span>&#160; <span class="comment">//</span></div><div class="line"><a name="l00048"></a><span class="lineno"> 48</span>&#160; <span class="comment">// std::function&lt;void(Model*)&gt; ConstraintCreationFunction(contraint_args) {</span></div><div class="line"><a name="l00049"></a><span class="lineno"> 49</span>&#160; <span class="comment">// return [=] (Model* model) {</span></div><div class="line"><a name="l00050"></a><span class="lineno"> 50</span>&#160; <span class="comment">// ... the same code ...</span></div><div class="line"><a name="l00051"></a><span class="lineno"> 51</span>&#160; <span class="comment">// };</span></div><div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160; <span class="comment">// }</span></div><div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160; <span class="comment">//</span></div><div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160; <span class="comment">// We also have a templated return value for the functions that need it like</span></div><div class="line"><a name="l00055"></a><span class="lineno"> 55</span>&#160; <span class="comment">// const BooleanVariable b = model.Add(NewBooleanVariable());</span></div><div class="line"><a name="l00056"></a><span class="lineno"> 56</span>&#160; <span class="comment">// const IntegerVariable i = model.Add(NewWeightedSum(weigths, variables));</span></div><div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160; <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00058"></a><span class="lineno"><a class="line" href="classoperations__research_1_1sat_1_1Model.html#a059b9d223761f2b9cc82df4871ae36fa"> 58</a></span>&#160; T <a class="code" href="classoperations__research_1_1sat_1_1Model.html#a059b9d223761f2b9cc82df4871ae36fa">Add</a>(std::function&lt;T(<a class="code" href="classoperations__research_1_1sat_1_1Model.html">Model</a>*)&gt; f) {</div><div class="line"><a name="l00059"></a><span class="lineno"> 59</span>&#160; <span class="keywordflow">return</span> f(<span class="keyword">this</span>);</div><div class="line"><a name="l00060"></a><span class="lineno"> 60</span>&#160; }</div><div class="line"><a name="l00061"></a><span class="lineno"> 61</span>&#160;</div><div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160; <span class="comment">// Similar to Add() but this is const.</span></div><div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160; <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00064"></a><span class="lineno"><a class="line" href="classoperations__research_1_1sat_1_1Model.html#a162bdd37a619fd04f9085005008951d9"> 64</a></span>&#160; T <a class="code" href="classoperations__research_1_1sat_1_1Model.html#a162bdd37a619fd04f9085005008951d9">Get</a>(std::function&lt;T(<span class="keyword">const</span> <a class="code" href="classoperations__research_1_1sat_1_1Model.html">Model</a>&amp;)&gt; f)<span class="keyword"> const </span>{</div><div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160; <span class="keywordflow">return</span> f(*<span class="keyword">this</span>);</div><div class="line"><a name="l00066"></a><span class="lineno"> 66</span>&#160; }</div><div class="line"><a name="l00067"></a><span class="lineno"> 67</span>&#160;</div><div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160; <span class="comment">// Returns an object of type T that is unique to this model (like a &quot;local&quot;</span></div><div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160; <span class="comment">// singleton). This returns an already created instance or create a new one if</span></div><div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160; <span class="comment">// needed using the T(Model* model) constructor if it exist or T() otherwise.</span></div><div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160; <span class="comment">//</span></div><div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160; <span class="comment">// This works a bit like in a dependency injection framework and allows to</span></div><div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160; <span class="comment">// really easily wire all the classes that make up a solver together. For</span></div><div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160; <span class="comment">// instance a constraint can depends on the LiteralTrail, or the IntegerTrail</span></div><div class="line"><a name="l00075"></a><span class="lineno"> 75</span>&#160; <span class="comment">// or both, it can depend on a Watcher class to register itself in order to</span></div><div class="line"><a name="l00076"></a><span class="lineno"> 76</span>&#160; <span class="comment">// be called when needed and so on.</span></div><div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160; <span class="comment">//</span></div><div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160; <span class="comment">// IMPORTANT: the Model* constructors function shouldn&#39;t form a cycle between</span></div><div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160; <span class="comment">// each other, otherwise this will crash the program.</span></div><div class="line"><a name="l00080"></a><span class="lineno"> 80</span>&#160; <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00081"></a><span class="lineno"><a class="line" href="classoperations__research_1_1sat_1_1Model.html#af4eb422b7cfd963c58d65b18b4e47717"> 81</a></span>&#160; T* <a class="code" href="classoperations__research_1_1sat_1_1Model.html#af4eb422b7cfd963c58d65b18b4e47717">GetOrCreate</a>() {</div><div class="line"><a name="l00082"></a><span class="lineno"> 82</span>&#160; <span class="keyword">const</span> <span class="keywordtype">size_t</span> type_id = gtl::FastTypeId&lt;T&gt;();</div><div class="line"><a name="l00083"></a><span class="lineno"> 83</span>&#160; <span class="keywordflow">if</span> (!gtl::ContainsKey(singletons_, type_id)) {</div><div class="line"><a name="l00084"></a><span class="lineno"> 84</span>&#160; <span class="comment">// TODO(user): directly store std::unique_ptr&lt;&gt; in singletons_?</span></div><div class="line"><a name="l00085"></a><span class="lineno"> 85</span>&#160; T* new_t = MyNew&lt;T&gt;(0);</div><div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160; singletons_[type_id] = new_t;</div><div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160; <a class="code" href="classoperations__research_1_1sat_1_1Model.html#a6411a892fd57781615c9edf80081026c">TakeOwnership</a>(new_t);</div><div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160; <span class="keywordflow">return</span> new_t;</div><div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160; }</div><div class="line"><a name="l00090"></a><span class="lineno"> 90</span>&#160; <span class="keywordflow">return</span> static_cast&lt;T*&gt;(gtl::FindOrDie(singletons_, type_id));</div><div class="line"><a name="l00091"></a><span class="lineno"> 91</span>&#160; }</div><div class="line"><a name="l00092"></a><span class="lineno"> 92</span>&#160;</div><div class="line"><a name="l00093"></a><span class="lineno"> 93</span>&#160; <span class="comment">// Likes GetOrCreate() but do not create the object if it is non-existing.</span></div><div class="line"><a name="l00094"></a><span class="lineno"> 94</span>&#160; <span class="comment">// This returns a const version of the object.</span></div><div class="line"><a name="l00095"></a><span class="lineno"> 95</span>&#160; <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00096"></a><span class="lineno"><a class="line" href="classoperations__research_1_1sat_1_1Model.html#a211cf867e9edd220616c0a8f6ba4b71d"> 96</a></span>&#160; <span class="keyword">const</span> T* <a class="code" href="classoperations__research_1_1sat_1_1Model.html#a211cf867e9edd220616c0a8f6ba4b71d">Get</a>()<span class="keyword"> const </span>{</div><div class="line"><a name="l00097"></a><span class="lineno"> 97</span>&#160; <span class="keywordflow">return</span> static_cast&lt;const T*&gt;(</div><div class="line"><a name="l00098"></a><span class="lineno"> 98</span>&#160; gtl::FindWithDefault(singletons_, gtl::FastTypeId&lt;T&gt;(), <span class="keyword">nullptr</span>));</div><div class="line"><a name="l00099"></a><span class="lineno"> 99</span>&#160; }</div><div class="line"><a name="l00100"></a><span class="lineno"> 100</span>&#160;</div><div class="line"><a name="l00101"></a><span class="lineno"> 101</span>&#160; <span class="comment">// Same as Get(), but returns a mutable version of the object.</span></div><div class="line"><a name="l00102"></a><span class="lineno"> 102</span>&#160; <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00103"></a><span class="lineno"><a class="line" href="classoperations__research_1_1sat_1_1Model.html#a6226de9875c58b81f461c123577d1689"> 103</a></span>&#160; T* <a class="code" href="classoperations__research_1_1sat_1_1Model.html#a6226de9875c58b81f461c123577d1689">Mutable</a>()<span class="keyword"> const </span>{</div><div class="line"><a name="l00104"></a><span class="lineno"> 104</span>&#160; <span class="keywordflow">return</span> static_cast&lt;T*&gt;(</div><div class="line"><a name="l00105"></a><span class="lineno"> 105</span>&#160; gtl::FindWithDefault(singletons_, gtl::FastTypeId&lt;T&gt;(), <span class="keyword">nullptr</span>));</div><div class="line"><a name="l00106"></a><span class="lineno"> 106</span>&#160; }</div><div class="line"><a name="l00107"></a><span class="lineno"> 107</span>&#160;</div><div class="line"><a name="l00108"></a><span class="lineno"> 108</span>&#160; <span class="comment">// Gives ownership of a pointer to this model.</span></div><div class="line"><a name="l00109"></a><span class="lineno"> 109</span>&#160; <span class="comment">// It will be destroyed when the model is.</span></div><div class="line"><a name="l00110"></a><span class="lineno"> 110</span>&#160; <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00111"></a><span class="lineno"><a class="line" href="classoperations__research_1_1sat_1_1Model.html#a6411a892fd57781615c9edf80081026c"> 111</a></span>&#160; <span class="keywordtype">void</span> <a class="code" href="classoperations__research_1_1sat_1_1Model.html#a6411a892fd57781615c9edf80081026c">TakeOwnership</a>(T* t) {</div><div class="line"><a name="l00112"></a><span class="lineno"> 112</span>&#160; cleanup_list_.emplace_back(<span class="keyword">new</span> Delete&lt;T&gt;(t));</div><div class="line"><a name="l00113"></a><span class="lineno"> 113</span>&#160; }</div><div class="line"><a name="l00114"></a><span class="lineno"> 114</span>&#160;</div><div class="line"><a name="l00115"></a><span class="lineno"> 115</span>&#160; <span class="comment">// This returns a non-singleton object owned by the model and created with the</span></div><div class="line"><a name="l00116"></a><span class="lineno"> 116</span>&#160; <span class="comment">// T(Model* model) constructor if it exist or the T() constructor otherwise.</span></div><div class="line"><a name="l00117"></a><span class="lineno"> 117</span>&#160; <span class="comment">// It is just a shortcut to new + TakeOwnership().</span></div><div class="line"><a name="l00118"></a><span class="lineno"> 118</span>&#160; <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00119"></a><span class="lineno"><a class="line" href="classoperations__research_1_1sat_1_1Model.html#a08f2ee3dc9fa03be18a4c38304e068d9"> 119</a></span>&#160; T* <a class="code" href="classoperations__research_1_1sat_1_1Model.html#a08f2ee3dc9fa03be18a4c38304e068d9">Create</a>() {</div><div class="line"><a name="l00120"></a><span class="lineno"> 120</span>&#160; T* new_t = MyNew&lt;T&gt;(0);</div><div class="line"><a name="l00121"></a><span class="lineno"> 121</span>&#160; <a class="code" href="classoperations__research_1_1sat_1_1Model.html#a6411a892fd57781615c9edf80081026c">TakeOwnership</a>(new_t);</div><div class="line"><a name="l00122"></a><span class="lineno"> 122</span>&#160; <span class="keywordflow">return</span> new_t;</div><div class="line"><a name="l00123"></a><span class="lineno"> 123</span>&#160; }</div><div class="line"><a name="l00124"></a><span class="lineno"> 124</span>&#160;</div><div class="line"><a name="l00125"></a><span class="lineno"> 125</span>&#160; <span class="comment">// Register a non-owned class that will be &quot;singleton&quot; in the model.</span></div><div class="line"><a name="l00126"></a><span class="lineno"> 126</span>&#160; <span class="comment">// It is an error to call this on an already registered class.</span></div><div class="line"><a name="l00127"></a><span class="lineno"> 127</span>&#160; <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00128"></a><span class="lineno"><a class="line" href="classoperations__research_1_1sat_1_1Model.html#a78f476ca154e64d281ae07efd825a779"> 128</a></span>&#160; <span class="keywordtype">void</span> <a class="code" href="classoperations__research_1_1sat_1_1Model.html#a78f476ca154e64d281ae07efd825a779">Register</a>(T* non_owned_class) {</div><div class="line"><a name="l00129"></a><span class="lineno"> 129</span>&#160; <span class="keyword">const</span> <span class="keywordtype">size_t</span> type_id = gtl::FastTypeId&lt;T&gt;();</div><div class="line"><a name="l00130"></a><span class="lineno"> 130</span>&#160; CHECK(!gtl::ContainsKey(singletons_, type_id));</div><div class="line"><a name="l00131"></a><span class="lineno"> 131</span>&#160; singletons_[type_id] = non_owned_class;</div><div class="line"><a name="l00132"></a><span class="lineno"> 132</span>&#160; }</div><div class="line"><a name="l00133"></a><span class="lineno"> 133</span>&#160;</div><div class="line"><a name="l00134"></a><span class="lineno"> 134</span>&#160; <span class="keyword">private</span>:</div><div class="line"><a name="l00135"></a><span class="lineno"> 135</span>&#160; <span class="comment">// We want to call the constructor T(model*) if it exists or just T() if</span></div><div class="line"><a name="l00136"></a><span class="lineno"> 136</span>&#160; <span class="comment">// it doesn&#39;t. For this we use some template &quot;magic&quot;:</span></div><div class="line"><a name="l00137"></a><span class="lineno"> 137</span>&#160; <span class="comment">// - The first MyNew() will only be defined if the type in decltype() exist.</span></div><div class="line"><a name="l00138"></a><span class="lineno"> 138</span>&#160; <span class="comment">// - The second MyNew() will always be defined, but because of the ellipsis</span></div><div class="line"><a name="l00139"></a><span class="lineno"> 139</span>&#160; <span class="comment">// it has lower priority that the first one.</span></div><div class="line"><a name="l00140"></a><span class="lineno"> 140</span>&#160; <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00141"></a><span class="lineno"> 141</span>&#160; decltype(T(static_cast&lt;Model*&gt;(<span class="keyword">nullptr</span>)))* MyNew(<span class="keywordtype">int</span>) {</div><div class="line"><a name="l00142"></a><span class="lineno"> 142</span>&#160; <span class="keywordflow">return</span> <span class="keyword">new</span> T(<span class="keyword">this</span>);</div><div class="line"><a name="l00143"></a><span class="lineno"> 143</span>&#160; }</div><div class="line"><a name="l00144"></a><span class="lineno"> 144</span>&#160; <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00145"></a><span class="lineno"> 145</span>&#160; T* MyNew(...) {</div><div class="line"><a name="l00146"></a><span class="lineno"> 146</span>&#160; <span class="keywordflow">return</span> <span class="keyword">new</span> T();</div><div class="line"><a name="l00147"></a><span class="lineno"> 147</span>&#160; }</div><div class="line"><a name="l00148"></a><span class="lineno"> 148</span>&#160;</div><div class="line"><a name="l00149"></a><span class="lineno"> 149</span>&#160; <span class="comment">// Map of FastTypeId&lt;T&gt; to a &quot;singleton&quot; of type T.</span></div><div class="line"><a name="l00150"></a><span class="lineno"> 150</span>&#160; std::map&lt;<span class="comment">/*typeid*/</span> size_t, <span class="keywordtype">void</span>*&gt; singletons_;</div><div class="line"><a name="l00151"></a><span class="lineno"> 151</span>&#160;</div><div class="line"><a name="l00152"></a><span class="lineno"> 152</span>&#160; <span class="keyword">struct </span>DeleteInterface {</div><div class="line"><a name="l00153"></a><span class="lineno"> 153</span>&#160; <span class="keyword">virtual</span> ~DeleteInterface() = <span class="keywordflow">default</span>;</div><div class="line"><a name="l00154"></a><span class="lineno"> 154</span>&#160; };</div><div class="line"><a name="l00155"></a><span class="lineno"> 155</span>&#160; <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00156"></a><span class="lineno"> 156</span>&#160; <span class="keyword">class </span>Delete : <span class="keyword">public</span> DeleteInterface {</div><div class="line"><a name="l00157"></a><span class="lineno"> 157</span>&#160; <span class="keyword">public</span>:</div><div class="line"><a name="l00158"></a><span class="lineno"> 158</span>&#160; <span class="keyword">explicit</span> Delete(T* t) : to_delete_(t) {}</div><div class="line"><a name="l00159"></a><span class="lineno"> 159</span>&#160; ~Delete() <span class="keyword">override</span> = <span class="keywordflow">default</span>;</div><div class="line"><a name="l00160"></a><span class="lineno"> 160</span>&#160;</div><div class="line"><a name="l00161"></a><span class="lineno"> 161</span>&#160; <span class="keyword">private</span>:</div><div class="line"><a name="l00162"></a><span class="lineno"> 162</span>&#160; std::unique_ptr&lt;T&gt; to_delete_;</div><div class="line"><a name="l00163"></a><span class="lineno"> 163</span>&#160; };</div><div class="line"><a name="l00164"></a><span class="lineno"> 164</span>&#160;</div><div class="line"><a name="l00165"></a><span class="lineno"> 165</span>&#160; <span class="comment">// The list of items to delete.</span></div><div class="line"><a name="l00166"></a><span class="lineno"> 166</span>&#160; <span class="comment">//</span></div><div class="line"><a name="l00167"></a><span class="lineno"> 167</span>&#160; <span class="comment">// TODO(user): I don&#39;t think we need the two layers of unique_ptr, but we</span></div><div class="line"><a name="l00168"></a><span class="lineno"> 168</span>&#160; <span class="comment">// don&#39;t care too much about efficiency here and this was easier to get</span></div><div class="line"><a name="l00169"></a><span class="lineno"> 169</span>&#160; <span class="comment">// working.</span></div><div class="line"><a name="l00170"></a><span class="lineno"> 170</span>&#160; std::vector&lt;std::unique_ptr&lt;DeleteInterface&gt;&gt; cleanup_list_;</div><div class="line"><a name="l00171"></a><span class="lineno"> 171</span>&#160;</div><div class="line"><a name="l00172"></a><span class="lineno"> 172</span>&#160; DISALLOW_COPY_AND_ASSIGN(<a class="code" href="classoperations__research_1_1sat_1_1Model.html#a7d97534275c629f2917ed5a029e2e2c5">Model</a>);</div><div class="line"><a name="l00173"></a><span class="lineno"> 173</span>&#160;};</div><div class="line"><a name="l00174"></a><span class="lineno"> 174</span>&#160;</div><div class="line"><a name="l00175"></a><span class="lineno"> 175</span>&#160;} <span class="comment">// namespace sat</span></div><div class="line"><a name="l00176"></a><span class="lineno"> 176</span>&#160;} <span class="comment">// namespace operations_research</span></div><div class="line"><a name="l00177"></a><span class="lineno"> 177</span>&#160;</div><div class="line"><a name="l00178"></a><span class="lineno"> 178</span>&#160;<span class="preprocessor">#endif // OR_TOOLS_SAT_MODEL_H_</span></div><div class="ttc" id="classoperations__research_1_1sat_1_1Model_html_a162bdd37a619fd04f9085005008951d9"><div class="ttname"><a href="classoperations__research_1_1sat_1_1Model.html#a162bdd37a619fd04f9085005008951d9">operations_research::sat::Model::Get</a></div><div class="ttdeci">T Get(std::function&lt; T(const Model &amp;)&gt; f) const</div><div class="ttdoc">Similar to Add() but this is const.</div><div class="ttdef"><b>Definition:</b> <a href="model_8h_source.html#l00064">model.h:64</a></div></div>
<div class="ttc" id="classoperations__research_1_1sat_1_1Model_html_a7d97534275c629f2917ed5a029e2e2c5"><div class="ttname"><a href="classoperations__research_1_1sat_1_1Model.html#a7d97534275c629f2917ed5a029e2e2c5">operations_research::sat::Model::Model</a></div><div class="ttdeci">Model()</div><div class="ttdef"><b>Definition:</b> <a href="model_8h_source.html#l00037">model.h:37</a></div></div>
<div class="ttc" id="classoperations__research_1_1sat_1_1Model_html_a6411a892fd57781615c9edf80081026c"><div class="ttname"><a href="classoperations__research_1_1sat_1_1Model.html#a6411a892fd57781615c9edf80081026c">operations_research::sat::Model::TakeOwnership</a></div><div class="ttdeci">void TakeOwnership(T *t)</div><div class="ttdoc">Gives ownership of a pointer to this model.</div><div class="ttdef"><b>Definition:</b> <a href="model_8h_source.html#l00111">model.h:111</a></div></div>
<div class="ttc" id="classoperations__research_1_1sat_1_1Model_html_a78f476ca154e64d281ae07efd825a779"><div class="ttname"><a href="classoperations__research_1_1sat_1_1Model.html#a78f476ca154e64d281ae07efd825a779">operations_research::sat::Model::Register</a></div><div class="ttdeci">void Register(T *non_owned_class)</div><div class="ttdoc">Register a non-owned class that will be &quot;singleton&quot; in the model.</div><div class="ttdef"><b>Definition:</b> <a href="model_8h_source.html#l00128">model.h:128</a></div></div>
<div class="ttc" id="classoperations__research_1_1sat_1_1Model_html_a08f2ee3dc9fa03be18a4c38304e068d9"><div class="ttname"><a href="classoperations__research_1_1sat_1_1Model.html#a08f2ee3dc9fa03be18a4c38304e068d9">operations_research::sat::Model::Create</a></div><div class="ttdeci">T * Create()</div><div class="ttdoc">This returns a non-singleton object owned by the model and created with the T(Model* model) construct...</div><div class="ttdef"><b>Definition:</b> <a href="model_8h_source.html#l00119">model.h:119</a></div></div>
<div class="ttc" id="classoperations__research_1_1sat_1_1Model_html"><div class="ttname"><a href="classoperations__research_1_1sat_1_1Model.html">operations_research::sat::Model</a></div><div class="ttdoc">Class that owns everything related to a particular optimization model.</div><div class="ttdef"><b>Definition:</b> <a href="model_8h_source.html#l00035">model.h:35</a></div></div>
<div class="ttc" id="classoperations__research_1_1sat_1_1Model_html_a211cf867e9edd220616c0a8f6ba4b71d"><div class="ttname"><a href="classoperations__research_1_1sat_1_1Model.html#a211cf867e9edd220616c0a8f6ba4b71d">operations_research::sat::Model::Get</a></div><div class="ttdeci">const T * Get() const</div><div class="ttdoc">Likes GetOrCreate() but do not create the object if it is non-existing.</div><div class="ttdef"><b>Definition:</b> <a href="model_8h_source.html#l00096">model.h:96</a></div></div>
<div class="ttc" id="classoperations__research_1_1sat_1_1Model_html_af4eb422b7cfd963c58d65b18b4e47717"><div class="ttname"><a href="classoperations__research_1_1sat_1_1Model.html#af4eb422b7cfd963c58d65b18b4e47717">operations_research::sat::Model::GetOrCreate</a></div><div class="ttdeci">T * GetOrCreate()</div><div class="ttdoc">Returns an object of type T that is unique to this model (like a &quot;local&quot; singleton).</div><div class="ttdef"><b>Definition:</b> <a href="model_8h_source.html#l00081">model.h:81</a></div></div>
<div class="ttc" id="classoperations__research_1_1sat_1_1Model_html_a059b9d223761f2b9cc82df4871ae36fa"><div class="ttname"><a href="classoperations__research_1_1sat_1_1Model.html#a059b9d223761f2b9cc82df4871ae36fa">operations_research::sat::Model::Add</a></div><div class="ttdeci">T Add(std::function&lt; T(Model *)&gt; f)</div><div class="ttdoc">This allows to have a nicer API on the client side, and it allows both of these forms:</div><div class="ttdef"><b>Definition:</b> <a href="model_8h_source.html#l00058">model.h:58</a></div></div>
<div class="ttc" id="classoperations__research_1_1sat_1_1Model_html_a6226de9875c58b81f461c123577d1689"><div class="ttname"><a href="classoperations__research_1_1sat_1_1Model.html#a6226de9875c58b81f461c123577d1689">operations_research::sat::Model::Mutable</a></div><div class="ttdeci">T * Mutable() const</div><div class="ttdoc">Same as Get(), but returns a mutable version of the object.</div><div class="ttdef"><b>Definition:</b> <a href="model_8h_source.html#l00103">model.h:103</a></div></div>
<div class="ttc" id="namespaceoperations__research_html"><div class="ttname"><a href="namespaceoperations__research.html">operations_research</a></div><div class="ttdoc">Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in c...</div><div class="ttdef"><b>Definition:</b> <a href="cp__model_8h_source.html#l00048">cp_model.h:48</a></div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
</div>
<div id="footer-container">
<div id="footer">
</div>
</div>
</body>
</html>