Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ jobs:
shell: bash
run: |
sudo apt-get update
sudo apt-get install -y flex bison libgmp-dev libmpfr-dev
sudo apt-get install -y flex bison libgmp-dev libmpfr-dev g++-14
# Space::clone needs deducing this, which GCC 13 lacks.
echo "CC=gcc-14" >> "$GITHUB_ENV"
echo "CXX=g++-14" >> "$GITHUB_ENV"

- name: Check generated source freshness
# Ubuntu LTS keeps Bison 3.8.2 and Flex 2.6.4, while Homebrew moves on.
Expand Down Expand Up @@ -446,6 +449,10 @@ jobs:

build-cmake-qt5-gist-smoke:
runs-on: ubuntu-24.04
env:
# Space::clone needs deducing this, which GCC 13 lacks.
CC: gcc-14
CXX: g++-14

steps:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
Expand All @@ -459,7 +466,7 @@ jobs:
shell: bash
run: |
sudo apt-get update
sudo apt-get install -y qtbase5-dev qtbase5-dev-tools libgl1-mesa-dev
sudo apt-get install -y qtbase5-dev qtbase5-dev-tools libgl1-mesa-dev g++-14

- name: Configure CMake with Qt 5/Gist
shell: bash
Expand Down
11 changes: 11 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ when that is clearer. Gecode builds as C++23.
Add abstractions only when they solve a concrete maintenance or correctness
problem.

Some downcasts cannot fail because a Gecode protocol fixes the dynamic type:
the space passed to `Space::constrain` and `Space::compare`, the choice passed
to `Brancher::commit`, and the advisor passed to `Propagator::advise`, among
others listed in the documentation of `down_cast` (`gecode/support/cast.hpp`).
Write such casts with `down_cast`, which asserts the type in debug builds.
Existing `static_cast`s at these sites are correct as well. Do not replace
either with `dynamic_cast` and an error path, and do not report them as
unchecked casts. Use `dynamic_cast` only where the type is genuinely unknown.
`Space::clone()` returns the static type of the cloned space, so cloning a
model needs no cast.

## Implementing propagators, branchers, and engines

Propagators:
Expand Down
25 changes: 25 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ Date: unreleased
This release contains breaking changes. See the entries below for the
changes downstream code needs.

[ENTRY]
Module: kernel
What: change
Rank: minor
[DESCRIPTION]
Space::clone returns the static type of the space it is called on, so cloning
a Model, or through a Model*, returns a Model* and needs no cast. Cloning through
a Space* still returns a Space*. Space::clone is now a member function template
with an explicit object parameter, so pointers to members to it no longer work.
The address of a specialization, such as the one for Space itself, is an
ordinary function pointer of type Space* (*)(const Space&). Gecode and code
including its headers need GCC 14, Clang 18, AppleClang 17, or MSVC 19.32 or
newer.

[ENTRY]
Module: support
What: new
Rank: minor
[DESCRIPTION]
Add down_cast for downcasts whose target type a Gecode protocol guarantees,
such as the space passed to Space::constrain and Space::compare, the choice
passed to Brancher::commit, and the advisor passed to Propagator::advise. It
asserts the type with dynamic_cast when assertions are enabled and is a
static_cast otherwise.

[ENTRY]
Module: flatzinc
What: change
Expand Down
8 changes: 5 additions & 3 deletions docs/cmake-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ For modelling and search, use MPG and the API reference linked from the
## Requirements

