Scippy

SCIP

Solving Constraint Integer Programs

objeventhdlr.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright 2002-2022 Zuse Institute Berlin */
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.h
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 #ifndef __SCIP_OBJEVENTHDLR_H__
33 #define __SCIP_OBJEVENTHDLR_H__
34 
35 #include <cstring>
36 #include <utility>
37 
38 #include "scip/scip.h"
39 #include "objscip/objcloneable.h"
40 
41 namespace scip
42 {
43 
44 /** @brief C++ wrapper for event handlers
45  *
46  * This class defines the interface for eventt handlers implemented in C++. Note that there is a pure virtual function
47  * (this function has to be implemented). This function is: scip_exec().
48  *
49  * - \ref EVENT "Instructions for implementing an event handler"
50  * - \ref type_event.h "Corresponding C interface"
51  */
52 class ObjEventhdlr : public ObjCloneable
53 {
54 public:
55  /*lint --e{1540}*/
56 
57  /** SCIP data structure */
59 
60  /** name of the event handler */
61  char* scip_name_;
62 
63  /** description of the event handler */
64  char* scip_desc_;
65 
66  /** default constructor */
68  SCIP* scip, /**< SCIP data structure */
69  const char* name, /**< name of event handler */
70  const char* desc /**< description of event handler */
71  )
72  : scip_(scip),
73  scip_name_(0),
74  scip_desc_(0)
75  {
76  /* the macro SCIPduplicateMemoryArray does not need the first argument: */
77  SCIP_CALL_ABORT( SCIPduplicateMemoryArray(scip_, &scip_name_, name, std::strlen(name)+1) );
78  SCIP_CALL_ABORT( SCIPduplicateMemoryArray(scip_, &scip_desc_, desc, std::strlen(desc)+1) );
79  }
80 
81  /** copy constructor */
82  ObjEventhdlr(const ObjEventhdlr& o) : ObjEventhdlr(o.scip_, o.scip_name_, o.scip_desc_) {}
83 
84  /** move constructor */
85  ObjEventhdlr(ObjEventhdlr&& o) : scip_(o.scip_), scip_name_(0), scip_desc_(0)
86  {
87  std::swap(scip_name_, o.scip_name_);
88  std::swap(scip_desc_, o.scip_desc_);
89  }
90 
91  /** destructor */
92  virtual ~ObjEventhdlr()
93  {
94  /* the macro SCIPfreeMemoryArray does not need the first argument: */
95  /*lint --e{64}*/
96  SCIPfreeMemoryArray(scip_, &scip_name_);
97  SCIPfreeMemoryArray(scip_, &scip_desc_);
98  }
99 
100  /** assignment of polymorphic classes causes slicing and is therefore disabled. */
101  ObjEventhdlr& operator=(const ObjEventhdlr& o) = delete;
102 
103  /** assignment of polymorphic classes causes slicing and is therefore disabled. */
104  ObjEventhdlr& operator=(ObjEventhdlr&& o) = delete;
105 
106  /** destructor of event handler to free user data (called when SCIP is exiting)
107  *
108  * @see SCIP_DECL_EVENTFREE(x) in @ref type_event.h
109  */
110  virtual SCIP_DECL_EVENTFREE(scip_free)
111  { /*lint --e{715}*/
112  return SCIP_OKAY;
113  }
114 
115  /** initialization method of event handler (called after problem was transformed)
116  *
117  * @see SCIP_DECL_EVENTINIT(x) in @ref type_event.h
118  */
119  virtual SCIP_DECL_EVENTINIT(scip_init)
120  { /*lint --e{715}*/
121  return SCIP_OKAY;
122  }
123 
124  /** deinitialization method of event handler (called before transformed problem is freed)
125  *
126  * @see SCIP_DECL_EVENTEXIT(x) in @ref type_event.h
127  */
128  virtual SCIP_DECL_EVENTEXIT(scip_exit)
129  { /*lint --e{715}*/
130  return SCIP_OKAY;
131  }
132 
133  /** solving process initialization method of event handler (called when branch and bound process is about to begin)
134  *
135  * @see SCIP_DECL_EVENTINITSOL(x) in @ref type_event.h
136  */
137  virtual SCIP_DECL_EVENTINITSOL(scip_initsol)
138  { /*lint --e{715}*/
139  return SCIP_OKAY;
140  }
141 
142  /** solving process deinitialization method of event handler (called before branch and bound process data is freed)
143  *
144  * @see SCIP_DECL_EVENTEXITSOL(x) in @ref type_event.h
145  */
146  virtual SCIP_DECL_EVENTEXITSOL(scip_exitsol)
147  { /*lint --e{715}*/
148  return SCIP_OKAY;
149  }
150 
151  /** frees specific constraint data
152  *
153  * @see SCIP_DECL_EVENTDELETE(x) in @ref type_event.h
154  */
155  virtual SCIP_DECL_EVENTDELETE(scip_delete)
156  { /*lint --e{715}*/
157  return SCIP_OKAY;
158  }
159 
160  /** execution method of event handler
161  *
162  * @see SCIP_DECL_EVENTEXEC(x) in @ref type_event.h
163  */
164  virtual SCIP_DECL_EVENTEXEC(scip_exec) = 0;
165 };
166 
167 } /* namespace scip */
168 
169 
170 
171 /** creates the event handler for the given event handler object and includes it in SCIP
172  *
173  * The method should be called in one of the following ways:
174  *
175  * 1. The user is resposible of deleting the object:
176  * SCIP_CALL( SCIPcreate(&scip) );
177  * ...
178  * MyEventhdlr* myeventhdlr = new MyEventhdlr(...);
179  * SCIP_CALL( SCIPincludeObjEventhdlr(scip, &myeventhdlr, FALSE) );
180  * ...
181  * SCIP_CALL( SCIPfree(&scip) );
182  * delete myeventhdlr; // delete eventhdlr AFTER SCIPfree() !
183  *
184  * 2. The object pointer is passed to SCIP and deleted by SCIP in the SCIPfree() call:
185  * SCIP_CALL( SCIPcreate(&scip) );
186  * ...
187  * SCIP_CALL( SCIPincludeObjEventhdlr(scip, new MyEventhdlr(...), TRUE) );
188  * ...
189  * SCIP_CALL( SCIPfree(&scip) ); // destructor of MyEventhdlr is called here
190  */
191 SCIP_EXPORT
193  SCIP* scip, /**< SCIP data structure */
194  scip::ObjEventhdlr* objeventhdlr, /**< event handler object */
195  SCIP_Bool deleteobject /**< should the event handler object be deleted when eventhdlristic is freed? */
196  );
197 
198 /** returns the eventhdlr object of the given name, or 0 if not existing */
199 SCIP_EXPORT
201  SCIP* scip, /**< SCIP data structure */
202  const char* name /**< name of event handler */
203  );
204 
205 /** returns the eventhdlr object for the given event handler */
206 SCIP_EXPORT
208  SCIP* scip, /**< SCIP data structure */
209  SCIP_EVENTHDLR* eventhdlr /**< event handler */
210  );
211 
212 #endif
virtual SCIP_DECL_EVENTEXIT(scip_exit)
Definition: objeventhdlr.h:128
virtual SCIP_DECL_EVENTINITSOL(scip_initsol)
Definition: objeventhdlr.h:137
scip::ObjEventhdlr * SCIPgetObjEventhdlr(SCIP *scip, SCIP_EVENTHDLR *eventhdlr)
ObjEventhdlr(ObjEventhdlr &&o)
Definition: objeventhdlr.h:85
#define SCIPduplicateMemoryArray(scip, ptr, source, num)
Definition: scip_mem.h:76
#define SCIPfreeMemoryArray(scip, ptr)
Definition: scip_mem.h:80
virtual SCIP_DECL_EVENTDELETE(scip_delete)
Definition: objeventhdlr.h:155
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:63
scip::ObjEventhdlr * SCIPfindObjEventhdlr(SCIP *scip, const char *name)
virtual SCIP_DECL_EVENTEXEC(scip_exec)=0
definition of base class for all clonable classes
ObjEventhdlr(SCIP *scip, const char *name, const char *desc)
Definition: objeventhdlr.h:67
#define SCIP_Bool
Definition: def.h:93
ObjEventhdlr(const ObjEventhdlr &o)
Definition: objeventhdlr.h:82
virtual SCIP_DECL_EVENTINIT(scip_init)
Definition: objeventhdlr.h:119
C++ wrapper for event handlers.
Definition: objeventhdlr.h:52
SCIP_RETCODE SCIPincludeObjEventhdlr(SCIP *scip, scip::ObjEventhdlr *objeventhdlr, SCIP_Bool deleteobject)
Definition of base class for all clonable classes.
Definition: objcloneable.h:47
virtual SCIP_DECL_EVENTEXITSOL(scip_exitsol)
Definition: objeventhdlr.h:146
virtual ~ObjEventhdlr()
Definition: objeventhdlr.h:92
virtual SCIP_DECL_EVENTFREE(scip_free)
Definition: objeventhdlr.h:110
#define SCIP_CALL_ABORT(x)
Definition: def.h:372
ObjEventhdlr & operator=(const ObjEventhdlr &o)=delete
SCIP callable library.