Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error message because of macro #126

Merged
merged 7 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 10 additions & 8 deletions src/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Initializes the Optimizer library. This must be called before any other library

"""
function init()
@checked Lib.XPRSinit(C_NULL)
Lib.XPRSinit(C_NULL)
kdheepak marked this conversation as resolved.
Show resolved Hide resolved
end

"""
Expand All @@ -83,7 +83,7 @@ Frees any allocated memory and closes all open files.

"""
function free()
@checked Lib.XPRSfree()
Lib.XPRSfree()
end

"""
Expand Down Expand Up @@ -125,7 +125,7 @@ You can use this function to disable some of the checking and validation of func

"""
function setcheckedmode(checked_mode)
@checked Lib.XPRSsetcheckedmode(checked_mode)
Lib.XPRSsetcheckedmode(checked_mode)
end

"""
Expand All @@ -135,19 +135,19 @@ You can use this function to interrogate whether checking and validation of all

"""
function getcheckedmode(r_checked_mode)
@checked Lib.XPRSgetcheckedmode(r_checked_mode)
Lib.XPRSgetcheckedmode(r_checked_mode)
end

function license(lic, path)
r = Lib.XPRSlicense(lic, path)
end

function beginlicensing(r_dontAlreadyHaveLicense)
@checked Lib.XPRSbeginlicensing(r_dontAlreadyHaveLicense)
Lib.XPRSbeginlicensing(r_dontAlreadyHaveLicense)
end

function endlicensing()
@checked Lib.XPRSendlicensing()
Lib.XPRSendlicensing()
end

"""
Expand All @@ -165,7 +165,7 @@ function getlicerrmsg(; len = 1024)
msg = Cstring(pointer(Array{Cchar}(undef, len*8)))
Lib.XPRSgetlicerrmsg(msg, len)
# TODO - @ checked version does not work
# @checked Lib.XPRSgetlicerrmsg(msg, len)
# Lib.XPRSgetlicerrmsg(msg, len)
return unsafe_string(msg)
end

Expand Down Expand Up @@ -2787,7 +2787,9 @@ Returns the error message corresponding to the last error encountered by a libra

"""
function getlasterror(prob::XpressProblem)
@invoke Lib.XPRSgetlasterror(prob, _)::String
out = Cstring(pointer(Array{Cchar}(undef, 512)))
s = Lib.XPRSgetlasterror(prob, out)
s == 0 ? unsafe_string(out) : "Unable to get last error"
end
kdheepak marked this conversation as resolved.
Show resolved Hide resolved

"""
Expand Down
4 changes: 4 additions & 0 deletions src/helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ mutable struct XpressProblem <: CWrapper
end
end

function get_xpress_error_message(prob::XpressProblem)
e = lstrip(Xpress.getlasterror(prob), ['?'])
end

function XpressProblem(; logfile = "")
ref = Ref{Lib.XPRSprob}()
createprob(ref)
Expand Down
7 changes: 6 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,20 @@ macro invoke(expr)
return f
end

function get_xpress_error_message(xprs_ptr)
e = "(Unable to extract error message for $(typeof(xprs_ptr)).)"
end

macro checked(expr)
@assert expr.head == :call "Can only use @checked on function calls"
@assert ( expr.args[1].head == :(.) ) && ( expr.args[1].args[1] == :Lib) "Can only use @checked on Lib.\$function"
@assert length(expr.args) >= 2 "Lib.\$function must be contain atleast one argument and the first argument must be of type XpressProblem"
f = replace(String(expr.args[1].args[2].value), "XPRS" => "")
return esc(quote
r = $(expr)
if r != 0
e = lstrip(Xpress.getlasterror(prob), ['?'])
xprs_ptr = $(expr.args[2])
e = get_xpress_error_message(xprs_ptr)
throw(XpressError(r, "Unable to call `Xpress.$($f)`:\n\n$e.\n"))
else
nothing
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ end
@test Xpress.getcontrol(prob, "HEURTHREADS") == 0
@test Xpress.getcontrol(prob, :HEURTHREADS) == 0

@test_throws Xpress.XpressError Xpress.copyprob(prob, prob)
end