- CMake 3.21 or newer
- A compiler with C++23 mode. Use features supported across the GCC, Clang,
AppleClang, and MSVC builds; enabling the language mode does not imply that
every C++23 library feature is available.
- A compiler with C++23 mode and explicit object parameters (deducing this):
GCC 14, Clang 18, AppleClang 17, or MSVC 19.32 (Visual Studio 2022 17.2) or
newer. Use features supported across the GCC, Clang, AppleClang, and MSVC
builds; enabling the language mode does not imply that every C++23 library
feature is available.
- `uv` on `PATH` for generated-script paths: documentation generation,
`GECODE_REGENERATE_VARIMP=ON`, and manual helper execution. Ordinary CMake
builds can use the checked-in generated headers without `uv`.
Expand Down
4 changes: 2 additions & 2 deletions examples/bacp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class BACP : public IntMinimizeScript {
/// Value selection function for load branching
static int
load(const Space& home, IntVar x, int) {
const BACP& b = static_cast<const BACP&>(home);
const BACP& b = down_cast<const BACP&>(home);
IntVarValues values(x);
int val = -1;
int best = Int::Limits::max + 1;
Expand All @@ -192,7 +192,7 @@ class BACP : public IntMinimizeScript {
/// Value selection function for reverse load branching
static int
load_rev(const Space& home, IntVar x, int) {
const BACP& b = static_cast<const BACP&>(home);
const BACP& b = down_cast<const BACP&>(home);
IntVarValues values(x);
int val = -1;
int best = Int::Limits::min - 1;
Expand Down
4 changes: 2 additions & 2 deletions examples/bin-packing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ class CDBF : public Brancher {
/// Perform commit for choice \a _c and alternative \a a
ExecStatus
commit(Space& home, const Gecode::Choice& _c, unsigned int a) override {
const Choice& c = static_cast<const Choice&>(_c);
const Choice& c = down_cast<const Choice&>(_c);
// This catches also the case that the choice has a single aternative only
if (a == 0) {
GECODE_ME_CHECK(bin[c.item].eq(home, c.same[0]));
Expand All @@ -383,7 +383,7 @@ class CDBF : public Brancher {
/// Print explanation
void
print(const Space&, const Gecode::Choice& _c, unsigned int a, std::ostream& o) const override {
const Choice& c = static_cast<const Choice&>(_c);
const Choice& c = down_cast<const Choice&>(_c);
if (a == 0) {
o << "bin[" << c.item << "] = " << c.same[0];
} else {
Expand Down
4 changes: 2 additions & 2 deletions examples/car-sequencing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,15 @@ class CarSequencing : public Script {
/// Return cost
void
constrain(const Space& _best) override {
const CarSequencing& best = static_cast<const CarSequencing&>(_best);
const CarSequencing& best = down_cast<const CarSequencing&>(_best);
rel(*this, stall_count, IRT_LT, best.stall_count.val());
}

using Script::compare;
/// Compare objective values
SpaceComparison
compare(const Space& _other) const override {
const CarSequencing& other = static_cast<const CarSequencing&>(_other);
const CarSequencing& other = down_cast<const CarSequencing&>(_other);
return compare_int(stall_count.val(), other.stall_count.val(), Sense::Minimize);
}

Expand Down
4 changes: 2 additions & 2 deletions examples/cartesian-heart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class CartesianHeart : public Script {
/// Add constraints to current model to get next solution (not too close)
void
constrain(const Space& _b) override {
const CartesianHeart& b = static_cast<const CartesianHeart&>(_b);
const CartesianHeart& b = down_cast<const CartesianHeart&>(_b);
rel(*this,
(f[0] >= (b.f[0].max() + step)) || (f[1] >= (b.f[1].max() + step)) || (f[1] <= (b.f[1].min() - step)));
}
Expand All @@ -103,7 +103,7 @@ class CartesianHeart : public Script {
*/
SpaceComparison
compare(const Space& _other) const override {
const CartesianHeart& b = static_cast<const CartesianHeart&>(_other);
const CartesianHeart& b = down_cast<const CartesianHeart&>(_other);
bool apart = (f[0].min() >= (b.f[0].max() + step)) || (f[1].min() >= (b.f[1].max() + step)) ||
(f[1].max() <= (b.f[1].min() - step));
return apart ? SpaceComparison::Better : SpaceComparison::Worse;
Expand Down
2 changes: 1 addition & 1 deletion examples/crossword.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class Crossword : public Script {
/// Print brancher information when branching on letters
static void
print_letters(const Space& home, const Brancher&, unsigned int a, IntVar, int i, const int& n, std::ostream& os) {
const Crossword& c = static_cast<const Crossword&>(home);
const Crossword& c = down_cast<const Crossword&>(home);
int x = i % c.w, y = i / c.w;
os << "letters[" << x << "," << y << "] " << ((a == 0) ? "=" : "!=") << " " << static_cast<char>(n);
}
Expand Down
14 changes: 7 additions & 7 deletions examples/fifteen-puzzle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class OpenPuzzle : public Script {
: Script(opt), tiles(*this, IntSet(0, 15), 16 * (opt.limit() + 1)),
path(
*this, IntSet(0, 15),
[](Space& home, OpenIntVarList x, int i) { static_cast<OpenPuzzle&>(home).transition(x, i); },
[](Space& home, OpenIntVarList x, int i) { down_cast<OpenPuzzle&>(home).transition(x, i); },
opt.limit() + 1) {
const Board initial = initial_board(opt);
rel(*this, tiles.length() == 16 * path.length());
Expand All @@ -255,7 +255,7 @@ class OpenPuzzle : public Script {
IntValBranch value_branch = INT_VAL_MIN();
if (opt.branching() == PuzzleOptions::BRANCH_MANHATTAN)
value_branch = INT_VAL([](const Space& home, IntVar next, int i) {
const OpenPuzzle& s = static_cast<const OpenPuzzle&>(home);
const OpenPuzzle& s = down_cast<const OpenPuzzle&>(home);
return manhattan_value(s.state(i - 1), s.path[i - 1].val(), next);
});
branch(*this, path, INT_VAR_NONE(), value_branch);
Expand Down Expand Up @@ -301,7 +301,7 @@ class OpenPuzzle : public Script {
}
void
constrain(const Space& solution) override {
const OpenPuzzle& best = static_cast<const OpenPuzzle&>(solution);
const OpenPuzzle& best = down_cast<const OpenPuzzle&>(solution);
rel(*this, path.length() < best.path.length().val());
}
void
Expand Down Expand Up @@ -342,11 +342,11 @@ class FixedPuzzle : public Script {
IntValBranch value_branch = INT_VAL_MIN();
if (opt.branching() == PuzzleOptions::BRANCH_MANHATTAN)
value_branch = INT_VAL([](const Space& home, IntVar next, int i) {
const FixedPuzzle& s = static_cast<const FixedPuzzle&>(home);
const FixedPuzzle& s = down_cast<const FixedPuzzle&>(home);
return manhattan_value(s.state(i - 1), s.path[i - 1].val(), next);
});
branch(*this, path, INT_VAR_NONE(), value_branch, [](const Space& home, IntVar, int i) {
const FixedPuzzle& s = static_cast<const FixedPuzzle&>(home);
const FixedPuzzle& s = down_cast<const FixedPuzzle&>(home);
const int depth = s.depth();
return values(s.state(depth)) != goal() && i == depth + 1;
});
Expand Down Expand Up @@ -383,7 +383,7 @@ class FixedPuzzle : public Script {
}
void
constrain(const Space& solution) override {
const FixedPuzzle& best = static_cast<const FixedPuzzle&>(solution);
const FixedPuzzle& best = down_cast<const FixedPuzzle&>(solution);
rel(*this, length < best.length.val());
}
void
Expand All @@ -406,7 +406,7 @@ run(PuzzleOptions& opt) {
case PuzzleOptions::SEARCH_ASTAR:
case PuzzleOptions::SEARCH_CABS: {
Search::HeuristicGuidance guidance(
Sense::Minimize, [](const Space& home) -> std::int64_t { return static_cast<const Model&>(home).bound(); });
Sense::Minimize, [](const Space& home) -> std::int64_t { return down_cast<const Model&>(home).bound(); });
opt.guidance(&guidance);
if (opt.search() == PuzzleOptions::SEARCH_ASTAR)
Script::run<Model, AStar, PuzzleOptions>(opt);
Expand Down
10 changes: 5 additions & 5 deletions examples/job-shop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ class JobShopSolve : public JobShopBase {
/// Trampoline function for AFC-based cost
static double
afc_merit(const Space& home, BoolVar x, int i) {
return static_cast<const JobShopSolve&>(home).afc(x, i);
return down_cast<const JobShopSolve&>(home).afc(x, i);
}
/// Action information
IntAction iaction;
Expand All @@ -524,7 +524,7 @@ class JobShopSolve : public JobShopBase {
/// Trampoline function for Action-based cost
static double
action_merit(const Space& home, BoolVar, int i) {
return static_cast<const JobShopSolve&>(home).action(i);
return down_cast<const JobShopSolve&>(home).action(i);
}
/// CHB information
IntCHB ichb;
Expand All @@ -537,7 +537,7 @@ class JobShopSolve : public JobShopBase {
/// Trampoline function for CHB-based cost
static double
chb_merit(const Space& home, BoolVar, int i) {
return static_cast<const JobShopSolve&>(home).chb(i);
return down_cast<const JobShopSolve&>(home).chb(i);
}
/// Random number generator for probing and relaxation
Rnd rnd;
Expand Down Expand Up @@ -730,7 +730,7 @@ solve(const JobShopOptions& opt) {
std::cout << "\t\tBounds: [" << l << "," << u << "]" << '\n' << std::flush;

for (unsigned int p = 0; p < opt.probes(); p++) {
JobShopProbe* jsp = static_cast<JobShopProbe*>(root->clone());
JobShopProbe* jsp = root->clone();
jsp->branch(p, rnd.split(p));
DFS<JobShopProbe> dfs(jsp, so);
JobShopProbe* s = dfs.next();
Expand Down Expand Up @@ -783,7 +783,7 @@ solve(const JobShopOptions& opt) {
bool stopped = false;
while (l < u) {
std::cout << "\t\tBounds: [" << l << "," << u << "]" << '\n' << std::flush;
JobShopSolve* jss = static_cast<JobShopSolve*>(model->clone());
JobShopSolve* jss = model->clone();
int m = (l + u) / 2;
rel(*jss, jss->cost() >= l);
rel(*jss, jss->cost() <= m);
Expand Down
6 changes: 3 additions & 3 deletions examples/knights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ class Warnsdorff : public Brancher {
/// Perform commit for choice \a _c and alternative \a a
ExecStatus
commit(Space& home, const Gecode::Choice& _c, unsigned int a) override {
const Choice& c = static_cast<const Choice&>(_c);
const Choice& c = down_cast<const Choice&>(_c);
if (a == 0)
return me_failed(x[c.pos].eq(home, c.val)) ? ES_FAILED : ES_OK;
return me_failed(x[c.pos].nq(home, c.val)) ? ES_FAILED : ES_OK;
}
/// Print explanation
void
print(const Space&, const Gecode::Choice& _c, unsigned int a, std::ostream& o) const override {
const Choice& c = static_cast<const Choice&>(_c);
const Choice& c = down_cast<const Choice&>(_c);
o << "x[" << c.pos << "] " << ((a == 0) ? "=" : "!=") << " " << c.val;
}
/// Copy brancher
Expand Down Expand Up @@ -345,7 +345,7 @@ class KnightsInspector : public Gist::Inspector {
/// Inspect space \a s
void
inspect(const Space& s) override {
const Knights& k = static_cast<const Knights&>(s);
const Knights& k = down_cast<const Knights&>(s);
const int half_unit = unit / 2;

if (!scene)
Expand Down
2 changes: 1 addition & 1 deletion examples/photo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Photo : public IntMinimizeScript {
void
spoke(SpokeContext& sc) override {
if ((sc.engine() == MetaEngine::Restart) && (sc.last() != nullptr) && (p > 0.0)) {
const Photo& l = static_cast<const Photo&>(*sc.last());
const Photo& l = down_cast<const Photo&>(*sc.last());
relax(*this, pos, l.pos, rnd.split(sc), p);
sc.coverage(SpokeCoverage::Partial);
}
Expand Down
10 changes: 5 additions & 5 deletions examples/queen-armies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class QueenArmies : public IntMaximizeScript {
/// Check status of brancher, return true if alternatives left.
bool
status(const Space& home) const override {
const QueenArmies& q = static_cast<const QueenArmies&>(home);
const QueenArmies& q = down_cast<const QueenArmies&>(home);
for (int i = start; i < q.n * q.n; ++i)
if (!q.white_matrix[i].assigned()) {
start = i;
Expand All @@ -197,7 +197,7 @@ class QueenArmies : public IntMaximizeScript {
/// Return choice
Gecode::Choice*
choice(Space& home) override {
const QueenArmies& q = static_cast<const QueenArmies&>(home);
const QueenArmies& q = down_cast<const QueenArmies&>(home);
int maxsize = -1;
int pos = -1;

Expand Down Expand Up @@ -229,15 +229,15 @@ class QueenArmies : public IntMaximizeScript {
*/
ExecStatus
commit(Space& home, const Gecode::Choice& _c, unsigned int a) override {
QueenArmies& q = static_cast<QueenArmies&>(home);
const Choice& c = static_cast<const Choice&>(_c);
QueenArmies& q = down_cast<QueenArmies&>(home);
const Choice& c = down_cast<const Choice&>(_c);
bool val = (a == 0) ? c.val : !c.val;
return me_failed(Int::BoolView(q.white_matrix[c.pos]).eq(q, val)) ? ES_FAILED : ES_OK;
}
/// Print explanation
void
print(const Space&, const Gecode::Choice& _c, unsigned int a, std::ostream& o) const override {
const Choice& c = static_cast<const Choice&>(_c);
const Choice& c = down_cast<const Choice&>(_c);
bool val = (a == 0) ? c.val : !c.val;
o << "w[" << c.pos << "] = " << val;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/queens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class QueensInspector : public Gist::Inspector {
/// Inspect space \a s
void
inspect(const Space& s) override {
const Queens& q = static_cast<const Queens&>(s);
const Queens& q = down_cast<const Queens&>(s);
const int inset = unit / 4;
const int diameter = unit / 2;

Expand Down
Loading
Loading