1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 2 /* */ 3 /* This file is part of the program and library */ 4 /* SCIP --- Solving Constraint Integer Programs */ 5 /* */ 6 /* Copyright (c) 2002-2023 Zuse Institute Berlin (ZIB) */ 7 /* */ 8 /* Licensed under the Apache License, Version 2.0 (the "License"); */ 9 /* you may not use this file except in compliance with the License. */ 10 /* You may obtain a copy of the License at */ 11 /* */ 12 /* http://www.apache.org/licenses/LICENSE-2.0 */ 13 /* */ 14 /* Unless required by applicable law or agreed to in writing, software */ 15 /* distributed under the License is distributed on an "AS IS" BASIS, */ 16 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ 17 /* See the License for the specific language governing permissions and */ 18 /* limitations under the License. */ 19 /* */ 20 /* You should have received a copy of the Apache-2.0 license */ 21 /* along with SCIP; see the file LICENSE. If not visit scipopt.org. */ 22 /* */ 23 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 24 25 /**@file objeventhdlr.cpp 26 * @brief C++ wrapper for event handlers 27 * @author Tobias Achterberg 28 */ 29 30 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/ 31 32 #include <cassert> 33 34 #include "objeventhdlr.h" 35 36 37 38 39 /* 40 * Data structures 41 */ 42 43 /** event handler data */ 44 struct SCIP_EventhdlrData 45 { 46 scip::ObjEventhdlr* objeventhdlr; /**< event handler object */ 47 SCIP_Bool deleteobject; /**< should the event handler object be deleted when eventhdlr is freed? */ 48 }; 49 50 51 52 53 /* 54 * Callback methods of event handler 55 */ 56 57 extern "C" 58 { 59 60 /** copy method for event handler plugins (called when SCIP copies plugins) */ 61 static 62 SCIP_DECL_EVENTCOPY(eventhdlrCopyObj) 63 { /*lint --e{715}*/ 64 SCIP_EVENTHDLRDATA* eventhdlrdata; 65 66 assert(scip != NULL); 67 68 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr); 69 assert(eventhdlrdata != NULL); 70 assert(eventhdlrdata->objeventhdlr != NULL); 71 assert(eventhdlrdata->objeventhdlr->scip_ != scip); 72 73 if( eventhdlrdata->objeventhdlr->iscloneable() ) 74 { 75 scip::ObjEventhdlr* newobjeventhdlr; 76 newobjeventhdlr = dynamic_cast<scip::ObjEventhdlr*> (eventhdlrdata->objeventhdlr->clone(scip)); 77 78 /* call include method of event handler object */ 79 SCIP_CALL( SCIPincludeObjEventhdlr(scip, newobjeventhdlr, TRUE) ); 80 } 81 82 return SCIP_OKAY; 83 } 84 85 /** destructor of event handler to free user data (called when SCIP is exiting) */ 86 static 87 SCIP_DECL_EVENTFREE(eventhdlrFreeObj) 88 { /*lint --e{715}*/ 89 SCIP_EVENTHDLRDATA* eventhdlrdata; 90 91 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr); 92 assert(eventhdlrdata != NULL); 93 assert(eventhdlrdata->objeventhdlr != NULL); 94 assert(eventhdlrdata->objeventhdlr->scip_ == scip); 95 96 /* call virtual method of eventhdlr object */ 97 SCIP_CALL( eventhdlrdata->objeventhdlr->scip_free(scip, eventhdlr) ); 98 99 /* free eventhdlr object */ 100 if( eventhdlrdata->deleteobject ) 101 delete eventhdlrdata->objeventhdlr; 102 103 /* free eventhdlr data */ 104 delete eventhdlrdata; 105 SCIPeventhdlrSetData(eventhdlr, NULL); /*lint !e64*/ 106 107 return SCIP_OKAY; 108 } 109 110 111 /** initialization method of event handler (called after problem was transformed) */ 112 static 113 SCIP_DECL_EVENTINIT(eventhdlrInitObj) 114 { /*lint --e{715}*/ 115 SCIP_EVENTHDLRDATA* eventhdlrdata; 116 117 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr); 118 assert(eventhdlrdata != NULL); 119 assert(eventhdlrdata->objeventhdlr != NULL); 120 assert(eventhdlrdata->objeventhdlr->scip_ == scip); 121 122 /* call virtual method of eventhdlr object */ 123 SCIP_CALL( eventhdlrdata->objeventhdlr->scip_init(scip, eventhdlr) ); 124 125 return SCIP_OKAY; 126 } 127 128 129 /** deinitialization method of event handler (called before transformed problem is freed) */ 130 static 131 SCIP_DECL_EVENTEXIT(eventhdlrExitObj) 132 { /*lint --e{715}*/ 133 SCIP_EVENTHDLRDATA* eventhdlrdata; 134 135 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr); 136 assert(eventhdlrdata != NULL); 137 assert(eventhdlrdata->objeventhdlr != NULL); 138 139 /* call virtual method of eventhdlr object */ 140 SCIP_CALL( eventhdlrdata->objeventhdlr->scip_exit(scip, eventhdlr) ); 141 142 return SCIP_OKAY; 143 } 144 145 146 /** solving process initialization method of event handler (called when branch and bound process is about to begin) */ 147 static 148 SCIP_DECL_EVENTINITSOL(eventhdlrInitsolObj) 149 { /*lint --e{715}*/ 150 SCIP_EVENTHDLRDATA* eventhdlrdata; 151 152 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr); 153 assert(eventhdlrdata != NULL); 154 assert(eventhdlrdata->objeventhdlr != NULL); 155 156 /* call virtual method of eventhdlr object */ 157 SCIP_CALL( eventhdlrdata->objeventhdlr->scip_initsol(scip, eventhdlr) ); 158 159 return SCIP_OKAY; 160 } 161 162 163 /** solving process deinitialization method of event handler (called before branch and bound process data is freed) */ 164 static 165 SCIP_DECL_EVENTEXITSOL(eventhdlrExitsolObj) 166 { /*lint --e{715}*/ 167 SCIP_EVENTHDLRDATA* eventhdlrdata; 168 169 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr); 170 assert(eventhdlrdata != NULL); 171 assert(eventhdlrdata->objeventhdlr != NULL); 172 173 /* call virtual method of eventhdlr object */ 174 SCIP_CALL( eventhdlrdata->objeventhdlr->scip_exitsol(scip, eventhdlr) ); 175 176 return SCIP_OKAY; 177 } 178 179 180 /** frees specific constraint data */ 181 static 182 SCIP_DECL_EVENTDELETE(eventhdlrDeleteObj) 183 { /*lint --e{715}*/ 184 SCIP_EVENTHDLRDATA* eventhdlrdata; 185 186 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr); 187 assert(eventhdlrdata != NULL); 188 assert(eventhdlrdata->objeventhdlr != NULL); 189 190 /* call virtual method of eventhdlr object */ 191 SCIP_CALL( eventhdlrdata->objeventhdlr->scip_delete(scip, eventhdlr, eventdata) ); 192 193 return SCIP_OKAY; 194 } 195 196 197 /** execution method of event handler */ 198 static 199 SCIP_DECL_EVENTEXEC(eventhdlrExecObj) 200 { /*lint --e{715}*/ 201 SCIP_EVENTHDLRDATA* eventhdlrdata; 202 203 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr); 204 assert(eventhdlrdata != NULL); 205 assert(eventhdlrdata->objeventhdlr != NULL); 206 207 /* call virtual method of eventhdlr object */ 208 SCIP_CALL( eventhdlrdata->objeventhdlr->scip_exec(scip, eventhdlr, event, eventdata) ); 209 210 return SCIP_OKAY; 211 } 212 } 213 214 215 216 /* 217 * event handler specific interface methods 218 */ 219 220 /** creates the event handler for the given event handler object and includes it in SCIP */ 221 SCIP_RETCODE SCIPincludeObjEventhdlr( 222 SCIP* scip, /**< SCIP data structure */ 223 scip::ObjEventhdlr* objeventhdlr, /**< event handler object */ 224 SCIP_Bool deleteobject /**< should the event handler object be deleted when eventhdlr is freed? */ 225 ) 226 { 227 SCIP_EVENTHDLRDATA* eventhdlrdata; 228 229 assert(scip != NULL); 230 assert(objeventhdlr != NULL); 231 232 /* create event handler data */ 233 eventhdlrdata = new SCIP_EVENTHDLRDATA; 234 eventhdlrdata->objeventhdlr = objeventhdlr; 235 eventhdlrdata->deleteobject = deleteobject; 236 237 /* include event handler */ 238 SCIP_CALL( SCIPincludeEventhdlr(scip, objeventhdlr->scip_name_, objeventhdlr->scip_desc_, 239 eventhdlrCopyObj, 240 eventhdlrFreeObj, eventhdlrInitObj, eventhdlrExitObj, 241 eventhdlrInitsolObj, eventhdlrExitsolObj, eventhdlrDeleteObj, eventhdlrExecObj, 242 eventhdlrdata) ); /*lint !e429*/ 243 244 return SCIP_OKAY; /*lint !e429*/ 245 } 246 247 /** returns the eventhdlr object of the given name, or 0 if not existing */ 248 scip::ObjEventhdlr* SCIPfindObjEventhdlr( 249 SCIP* scip, /**< SCIP data structure */ 250 const char* name /**< name of event handler */ 251 ) 252 { 253 SCIP_EVENTHDLR* eventhdlr; 254 SCIP_EVENTHDLRDATA* eventhdlrdata; 255 256 eventhdlr = SCIPfindEventhdlr(scip, name); 257 if( eventhdlr == NULL ) 258 return 0; 259 260 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr); 261 assert(eventhdlrdata != NULL); 262 263 return eventhdlrdata->objeventhdlr; 264 } 265 266 /** returns the eventhdlr object for the given event handler */ 267 scip::ObjEventhdlr* SCIPgetObjEventhdlr( 268 SCIP* scip, /**< SCIP data structure */ 269 SCIP_EVENTHDLR* eventhdlr /**< event handler */ 270 ) 271 { 272 SCIP_EVENTHDLRDATA* eventhdlrdata; 273 274 assert(scip != NULL); 275 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr); 276 assert(eventhdlrdata != NULL); 277 278 return eventhdlrdata->objeventhdlr; 279 } 280