Skip to content

Commit 8105da9

Browse files
committed
var() method for event
1 parent b74d1be commit 8105da9

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/eventhdlr.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use crate::{ffi, Model, Solving};
1+
use crate::scip::ScipPtr;
2+
use crate::{ffi, Model, Solving, Variable};
3+
use scip_sys::SCIPeventGetVar;
24
use std::ops::{BitOr, BitOrAssign};
5+
use std::rc::Rc;
36

47
/// Trait used to define custom event handlers.
58
pub trait Eventhdlr {
@@ -201,6 +204,7 @@ impl SCIPEventhdlr {
201204
/// Wrapper for the internal SCIP event.
202205
pub struct Event {
203206
pub(crate) raw: *mut ffi::SCIP_EVENT,
207+
pub(crate) scip: Rc<ScipPtr>,
204208
}
205209

206210
impl Event {
@@ -214,6 +218,23 @@ impl Event {
214218
let event_type = unsafe { ffi::SCIPeventGetType(self.raw) };
215219
EventMask(event_type)
216220
}
221+
222+
/// Returns the associated variable for a variable event
223+
pub fn var(&self) -> Option<Variable> {
224+
if self
225+
.event_type()
226+
.matches(EventMask::VAR_EVENT | EventMask::VAR_FIXED | EventMask::VAR_DELETED)
227+
{
228+
let var_ptr = unsafe { SCIPeventGetVar(self.raw) };
229+
assert!(!var_ptr.is_null());
230+
Some(Variable {
231+
raw: var_ptr,
232+
scip: self.scip.clone(),
233+
})
234+
} else {
235+
None
236+
}
237+
}
217238
}
218239

219240
#[cfg(test)]
@@ -278,6 +299,7 @@ mod tests {
278299
) {
279300
assert!(self.get_type().matches(event.event_type()));
280301
assert_eq!(eventhdlr.name(), "InternalSCIPEventHdlrTester");
302+
assert!(event.var().is_none())
281303
}
282304
}
283305

src/scip.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -640,13 +640,16 @@ impl ScipPtr {
640640
let data_ptr = unsafe { ffi::SCIPeventhdlrGetData(eventhdlr) };
641641
assert!(!data_ptr.is_null());
642642
let eventhdlr_ptr = data_ptr as *mut Box<dyn Eventhdlr>;
643-
let scip_ptr = ScipPtr::from_raw(scip, true);
643+
let scip_ptr = Rc::new(ScipPtr::from_raw(scip, true));
644644
let model = Model {
645-
scip: Rc::new(scip_ptr),
645+
scip: scip_ptr.clone(),
646646
state: Solving,
647647
};
648648
let eventhdlr = SCIPEventhdlr { raw: eventhdlr };
649-
let event = Event { raw: event };
649+
let event = Event {
650+
raw: event,
651+
scip: scip_ptr.clone(),
652+
};
650653
unsafe { (*eventhdlr_ptr).execute(model, eventhdlr, event) };
651654
Retcode::Okay.into()
652655
}

0 commit comments

Comments
 (0)