this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2017
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include <functional>
35
36namespace Gecode {
37
38 /// Function type for printing variable and value selection
39 template<class Var, class Val>
40 using VarValPrint = std::function<void(const Space& home, const Brancher& b,
41 unsigned int a,
42 Var x, int i, const Val& m,
43 std::ostream& o)>;
44
45 /// Class storing a print function
46 template<class View, class Val>
47 class BrancherPrint {
48 public:
49 /// The corresponding variable type
50 typedef typename View::VarType Var;
51 protected:
52 SharedData<VarValPrint<Var,Val>> p;
53 public:
54 /// Initialize
55 BrancherPrint(VarValPrint<Var,Val> vvp);
56 /// Initialize during cloning
57 BrancherPrint(BrancherPrint& bp);
58 /// Whether printing is enabled
59 operator bool(void) const;
60 /// Invoke print function
61 void operator ()(const Space& home, const Brancher& b,
62 unsigned int a,
63 View x, int i, const Val& m,
64 std::ostream& o) const;
65 /// Whether dispose must always be called (that is, notice is needed)
66 bool notice(void) const;
67 /// Delete
68 void dispose(Space& home);
69 };
70
71 /// Class without print function
72 template<class View, class Val>
73 class BrancherNoPrint {
74 public:
75 /// The corresponding variable type
76 typedef typename View::VarType Var;
77 public:
78 /// Initialize
79 BrancherNoPrint(VarValPrint<Var,Val> vvp);
80 /// Initialize during cloning
81 BrancherNoPrint(BrancherNoPrint& bp);
82 /// Whether printing is enabled
83 operator bool(void) const;
84 /// Invoke print function
85 void operator ()(const Space& home, const Brancher& b,
86 unsigned int a,
87 View x, int i, const Val& m,
88 std::ostream& o) const;
89 /// Whether dispose must always be called (that is, notice is needed)
90 bool notice(void) const;
91 /// Delete
92 void dispose(Space& home);
93 };
94
95
96
97 template<class View, class Val>
98 forceinline
99 BrancherPrint<View,Val>::BrancherPrint(VarValPrint<Var,Val> vvp) : p(vvp) {
100 if (!vvp)
101 throw Gecode::InvalidFunction("BrancherPrint::BrancherPrint");
102 }
103
104 template<class View, class Val>
105 forceinline
106 BrancherPrint<View,Val>::BrancherPrint(BrancherPrint<View,Val>& bp)
107 : p(bp.p) {
108 }
109
110 template<class View, class Val>
111 forceinline
112 BrancherPrint<View,Val>::operator bool(void) const {
113 return true;
114 }
115
116 template<class View, class Val>
117 forceinline void
118 BrancherPrint<View,Val>::operator ()(const Space& home, const Brancher& b,
119 unsigned int a,
120 View x, int i, const Val& m,
121 std::ostream& o) const {
122 GECODE_VALID_FUNCTION(p());
123 Var xv(x.varimp());
124 p()(home,b,a,xv,i,m,o);
125 }
126
127 template<class View, class Val>
128 forceinline bool
129 BrancherPrint<View,Val>::notice(void) const {
130 return true;
131 }
132
133 template<class View, class Val>
134 forceinline void
135 BrancherPrint<View,Val>::dispose(Space&) {
136 p.~SharedData<VarValPrint<Var,Val>>();
137 }
138
139
140 template<class View, class Val>
141 forceinline
142 BrancherNoPrint<View,Val>::BrancherNoPrint(VarValPrint<Var,Val> vvp) {
143 assert(!vvp);
144 (void) vvp;
145 }
146
147 template<class View, class Val>
148 forceinline
149 BrancherNoPrint<View,Val>::BrancherNoPrint(BrancherNoPrint<View,Val>&) {}
150
151 template<class View, class Val>
152 forceinline
153 BrancherNoPrint<View,Val>::operator bool(void) const {
154 return false;
155 }
156
157 template<class View, class Val>
158 forceinline void
159 BrancherNoPrint<View,Val>::operator ()(const Space&, const Brancher&,
160 unsigned int,
161 View, int, const Val&,
162 std::ostream&) const {}
163 template<class View, class Val>
164 forceinline bool
165 BrancherNoPrint<View,Val>::notice(void) const {
166 return false;
167 }
168
169 template<class View, class Val>
170 forceinline void
171 BrancherNoPrint<View,Val>::dispose(Space&) {}
172
173}
174
175// STATISTICS: kernel-branch