CP Solver: porting fail interception to std::functions.

This commit is contained in:
Vincent Furnon
2015-05-04 17:30:16 +02:00
parent 7c2675df89
commit 8b3976703f
5 changed files with 9 additions and 13 deletions

View File

@@ -2436,7 +2436,7 @@ bool Solver::SolveAndCommit(DecisionBuilder* const db,
void Solver::Fail() {
if (fail_intercept_) {
fail_intercept_->Run();
fail_intercept_();
return;
}
ConstraintSolverFailsHere();

View File

@@ -61,6 +61,7 @@
#ifndef OR_TOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVER_H_
#define OR_TOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVER_H_
#include <functional>
#include "base/hash.h"
#include "base/hash.h"
#include <iosfwd>
@@ -2844,8 +2845,9 @@ class Solver {
Decision* balancing_decision() const { return balancing_decision_.get(); }
// Internal
// set_fail_intercept does not take ownership of the closure.
void set_fail_intercept(Closure* const c) { fail_intercept_ = c; }
void set_fail_intercept(std::function<void()> fail_intercept) {
fail_intercept_ = fail_intercept;
}
void clear_fail_intercept() { fail_intercept_ = nullptr; }
// Access to demon profiler.
DemonProfiler* demon_profiler() const { return demon_profiler_; }
@@ -3054,7 +3056,7 @@ class Solver {
uint64 fail_stamp_;
std::unique_ptr<Decision> balancing_decision_;
// intercept failures
Closure* fail_intercept_;
std::function<void()> fail_intercept_;
// Demon monitor
DemonProfiler* const demon_profiler_;

View File

@@ -83,12 +83,10 @@ struct FailureProtect {
%exception Method {
operations_research::Solver* const solver = GetSolver;
FailureProtect protect;
Closure* const intercept = NewCallback<>(&protect, &FailureProtect::JumpBack);
solver->set_fail_intercept(intercept);
solver->set_fail_intercept([&protect]() { protect.JumpBack(); });
if (setjmp(protect.exception_buffer) == 0) {
$action
solver->clear_fail_intercept();
delete intercept;
} else {
solver->clear_fail_intercept();
SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, "fail");

View File

@@ -27,12 +27,10 @@
%exception Method {
operations_research::Solver* const solver = GetSolver;
FailureProtect protect;
Closure* const intercept = NewCallback<>(&protect, &FailureProtect::JumpBack);
solver->set_fail_intercept(intercept);
solver->set_fail_intercept([&protect]() { protect.JumpBack(); });
if (setjmp(protect.exception_buffer) == 0) {
$action
solver->clear_fail_intercept();
delete intercept;
} else {
solver->clear_fail_intercept();
jclass fail_class = jenv->FindClass(

View File

@@ -787,12 +787,10 @@ PY_STRINGIFY_DEBUGSTRING(Decision);
%exception Method {
operations_research::Solver* const solver = GetSolver;
FailureProtect protect;
Closure* const intercept = NewCallback<>(&protect, &FailureProtect::JumpBack);
solver->set_fail_intercept(intercept);
solver->set_fail_intercept([&protect]() { protect.JumpBack(); });
if (setjmp(protect.exception_buffer) == 0) {
$action
solver->clear_fail_intercept();
delete intercept;
} else {
solver->clear_fail_intercept();
// IMPORTANT: the type and message of the exception raised matter,