Skip to content

Commit

Permalink
add simple @sc macro for SCIP_CALL
Browse files Browse the repository at this point in the history
  • Loading branch information
rschwarz committed Dec 20, 2018
1 parent 49bbe94 commit 42bed2d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
30 changes: 10 additions & 20 deletions src/managed_scip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ mutable struct ManagedSCIP

function ManagedSCIP()
scip = Ref{Ptr{SCIP_}}()
rc = SCIPcreate(scip)
@assert rc == SCIP_OKAY
@SC SCIPcreate(scip)
@assert scip[] != C_NULL
rc = SCIPincludeDefaultPlugins(scip[])
@assert rc == SCIP_OKAY
rc = SCIP.SCIPcreateProbBasic(scip[], "")
@assert rc == SCIP_OKAY
@SC SCIPincludeDefaultPlugins(scip[])
@SC SCIP.SCIPcreateProbBasic(scip[], "")

mscip = new(scip, [], [])
finalizer(free_scip, mscip)
Expand All @@ -24,28 +21,23 @@ function free_scip(mscip::ManagedSCIP)
# avoid double-free
if mscip.scip[] != C_NULL
for cons in mscip.conss
rc = SCIPreleaseCons(mscip.scip[], cons)
@assert rc == SCIP_OKAY
@SC SCIPreleaseCons(mscip.scip[], cons)
end
for var in mscip.vars
rc = SCIPreleaseVar(mscip.scip[], var)
@assert rc == SCIP_OKAY
@SC SCIPreleaseVar(mscip.scip[], var)
end
rc = SCIPfree(mscip.scip)
@assert rc == SCIP_OKAY
@SC SCIPfree(mscip.scip)
end
@assert mscip.scip[] == C_NULL
end

"Add variable to problem (continuous, no bounds)"
function add_variable(mscip::ManagedSCIP)
var = Ref{Ptr{SCIP_VAR}}()
rc = SCIPcreateVarBasic(
@SC rc = SCIPcreateVarBasic(
mscip.scip[], var, "", -SCIPinfinity(mscip.scip[]),
SCIPinfinity(mscip.scip[]), 0.0, SCIP_VARTYPE_CONTINUOUS)
@assert rc == SCIP_OKAY
rc = SCIPaddVar(mscip.scip[], var[])
@assert rc == SCIP_OKAY
@SC rc = SCIPaddVar(mscip.scip[], var[])

push!(mscip.vars, var)
# can't delete variable, so we use the array position as index
Expand All @@ -57,11 +49,9 @@ function add_linear_constraint(mscip::ManagedSCIP, varidx, coeffs, lhs, rhs)
@assert length(varidx) == length(coeffs)
vars = [mscip.vars[i][] for i in varidx]
cons = Ref{Ptr{SCIP_CONS}}()
rc = SCIPcreateConsBasicLinear(
@SC rc = SCIPcreateConsBasicLinear(
mscip.scip[], cons, "", length(vars), vars, coeffs, lhs, rhs)
@assert rc == SCIP_OKAY
rc = SCIPaddCons(mscip.scip[], cons[])
@assert rc == SCIP_OKAY
@SC rc = SCIPaddCons(mscip.scip[], cons[])

push!(mscip.conss, cons)
# can't delete constraint, so we use the array position as index
Expand Down
5 changes: 5 additions & 0 deletions src/wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,8 @@ include(wrap("cons_superindicator"))
include(wrap("cons_symresack"))
include(wrap("cons_varbound"))
include(wrap("cons_xor"))

# SCIP_CALL: macro to check return codes, inspired by @assert
macro SC(ex)
return :(@assert $(esc(ex)) == SCIP_OKAY)
end
7 changes: 7 additions & 0 deletions test/direct_library_calls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@
rc = SCIP.SCIPfree(scip__)
@test rc == SCIP.SCIP_OKAY
end

@testset "SCIP_CALL macro (@SC)" begin
# should do nothing
@SCIP.SC SCIP.SCIP_OKAY

@test_throws AssertionError @SCIP.SC SCIP.SCIP_ERROR
end

0 comments on commit 42bed2d

Please sign in to comment.